I am trying to develop a heap allocator that has following properties :-
- I will assign a contiguous memory chunk (begin-end address) to it.
- It will allocate using memory only in the assigned chunk. (not a property of std::allocator)
- I can ask it to allocate any size.
- If the chunk is not large enough / it is full, it will return null.
- reasonable performance (It is totally ok to be slower than poll, scope, linear allocators.)
Something like this :-
HeapAllocator heap;
void* beginByte=...;
int numByte=...;
heap.assignMemory(beginByte,numByte);
....
int iWantNumByte=sizeof(SomeClass);
void* iGetYou=heap.allocate(iWantNumByte);
heap.deallocate(iGetYou);
To avoid reinventing the wheel, here are such libraries I found :-
- slow : http://jinie.github.io/memmgr/ (code at https://github.com/jinie/memmgr)
- slow : http:// eli.thegreenplace.net/2008/10/17/memmgr-a-fixed-pool-memory-allocator/ (code at https://github.com/eliben/code-for-blog/tree/master/2008/memmgr)
- https://www.codeproject.com/Articles/14525/Heap-Manager-for-Allocating-Memory-from-a-Shared-M
- slow : https://www.gamedev.net/resources/_/technical/general-programming/c-custom-memory-allocation-r3010
As I skimmed the code :-
- 1 and 2 and 4 : cost max cost of allocate/deallocate = O(amountOfFreeBlock). They are very easy to understand.
- 3 : cost = O( log(amountOfFreeBlock)) The document is quite good, but it doesn't cover every aspect.
- 3 : also has a bug ( comment in that page)
Are there more libraries that I can copy study?
Are there any good books/articles I should read?