23 #include <smart_ptr.h> 25 #ifndef _libint2_src_bin_libint_memory_h_ 26 #define _libint2_src_bin_libint_memory_h_ 35 template <
typename A,
typename S>
42 MemoryBlock(
const Address& address,
const Size& size,
bool free,
43 const SafePtr<MemoryBlock>& left,
44 const SafePtr<MemoryBlock>& right) :
45 address_(address), size_(size), free_(free),
46 left_(left), right_(right)
50 address_(other.address_), size_(other.size_), free_(other.free_),
51 left_(other.left_), right_(other.right_)
59 address_ = other.address_;
63 right_ = other.right_;
68 Address
address()
const {
return address_; }
70 Size
size()
const {
return size_; }
72 bool free()
const {
return free_; }
74 SafePtr<MemoryBlock>
left()
const {
return left_; }
76 SafePtr<MemoryBlock>
right()
const {
return right_; }
78 void left(
const SafePtr<MemoryBlock>& l) { left_ = l; }
80 void right(
const SafePtr<MemoryBlock>& r) { right_ = r; }
83 void set_address(
const Address& address) { address_ = address; }
85 void set_size(
const Size& size) { size_ = size; }
91 const SafePtr<MemoryBlock>& j) {
92 return i->size() < j->size();
97 static bool size_eq(SafePtr<MemoryBlock> i, Size sz) {
98 return i->size() == sz;
103 static bool size_geq(SafePtr<MemoryBlock> i, Size sz) {
104 return i->size() >= sz;
108 const SafePtr<MemoryBlock>& j) {
109 return i->address() < j->address();
115 return i->address() == a;
118 static bool is_free(
const SafePtr<MemoryBlock>& i) {
124 if (address() > other.
address()) {
125 address_ = other.address_;
127 size_ += other.
size();
136 SafePtr<this_type> left_;
137 SafePtr<MemoryBlock> right_;
154 static const Address InvalidAddress = -1;
157 typedef std::list< SafePtr<MemBlock> > memblkset;
165 SafePtr<MemBlock> superblock_;
167 Size max_memory_used_;
169 SafePtr<MemBlock> merge_blocks(
const SafePtr<MemBlock>& left,
const SafePtr<MemBlock>& right);
170 SafePtr<MemBlock> merge_to_superblock(
const SafePtr<MemBlock>& blk);
171 void update_max_memory();
178 virtual Address alloc(
const Size& size) =0;
180 virtual void free(
const Address& address);
197 SafePtr<MemBlock> steal_from_block(
const SafePtr<MemBlock>& blk,
const Size& size);
199 SafePtr<MemBlock> find_block(
const Address& a);
214 Address alloc(
const Size& size);
229 BestFitMemoryManager(
bool search_exact =
true,
const Size& tight_fit = 0,
const Size& maxsize = ULONG_MAX);
233 Address alloc(
const Size& size);
252 Address alloc(
const Size& size);
269 Address alloc(
const Size& size);
281 static const unsigned int ntypes = 8;
282 SafePtr<MemoryManager> memman(
unsigned int type)
const;
283 std::string label(
unsigned int type)
const;
290 typedef std::list< MemoryManager::MemBlock > MemBlockSet;
296 void merge(MemBlockSet& blocks);
Size size() const
Returns size.
Definition: src/bin/libint/memory.h:70
MemoryManagerFactory is a very dumb factory for MemoryManagers.
Definition: src/bin/libint/memory.h:279
bool free() const
Returns true if the block is free.
Definition: src/bin/libint/memory.h:72
void merge(MemBlockSet &blocks)
Merge blocks, if possible.
Definition: memory.cc:590
void set_free(bool free)
Sets block's free status.
Definition: src/bin/libint/memory.h:87
void set_address(const Address &address)
Sets the address.
Definition: src/bin/libint/memory.h:83
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:24
static bool size_less_than(const SafePtr< MemoryBlock > &i, const SafePtr< MemoryBlock > &j)
Returns true if the size of *i is less than the size of *j.
Definition: src/bin/libint/memory.h:90
Size max_memory_used() const
Returns the max amount of memory used up to this moment.
Definition: src/bin/libint/memory.h:182
static bool size_eq(SafePtr< MemoryBlock > i, Size sz)
Returns true if the size of *i equals sz.
Definition: src/bin/libint/memory.h:97
Address address() const
Returns address.
Definition: src/bin/libint/memory.h:68
LastFitMemoryManager allocates memory by finding last suitable free block.
Definition: src/bin/libint/memory.h:263
bool can_merge(const MemoryManager::MemBlock &A, const MemoryManager::MemBlock &B)
True if can merge blocks.
Definition: memory.cc:580
BestFitMemoryManager allocates memory by trying to find a suitable free block, which is is larger tha...
Definition: src/bin/libint/memory.h:227
void set_size(const Size &size)
Sets the size.
Definition: src/bin/libint/memory.h:85
SafePtr< MemBlock > superblock() const
Returns the superblock.
Definition: src/bin/libint/memory.h:195
memblkset & blocks()
Returns blocks.
Definition: src/bin/libint/memory.h:193
Class MemoryManager handles allocation and deallocation of raw memory (stack) provided at runtime of ...
Definition: src/bin/libint/memory.h:147
MemoryBlock<Address,Size> describes a block of raw memory addressed via Address and size described by...
Definition: src/bin/libint/memory.h:36
const MemoryBlock & merge(const MemoryBlock &other)
Merge A to this (does not check if merge can happen – can_merge(*this,*A) must be already satisfied)....
Definition: src/bin/libint/memory.h:123
intptr_t Address
Negative Address is used to denote an invalid address – hence signed integer.
Definition: src/bin/libint/memory.h:150
WorstFitMemoryManager allocates memory by trying to find the largest-possible free block.
Definition: src/bin/libint/memory.h:208
FirstFitMemoryManager allocates memory by finding first suitable free block.
Definition: src/bin/libint/memory.h:246
static bool is_free(const SafePtr< MemoryBlock > &i)
Returns true if *i is free.
Definition: src/bin/libint/memory.h:118
static bool size_geq(SafePtr< MemoryBlock > i, Size sz)
Returns true if the size of *i greater or equal than sz.
Definition: src/bin/libint/memory.h:103
static bool address_less_than(const SafePtr< MemoryBlock > &i, const SafePtr< MemoryBlock > &j)
Returns true if the address of *i is less than the address of *j.
Definition: src/bin/libint/memory.h:107
void right(const SafePtr< MemoryBlock > &r)
Sets the right adjacent block.
Definition: src/bin/libint/memory.h:80
static bool address_eq(SafePtr< MemoryBlock > i, Address a)
Returns true if the address of *i equals a.
Definition: src/bin/libint/memory.h:114
Size maxmem() const
Returns maxmem.
Definition: src/bin/libint/memory.h:191
void left(const SafePtr< MemoryBlock > &l)
Sets the left adjacent block.
Definition: src/bin/libint/memory.h:78
SafePtr< MemoryBlock > left() const
Returns the left adjacent block.
Definition: src/bin/libint/memory.h:74
SafePtr< MemoryBlock > right() const
Returns the right adjacent block.
Definition: src/bin/libint/memory.h:76
const MemoryBlock & operator=(const MemoryBlock &other)
copy A to this
Definition: src/bin/libint/memory.h:58