How come storage allocators use a circular linked list to store allocated/ free addresses instead of a balanced tree? Traversing a linked list would require O(n) order of complexity whereas a balanced tree could be traversed in O(logn), right? What’s the advantage / reasoning behind it?
How come storage allocators use a circular linked list to store allocated/ free addresses
Share
The premise (“storage allocators use a circular linked list to store allocated/ free addresses”) is not necessarily true. It might be true for some allocators, but it’s not true in general.
If the allocator uses a linked-list-like structure to keep track of blocks of memory, it’s often embedded as meta data in the memory blocks themselves – ie. not as a separate data structure on the side.
For example, each block of memory could start with the status (free/allocated), and the size of the block. This approach basically implements a linked list (using the size, you can easily determine the start address of the next block), but it has other properties that a linked list doesn’t have : you can still find a specific memory block (node) by knowing its memory address.
So, you’d have an O(1) access time (because you, or the compiler, knows the memory address of the block of memory). Merging neighboring free blocks is also straightforward. If it’s necessary to run some kind of de-fragmentation or compaction algorithm, that can be done using the linked-list-like structure. Finding a free block of sufficient size can be done using the linked-list-like structure too (although sometimes a second embedded linked list is used for free blocks specifically, to minimize the overhead of allocation functions).
Of course, this is just one possible approach to the problem. But it goes to show that using a linked list is not necessarily a worse choice than another data structure.