Heap optimized for (but not limited to) single-threaded usage Heap optimized for (but not limited to) single-threaded usage c c

Heap optimized for (but not limited to) single-threaded usage


One thought is to maintain a memory allocator per-thread. Pre-assign fairly chunky blocks of memory to each allocator from a global memory pool. Design your algorithm to assign the chunky blocks from adjacent memory addresses (more on that later).

When the allocator for a given thread is low on memory, it requests more memory from the global memory pool. This operation requires a lock, but should occur far less frequently than in your current case. When the allocator for a given thread frees it's last byte, return all memory for that allocator to the global memory pool (assume thread is terminated).

This approach will tend to exhaust memory earlier than your current approach (memory can be reserved for one thread that never needs it). The extent to which that is an issue depends on the thread creation / lifetime / destruction profile of your app(s). You can mitigate that at the expense of additional complexity, e.g. by introducing a signal that a memory allocator for given thread is out of memory, and the global pool is exhaused, that other memory allocators can respond to by freeing some memory.

An advantage of this scheme is that it will tend to eliminate false sharing, as memory for a given thread will tend to be allocated in contiguous address spaces.

On a side note, if you have not already read it, I suggest IBM's Inside Memory Management article for anyone implementing their own memory management.

UPDATE

If the goal is to have very fast memory allocation optimized for a multi-threaded environment (as opposed to learning how to do it yourself), have a look at alternate memory allocators. If the goal is learning, perhaps check out their source code.


It might be a good idea to read Jeff Bonwicks classic papers on the slab allocator and vmem. The original slab allocator sounds somewhat what you're doing. Although not very multithread friendly it might give you some ideas.

The Slab Allocator: An Object-Caching Kernel Memory Allocator

Then he extended the concept with VMEM, which will definitely give you some ideas since it had very nice behavior in a multi cpu environment.

Magazines and Vmem: Extending the Slab Allocator to Many CPUs and Arbitrary Resources