ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SizeClassAllocator.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------- File SizeClassAllocator.h ------------------------*/
3/*--------------------------------------------------------------------------*/
55/*--------------------------------------------------------------------------*/
56/*----------------------------- DEFINITIONS --------------------------------*/
57/*--------------------------------------------------------------------------*/
58
59#ifndef __ADS_SizeClassAllocator
60 #define __ADS_SizeClassAllocator
61/* self-identification: #endif constant */
62
63/*--------------------------------------------------------------------------*/
64/*------------------------------ INCLUDES ----------------------------------*/
65/*--------------------------------------------------------------------------*/
66
67#include "ADS/Contract.h"
68
69#include <bit>
70#include <cstddef>
71#include <cstdint>
72#include <new>
73#include <utility>
74
75/*--------------------------------------------------------------------------*/
76/*--------------------------- NAMESPACE ------------------------------------*/
77/*--------------------------------------------------------------------------*/
78
79namespace ADS
80{
81
82/*--------------------------------------------------------------------------*/
83/*----------------------- CLASS SizeClassAllocator -------------------------*/
84/*--------------------------------------------------------------------------*/
85
88{
89
90/*--------------------------------------------------------------------------*/
91 public:
93 static constexpr std::size_t LARGEST = 32768;
94
96 static constexpr std::size_t PAGE = std::size_t( 1 ) << 16;
97
99 SizeClassAllocator( void ) = default;
104
107
111
114
142 [[nodiscard]] void * allocate( std::size_t n );
143
147 void deallocate( void * p , std::size_t n ) noexcept;
148
169 [[nodiscard]] static unsigned sizeClass( std::size_t n );
170
175 [[nodiscard]] static std::size_t classSize( unsigned c );
176
178 [[nodiscard]] std::size_t bytes( void ) const { return( f_bytes ); }
179
181 void release( void ) noexcept;
182
183/*--------------------------------------------------------------------------*/
184 private:
186 static constexpr unsigned CLASSES = 9 + 4 * 8;
187
189 struct Free
190 {
192 Free * next;
193 };
194
196 struct Page
197 {
199 Page * prev;
201 std::size_t bytes;
202 };
203
205 void * fresh( unsigned c );
206
207 Free * f_free[ CLASSES ] = {};
208 char * f_next[ CLASSES ] = {};
209 char * f_end[ CLASSES ] = {};
210 Page * f_pages = nullptr;
211 std::size_t f_bytes = 0;
212
213 }; // end( class SizeClassAllocator )
214
215/*--------------------------------------------------------------------------*/
216
217} // end( namespace ADS )
218
219/*--------------------------------------------------------------------------*/
220/*-------------------------- IMPLEMENTATION --------------------------------*/
221/*--------------------------------------------------------------------------*/
222
223#include "ADS/memory/impl/SizeClassAllocator.tpp"
224
225/*--------------------------------------------------------------------------*/
226
227#endif /* SizeClassAllocator.h included */
228
229/*--------------------------------------------------------------------------*/
230/*---------------------- End File SizeClassAllocator.h ---------------------*/
231/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
void release(void) noexcept
frees all the pages; the blocks given are no longer valid
SizeClassAllocator & operator=(const SizeClassAllocator &)=delete
no copy, since its blocks are in its pages
static unsigned sizeClass(std::size_t n)
the class of a request of bytes: 0 for , up to 128, and otherwise, with ,
~SizeClassAllocator()
frees the pages
Definition SizeClassAllocator.h:113
static std::size_t classSize(unsigned c)
the size of the class : 8, up to 128, and otherwise with and
SizeClassAllocator & operator=(SizeClassAllocator &&other) noexcept
the pages of other, which is left empty, after those of this allocator are freed
SizeClassAllocator(void)=default
the allocator with no page
static constexpr std::size_t PAGE
the bytes of a page
Definition SizeClassAllocator.h:96
SizeClassAllocator(const SizeClassAllocator &)=delete
no copy, since its blocks are in its pages
SizeClassAllocator(SizeClassAllocator &&other) noexcept
the pages of other, which is left empty
void * allocate(std::size_t n)
a block of bytes at least, aligned to 16 bytes (8 for the class of 8 bytes)
std::size_t bytes(void) const
the memory of the pages, in bytes
Definition SizeClassAllocator.h:178
void deallocate(void *p, std::size_t n) noexcept
gives back the block of a request of bytes, pushed on for its class
static constexpr std::size_t LARGEST
the largest request served by the classes
Definition SizeClassAllocator.h:93
Definition ADT.h:96