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

The dynamic storage allocation of Knuth, beyond the book, on a memory of \( N \) words simulated by a vector, so that the blocks and their headers can be inspected: More...

#include "ADS/Contract.h"
#include <cstddef>
#include <limits>
#include <map>
#include <set>
#include <vector>
#include "ADS/memory/impl/Allocators.tpp"

Go to the source code of this file.

Classes

class  ADS::BoundaryTagAllocator
 the dynamic storage allocation with boundary tags More...
class  ADS::BuddyAllocator
 the buddy system on \( 2^m \) words More...

Namespaces

namespace  ADS

Detailed description

The dynamic storage allocation of Knuth, beyond the book, on a memory of \( N \) words simulated by a vector, so that the blocks and their headers can be inspected:

  • BoundaryTagAllocator keeps the free blocks on a doubly linked list and gives a request the first block large enough (first fit, Algorithm 2.5A), the smallest one (best fit), or the first one after the last block given (next fit, the roving pointer of Knuth); a block is split if the rest is not too small. Each block has a tag at both ends, its size and whether it is free (the boundary tags of Algorithm 2.5C), so that a freed block merges in \( O( 1 ) \) time with its neighbors if they are free: two free blocks are never adjacent. A request costs \( O( f ) \) time for \( f \) free blocks, all of them for the best fit;
  • BuddyAllocator (Knowlton, Algorithms 2.5R and 2.5S) splits a memory of \( 2^m \) words into blocks of powers of 2, a request taking the smallest power not below it, split from a larger free block by halves; the buddy of the block of size \( 2^k \) at the address \( a \) is at \( a \oplus 2^k \), so that a freed block merges with its buddy, if free, and so on upwards. A request and a free cost \( O( m ) \) steps, each a search of an ordered set of the free blocks of one size, \( O( m \log N ) \) time in all; the internal waste is less than half of a block, and two free buddies are never both on the lists.

Both return the address of the first word of the block given, or \( -1 \) if no block fits. The contracts [see Contract.h]: a request is positive, and free() gets an address that allocate() gave and that is in use; the invariants (the free blocks tile the free memory, never adjacent ones for the boundary tags, never two free buddies) are audits. The operations are defined in Allocators.tpp.

See also
D. E. Knuth, The Art of Computer Programming, vol. 1, 3rd ed., Addison-Wesley, 1997, section 2.5
K. C. Knowlton, A fast storage allocator, Communications of the ACM 8(10), 623-624, 1965, doi:10.1145/365628.365655
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa