Advanced Multi-Physics (AMP)
On-Line Documentation
BuddyAllocator.h
Go to the documentation of this file.
1#ifndef included_BuddyAllocator
2#define included_BuddyAllocator
3
4
5#include <cstdint>
6#include <functional>
7#include <stdlib.h>
8
9
15{
16public:
19 size_t bytes,
20 size_t blockSize = 1024,
21 std::function<void *( size_t )> allocator = ::malloc,
22 std::function<void( void * )> deallocator = ::free,
23 std::function<void( void *, size_t )> zero = []( void *, size_t ) {} );
24
27
30
31 // Copy/assignment constructors
32 BuddyAllocator( const BuddyAllocator & ) = delete;
36
38 void *allocate( size_t bytes );
39
41 void deallocate( void *p, size_t bytes );
42
44 static constexpr bool isLIFO() { return false; }
45
46private:
47 uint8_t d_n; // Number of blocks (log2)
48 uint8_t d_log_N; // Size of a block (log2)
49 void *d_memory; // Raw memory pointer
50
51 // Functions to get/clear the level id of the block
52 int8_t *d_size; // Data to hold the level index
53 inline void allocateSize() { d_size = new int8_t[N_blocks()]; }
54 inline void setLevel( int block, int level ) { d_size[block] = level; }
55 inline int getLevel( int block ) { return d_size[block]; }
56
57 // Functions to allocate/free a block from a level (need to be thread-safe)
58 int *d_N;
59 int *d_ptr;
60 int blockAlloc( int level );
61 void blockFree( int level, int block );
62
63 // Functions to allocate/free/zero memory
64 std::function<void *( size_t )> d_allocator;
65 std::function<void( void * )> d_deallocator;
66 std::function<void( void *, size_t )> d_zero;
67
68 // Helper functions
69 inline size_t N_blocks() const { return ( (size_t) 0x01 ) << d_n; }
70 inline size_t N_bytes() const { return ( (size_t) 0x01 ) << ( d_n + d_log_N ); }
71};
72
73
74#endif
void * allocate(size_t bytes)
Allocate memory.
static constexpr bool isLIFO()
Check if the allocator is Last-In-First-Out (LIFO)
std::function< void(void *, size_t)> d_zero
BuddyAllocator(const BuddyAllocator &)=delete
size_t N_blocks() const
size_t N_bytes() const
~BuddyAllocator()
Destructor.
BuddyAllocator(size_t bytes, size_t blockSize=1024, std::function< void *(size_t)> allocator=::malloc, std::function< void(void *)> deallocator=::free, std::function< void(void *, size_t)> zero=[](void *, size_t) {})
Default constructor.
BuddyAllocator & operator=(BuddyAllocator &&)
void blockFree(int level, int block)
BuddyAllocator & operator=(const BuddyAllocator &)=delete
std::function< void(void *)> d_deallocator
std::function< void *(size_t)> d_allocator
int blockAlloc(int level)
BuddyAllocator(BuddyAllocator &&)
void setLevel(int block, int level)
BuddyAllocator()
Empty constructor.
void deallocate(void *p, size_t bytes)
Deallocate memory.
int getLevel(int block)



Advanced Multi-Physics (AMP)
Oak Ridge National Laboratory
Idaho National Laboratory
Los Alamos National Laboratory
This page automatically produced from the
source code by doxygen
Last updated: Tue Mar 10 2026 13:06:41.
Comments on this page