ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Allocators.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File Allocators.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
47/*--------------------------------------------------------------------------*/
48/*----------------------------- DEFINITIONS --------------------------------*/
49/*--------------------------------------------------------------------------*/
50
51#ifndef __ADS_Allocators
52 #define __ADS_Allocators
53/* self-identification: #endif constant */
54
55/*--------------------------------------------------------------------------*/
56/*------------------------------ INCLUDES ----------------------------------*/
57/*--------------------------------------------------------------------------*/
58
59#include "ADS/Contract.h"
60
61#include <cstddef>
62#include <limits>
63#include <map>
64#include <set>
65#include <vector>
66
67/*--------------------------------------------------------------------------*/
68/*--------------------------- NAMESPACE ------------------------------------*/
69/*--------------------------------------------------------------------------*/
70
71namespace ADS
72{
73
74/*--------------------------------------------------------------------------*/
75/*------------------------ CLASS BoundaryTagAllocator ----------------------*/
76/*--------------------------------------------------------------------------*/
77
80{
81
82/*--------------------------------------------------------------------------*/
83 public:
85 enum class Fit {
89 };
90
93 explicit BoundaryTagAllocator( long N , Fit fit = Fit::FIRST );
94
121 long allocate( long n );
122
143 void free( long a );
144
146 [[nodiscard]] long freeWords( void ) const;
147
149 [[nodiscard]] long freeBlocks( void ) const;
150
151/*--------------------------------------------------------------------------*/
152 private:
153 // a block of s words at p: M[ p ] = M[ p + s - 1 ] = +s if free, -s if in
154 // use; a free block has also M[ p + 1 ] = next, M[ p + 2 ] = prev on the
155 // list of the free blocks, whose head is the place N (two extra words)
156
157 void unlink( long p );
158 void link( long p );
159
161 [[nodiscard]] bool tiled( void ) const;
162
163 std::vector< long > M;
164 long N;
165 Fit f_fit;
166 long f_rover;
167 std::set< long > f_used;
168
169 }; // end( class BoundaryTagAllocator )
170
171/*--------------------------------------------------------------------------*/
172/*-------------------------- CLASS BuddyAllocator --------------------------*/
173/*--------------------------------------------------------------------------*/
174
177{
178
179/*--------------------------------------------------------------------------*/
180 public:
184 explicit BuddyAllocator( int m );
185
207 long allocate( long n );
208
224 void free( long a );
225
227 [[nodiscard]] long freeWords( void ) const;
228
231 [[nodiscard]] long blockSize( long a ) const;
232
233/*--------------------------------------------------------------------------*/
234 private:
236 [[nodiscard]] bool merged( void ) const;
237
238 int f_m;
239 std::vector< std::set< long > > f_free;
240 std::map< long , int > f_used;
241
242 }; // end( class BuddyAllocator )
243
244/*--------------------------------------------------------------------------*/
245
246} // end( namespace ADS )
247
248/*--------------------------------------------------------------------------*/
249/*-------------------------- IMPLEMENTATION --------------------------------*/
250/*--------------------------------------------------------------------------*/
251
252#include "ADS/memory/impl/Allocators.tpp"
253
254/*--------------------------------------------------------------------------*/
255
256#endif /* Allocators.h included */
257
258/*--------------------------------------------------------------------------*/
259/*------------------------- End File Allocators.h --------------------------*/
260/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
long freeBlocks(void) const
the number of the free blocks
BoundaryTagAllocator(long N, Fit fit=Fit::FIRST)
the memory of words, one free block, with the choice fit
Fit
the block chosen for a request
Definition Allocators.h:85
@ FIRST
the first one large enough on the list (Algorithm 2.5A)
Definition Allocators.h:86
@ NEXT
the first one large enough after the last one given
Definition Allocators.h:88
@ BEST
the smallest one large enough
Definition Allocators.h:87
long freeWords(void) const
the number of the words in free blocks, tags included
long allocate(long n)
a block of at least words, its first word, or
void free(long a)
frees the block whose first word is
long allocate(long n)
a block of at least words, a power of 2, its address, or
long blockSize(long a) const
the size of the block in use at
void free(long a)
frees the block at
BuddyAllocator(int m)
the memory of words, one free block
long freeWords(void) const
the number of the words in free blocks
Definition ADT.h:96