I am in the process of implementing a small memory manager. The users of this memory pool will always access the memory bytes via handles. So a memory allocation/deallocation is done with two APIs:
Handle Allocate(size_t numBytes);
void Free(Handle handle);
The allocation/deallocation deals with opaque handles. To actually access the bytes, the user must then map/unmap the memory:
void * Map(Handle handle, int mappingFlags);
void Unmap(Handle handle, void ** ptr);
Mapping flags: are read-only, write-only and read-write.
The average size of the memory blocks should be between 1KB to 1MB, with some eventual very big block in the neighbourhood of 10MB.
The memory pool starts of as one big pre-allocate block. The manager must then handle variable size allocations. When the pool is depleted, the manager can try to ask the system for another big block.
My questions:
1) I'm not sure which memory management scheme would be best employed in the scenario.
2) I think it would be possible to implement a memory defragmentation scheme, thanks to the handles. Am I right?
Allocate
, doing all actual work inMap
) but since you must work around existing mappings this hardly deserves the term "defragmentation". Also, what are you trying to achieve with the allocate/map separation? It's a rather unorthodox API. – delnan 2 days ago