Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient C pool allocator?

I'm currently attempting to write a 2D scene graph in C, and I need to decide on a way of storing the child nodes. I'm expecting very many reads and few writes, so a linked list is out of the question due to poor spatial locality of reference, and using realloc every time to add a child node would probably fragment the free list into oblivion. A pool allocator seems to be the best solution, but I can't seem to find any implementations to use. Does anyone know of an allocator that would efficiently handle random-ish allocations and deallocations of a few hundred small structs, or perhaps a better allocation scheme?

like image 307
Electro Avatar asked Nov 19 '10 18:11

Electro


People also ask

What is pool allocator?

Overview. A Pool allocator (or simply, a Memory pool) is a variation of the fast Bump-allocator, which in general allows O(1) allocation, when a free block is found right away, without searching a free-list. To achieve this fast allocation, usually a pool allocator uses blocks of a predefined size.

Where does the allocators are implemented in C++?

In C++ computer programming, allocators are a component of the C++ Standard Library. The standard library provides several data structures, such as list and set, commonly referred to as containers. A common trait among these containers is their ability to change size during the execution of the program.

Which memory allocation is used in game development?

One very common memory usage pattern in game programming, is the need to allocate lots of small blocks of memory of the same size.


2 Answers

I'm preparing to deploy TLSF as a real-time allocator. I haven't had a chance to profile its performance yet, but it seems to work, and the license is right.

According to their docs, its operations execute "a maximum of 168 processor instructions in a x86 architecture". It comes as a single .c file, which compiled without modifications on my system.

like image 193
T.E.D. Avatar answered Sep 21 '22 00:09

T.E.D.


Take a look at halloc, it might be of some help.

http://swapped.cc/halloc/

like image 34
Naveen Avatar answered Sep 22 '22 00:09

Naveen