I’m looking for ideas for a heap-manager to handle a very specific situation: Lots and lots of very small allocations, ranging from 12 to 64 bytes each. Anything bigger, I will pass on to the regular heap-manager, so only tiny blocks need be catered for. Only 4-byte alignment is needed.
My main concerns are
- Overhead. The regular libc heap will typically round up an allocation to a multiple of 16 bytes, then add another 16 byte header – this means over 50% overhead on a 20-byte allocation, which sucks.
- Performance
One helpful aspect is that Lua (which is the user of this heap) will tell you the size of the block it’s freeing when it calls free() – this may enable certain optimisations.
I’ll post my current approach, which works ok, but I’d like to improve on it if at all possible. Any ideas?
It is possible to build a heap manager that is very efficient for objects that are all the same size. You could create one of these heaps for each size of object that you need, or if you don’t mind using a bit of space, create one for 16 byte objects, one for 32, and one for 64. The maximum overhead would be 31 bytes for a 33 byte allocation (which would go on the 64 blocksize heap).