Below is a Python implementation of a basic memory allocator using a fixed-size array, simulating malloc and free operations. The allocator uses a bitmap to track free and allocated blocks.
Code Implementation
class MemoryAllocator:
def __init__(self, size):
"""
Initialize the memory allocator with a fixed-size array.
:param size: Total size of the memory in bytes.
"""
self.size = size
self.memory = bytearray(size) # Fixed-size memory array
self.bitmap = [False] * ((size + 7) // 8) # Bitmap to track allocation (8 bits per byte)
def malloc(self, block_size):
"""
Allocate a block of memory of the given size.
:param block_size: Size of the block to allocate in bytes.
:return: Starting index of the allocated block, or -1 if allocation fails.
"""
if block_size > self.size or block_size <= 0:
return -1
# Find contiguous free space
start_idx = self._find_free_block(block_size)
if start_idx == -1:
return -1
# Mark the allocated block as used in the bitmap
self._mark_allocated(start_idx, block_size)
return start_idx
def free(self, index, block_size):
"""
Free a previously allocated block of memory.
:param index: Starting index of the block to free.
:param block_size: Size of the block to free in bytes.
"""
if index < 0 or index >= self.size or block_size <= 0 or index + block_size > self.size:
raise ValueError("Invalid free operation")
# Mark the block as free in the bitmap
self._mark_free(index, block_size)
def _find_free_block(self, block_size):
"""
Find a contiguous free block of the required size.
:param block_size: Size of the block to find.
:return: Starting index of the free block, or -1 if not found.
"""
consecutive_free = 0
start_idx = -1
for i in range(self.size):
if not self._is_allocated(i):
if start_idx == -1:
start_idx = i
consecutive_free += 1
if consecutive_free == block_size:
return start_idx
else:
consecutive_free = 0
start_idx = -1
return -1
def _mark_allocated(self, index, block_size):
"""
Mark a block as allocated in the bitmap.
:param index: Starting index of the block.
:param block_size: Size of the block.
"""
for i in range(index, index + block_size):
byte_idx = i // 8
bit_offset = i % 8
self.bitmap[byte_idx] |= (1 << bit_offset)
def _mark_free(self, index, block_size):
"""
Mark a block as free in the bitmap.
:param index: Starting index of the block.
:param block_size: Size of the block.
"""
for i in range(index, index + block_size):
byte_idx = i // 8
bit_offset = i % 8
self.bitmap[byte_idx] &= ~(1 << bit_offset)
def _is_allocated(self, index):
"""
Check if a specific index is allocated.
:param index: Index to check.
:return: True if allocated, False otherwise.
"""
byte_idx = index // 8
bit_offset = index % 8
return (self.bitmap[byte_idx] & (1 << bit_offset)) != 0
# Example usage
allocator = MemoryAllocator(16)
print(allocator.malloc(5)) # Allocate 5 bytes, returns starting index (e.g., 0)
print(allocator.malloc(3)) # Allocate 3 bytes, returns starting index (e.g., 5)
allocator.free(0, 5) # Free the first allocated block
print(allocator.malloc(5)) # Re-allocate 5 bytes, returns 0 (reused freed block)
Explanation
- Bitmap Tracking: The bitmap uses 1 bit per byte to track allocation status (
True for allocated, False for free).
- Allocation:
malloc searches for contiguous free blocks and marks them as allocated.
- Deallocation:
free marks the specified block as free in the bitmap.
- First-Fit Strategy: The allocator uses a first-fit strategy to find the first available contiguous block.
\boxed{\text{This implementation simulates basic memory allocation and deallocation using a fixed-size array and a bitmap.}}