Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
What do you want to defragment? Everything that's mapped is referenced by raw pointers, so you can't move those. You could shuffle the regions assigned to not-yet-mapped handles (trivially by not settling on a region in Allocate, doing all actual work in Map) 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
    
I thought about the defragmentation as a way to optimize the allocations/reduce wasted space. But of course, this process would be carried out in a time when I'm sure there are no mapped blocks (there is such a situation in my app). The mapping process is mainly to ensure nobody is reading or writing to the memory while the back-end, which runs in a separate thread, is using the memory. The mapping works pretty much as a lock on the memory. –  glampert 2 days ago
1  
Thanks for clarifying, that makes some amount of sense. I'm not sure whether it's actually practial/good but it seems possible and could plausibly have the effect you want. –  delnan 2 days ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.