CDataAllocator

Last Updated on April 21, 2025 by chase

This weekend I thought I would brush up a little bit on some C++, I thought back to some of the more challenging assignments I had back when I was in school. This one isn’t as wide in scope as that assignment, but it was a great exercise. I’m not one to jump into heavy template usage where it isn’t needed, but this sort of system depends on it for extensibility of multiple data types.

This turned out great for an initial pass like this. It is a node based memory manager, you define a class that specifies how much memory you want each node to hold, as well as how many nodes. Each node can store up to that amount of memory. Each node is tracked in an unordered_map that allows for O(1) retrieval during deletion. A linked list is used for the free list to allow O(1) time during allocation for finding an available node, as well as during adding the deallocated node back to the free list.

What I’ve always enjoyed about memory managers is just how the memory is contiguous, minimizing paging, performance gains and really just keeping data organized.

How the CDataAllocator initializes nodes contiguously in memory

Here is the implementation:

This is a simple example for how it can be used:

Some other additional improvements:

  • Information on how many nodes are free and in use.
  • Allow packing more data into nodes that still have memory bytes available.
  • Sorted free list.

Here is a test use case through my CAsyncTaskManager:

As you can see by using the CDataAllocator64, the memory addresses are all within the same range, and consistent size. For example if we were to calculate an address offset: 0x600C - 0x5FAC or any other consecutive offset, we consistently get 60 bytes. Which aligns with our CDataAllocator64.

However, if we were to just use new allocations here:

We would get something that looks like this:

Which has inconsistent byte offsets between allocated data.

Leave a Reply

Your email address will not be published. Required fields are marked *