ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SizeClassAllocator.h File Reference

An allocator of small blocks by size classes, beyond the book: the scheme of the allocators of today (mimalloc, jemalloc, tcmalloc), for one thread, the size of a block given back by its owner (sized deallocation, as std::allocator does), so that a block needs no header. More...

#include "ADS/Contract.h"
#include <bit>
#include <cstddef>
#include <cstdint>
#include <new>
#include <utility>
#include "ADS/memory/impl/SizeClassAllocator.tpp"

Go to the source code of this file.

Classes

class  ADS::SizeClassAllocator
 an allocator of small blocks by size classes, for one thread More...

Namespaces

namespace  ADS

Detailed description

An allocator of small blocks by size classes, beyond the book: the scheme of the allocators of today (mimalloc, jemalloc, tcmalloc), for one thread, the size of a block given back by its owner (sized deallocation, as std::allocator does), so that a block needs no header.

A request of \( n \) bytes, \( 1 \le n \le 32768 \), goes to the least class not below it: 8, the multiples of 16 up to 128, then four classes per power of 2 (160, 192, 224, 256, 320, ...), i.e., the sizes \( 2^e + j \, 2^{e - 2} \) for \( j = 1 , \ldots , 4 \) and \( 7 \le e \le 14 \), so that past 128 bytes the waste is below \( 2^{e-2} \), i.e., below 20% of the block, at least \( 1.25 \cdot 2^e \), and below 25% of the request, more than \( 2^e \) (below 16 bytes up to 128), and the class is found by a shift and a count of the leading zeros, with no table and no loop. Each class has a free list of the blocks given back, the last one first (whose memory is still in the cache), and a page of 64 KB (the size of the largest block, if larger) whose rest is cut a block at a time; a request takes the first free block, or the next one of the page, or a new page, in \( O( 1 ) \) time, and a release puts the block at the head of the list of its class, in \( O( 1 ) \). The pages go back to the system with the allocator (or by release()): memory is not returned while the allocator lives, as with a pool, but the blocks given back serve any later request of their class. A request of more than 32768 bytes goes to operator new.

The contracts [see Contract.h]: a request is of at least one byte, and deallocate() gets a block of this allocator with the size of its request. The operations are defined in SizeClassAllocator.tpp.

See also
D. E. Knuth, The Art of Computer Programming, vol. 1, 3rd ed., Addison-Wesley, 1997, section 2.5 (the lists of available blocks)
M. Masmano, I. Ripoll, A. Crespo, J. Real, TLSF: a new dynamic memory allocator for real-time systems, Proceedings of the 16th Euromicro Conference on Real-Time Systems (ECRTS), 79-88, 2004, doi:10.1109/EMRTS.2004.1311009
E. D. Berger, B. G. Zorn, K. S. McKinley, Reconsidering custom memory allocation, Proceedings of the 17th ACM SIGPLAN Conference on Object-Oriented Programming, Systems, Languages, and Applications (OOPSLA), 1-12, 2002, doi:10.1145/582419.582421
D. Leijen, B. Zorn, L. de Moura, Mimalloc: free list sharding in action, Programming Languages and Systems (APLAS 2019), Lecture Notes in Computer Science 11893, 244-265, 2019, doi:10.1007/978-3-030-34175-6_13
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa