cmemory-managementmallocallocation

What's the difference between a memory arena and a memory pool?


I've seen both terms used for wrappers to memory allocators, whats the difference between these? (if any)


Solution

  • The difference is whether the objects are initialized.

    A memory arena is usually used in performance-critical or embedded general C programming. It is usually a statically-allocated char[] or memory-mapped void *. You can ask for a length of memory and cast the resulting void pointer to whatever type you want.

    A memory pool is usually used in game development. For optimal performance these have type MyObject[], and upon running out of pooled objects people skip the pool and allocate/free from dynamic memory. Other people allow the pool to grow by using a linked list, which may be slower. The point of a pool is that it saves the effort made to initialize the objects, which may be more complicated than just memset, such as a pool for variable-length strings, which includes pointers. So a memory pool is just a collection of initialized objects of a single type ready to use.

    A memory allocator is complementary to these two data structures. An allocator is a function. Using an allocator is like using a getter function instead of reading a global variable directly. One general-purpose allocator is the malloc standard library function. It can be implemented differently without the consumer caring about the details. For example different glibc/BSD/macOS libc versions may have used a different mix of brk or mmap backings or numbers of arenas, Windows uses another implementation, and some applications choose to use the tcmalloc or jemalloc alternative memory allocator implementations.

    The difference between a memory pool and connection pool is that a memory pool does not contain any open IO resources for sockets or files. However, in 2012 not everyone observed the distinction between the four. In 2017, many of the distinctions were made by the C++ gamedev community. From 2023, the Linux Rust/C interoperability effort is making some of these distinctions.