I haven’t written some c++ now for a good six months, I’ve been busy within Unity, c#, and python for the most part, at the time of this post. This weekend I thought I would brush up a little bit, 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 really kind of depends on it for extensibility of multiple data types.

This turned out pretty 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. What I’ve always enjoyed about memory managers is just how the memory is contiguous, minimizing paging, performance gains if done right and really just helping to keep things 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 of the improvements for this one could be: to associate a key to each node in it’s header, so when we delete we can collect it in O(k) time through an unordered_map. Right now I’m utilizing a linked list to traverse the nodes. Also, more information on how many nodes are free and in use. Another awesome improvement would be to mark a node as reusable, so we can pack more data into nodes that still have memory bytes available for storage. As well as padding validation, to make sure memory hasn’t been trampled. Maybe also, memory range verification when deleting data – to make sure it’s something that most likely can be deleted in the particular allocator. Just a few things I’ve been thinking about, it was a fun little exercise.

Leave a Reply

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