25#ifndef _libint2_src_bin_libint_memory_h_
26#define _libint2_src_bin_libint_memory_h_
33 template <
typename A,
typename S>
41 const SafePtr<MemoryBlock>&
left,
42 const SafePtr<MemoryBlock>&
right) :
48 address_(other.address_), size_(other.size_), free_(other.free_),
49 left_(other.left_), right_(other.right_)
57 address_ = other.address_;
61 right_ = other.right_;
66 Address
address()
const {
return address_; }
68 Size
size()
const {
return size_; }
70 bool free()
const {
return free_; }
72 SafePtr<MemoryBlock>
left()
const {
return left_; }
74 SafePtr<MemoryBlock>
right()
const {
return right_; }
76 void left(
const SafePtr<MemoryBlock>& l) { left_ = l; }
78 void right(
const SafePtr<MemoryBlock>& r) { right_ = r; }
89 const SafePtr<MemoryBlock>& j) {
90 return i->size() < j->size();
95 static bool size_eq(SafePtr<MemoryBlock> i, Size sz) {
96 return i->size() == sz;
101 static bool size_geq(SafePtr<MemoryBlock> i, Size sz) {
102 return i->size() >= sz;
106 const SafePtr<MemoryBlock>& j) {
107 return i->address() < j->address();
113 return i->address() == a;
116 static bool is_free(
const SafePtr<MemoryBlock>& i) {
123 address_ = other.address_;
125 size_ += other.
size();
134 SafePtr<this_type> left_;
135 SafePtr<MemoryBlock> right_;
152 static const Address InvalidAddress = -1;
155 typedef std::list< SafePtr<MemBlock> > memblkset;
163 SafePtr<MemBlock> superblock_;
165 Size max_memory_used_;
167 SafePtr<MemBlock> merge_blocks(
const SafePtr<MemBlock>& left,
const SafePtr<MemBlock>& right);
168 SafePtr<MemBlock> merge_to_superblock(
const SafePtr<MemBlock>& blk);
169 void update_max_memory();
195 SafePtr<MemBlock>
steal_from_block(
const SafePtr<MemBlock>& blk,
const Size& size);
227 BestFitMemoryManager(
bool search_exact =
true,
const Size& tight_fit = 0,
const Size& maxsize = ULONG_MAX);
279 static const unsigned int ntypes = 8;
280 SafePtr<MemoryManager> memman(
unsigned int type)
const;
281 std::string label(
unsigned int type)
const;
288 typedef std::list< MemoryManager::MemBlock > MemBlockSet;
294 void merge(MemBlockSet& blocks);
BestFitMemoryManager allocates memory by trying to find a suitable free block, which is is larger tha...
Definition: src/bin/libint/memory.h:225
Address alloc(const Size &size) override
Implementation of MemoryManager::alloc()
Definition: memory.cc:276
FirstFitMemoryManager allocates memory by finding first suitable free block.
Definition: src/bin/libint/memory.h:244
Address alloc(const Size &size) override
Implementation of MemoryManager::alloc()
Definition: memory.cc:362
LastFitMemoryManager allocates memory by finding last suitable free block.
Definition: src/bin/libint/memory.h:261
Address alloc(const Size &size) override
Implementation of MemoryManager::alloc()
Definition: memory.cc:436
MemoryBlock<Address,Size> describes a block of raw memory addressed via Address and size described by...
Definition: src/bin/libint/memory.h:35
void set_size(const Size &size)
Sets the size.
Definition: src/bin/libint/memory.h:83
SafePtr< MemoryBlock > right() const
Returns the right adjacent block.
Definition: src/bin/libint/memory.h:74
SafePtr< MemoryBlock > left() const
Returns the left adjacent block.
Definition: src/bin/libint/memory.h:72
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:121
static bool address_eq(SafePtr< MemoryBlock > i, Address a)
Returns true if the address of *i equals a.
Definition: src/bin/libint/memory.h:112
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:88
static bool is_free(const SafePtr< MemoryBlock > &i)
Returns true if *i is free.
Definition: src/bin/libint/memory.h:116
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:105
Size size() const
Returns size.
Definition: src/bin/libint/memory.h:68
void set_address(const Address &address)
Sets the address.
Definition: src/bin/libint/memory.h:81
bool free() const
Returns true if the block is free.
Definition: src/bin/libint/memory.h:70
void set_free(bool free)
Sets block's free status.
Definition: src/bin/libint/memory.h:85
const MemoryBlock & operator=(const MemoryBlock &other)
copy A to this
Definition: src/bin/libint/memory.h:56
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:101
void right(const SafePtr< MemoryBlock > &r)
Sets the right adjacent block.
Definition: src/bin/libint/memory.h:78
Address address() const
Returns address.
Definition: src/bin/libint/memory.h:66
void left(const SafePtr< MemoryBlock > &l)
Sets the left adjacent block.
Definition: src/bin/libint/memory.h:76
static bool size_eq(SafePtr< MemoryBlock > i, Size sz)
Returns true if the size of *i equals sz.
Definition: src/bin/libint/memory.h:95
MemoryManagerFactory is a very dumb factory for MemoryManagers.
Definition: src/bin/libint/memory.h:277
Class MemoryManager handles allocation and deallocation of raw memory (stack) provided at runtime of ...
Definition: src/bin/libint/memory.h:145
memblkset & blocks()
Returns blocks.
Definition: src/bin/libint/memory.h:191
Size max_memory_used() const
Returns the max amount of memory used up to this moment.
Definition: src/bin/libint/memory.h:180
Size maxmem() const
Returns maxmem.
Definition: src/bin/libint/memory.h:189
SafePtr< MemBlock > find_block(const Address &a)
finds the block at Address a
Definition: memory.cc:80
virtual Address alloc(const Size &size)=0
Reserve a block and return its address.
intptr_t Address
Negative Address is used to denote an invalid address – hence signed integer.
Definition: src/bin/libint/memory.h:148
SafePtr< MemBlock > superblock() const
Returns the superblock.
Definition: src/bin/libint/memory.h:193
SafePtr< MemBlock > steal_from_block(const SafePtr< MemBlock > &blk, const Size &size)
steals size memory from block blk and returns the new block
Definition: memory.cc:50
void reset()
resets the state of MemoryManager; does not invalidate stats, however
Definition: memory.cc:181
virtual void free(const Address &address)
Release a block previously reserved using alloc.
Definition: memory.cc:92
WorstFitMemoryManager allocates memory by trying to find the largest-possible free block.
Definition: src/bin/libint/memory.h:206
Address alloc(const Size &size) override
Implementation of MemoryManager::alloc()
Definition: memory.cc:207
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:24
void merge(MemBlockSet &blocks)
Merge blocks, if possible.
Definition: memory.cc:590
bool can_merge(const MemoryManager::MemBlock &A, const MemoryManager::MemBlock &B)
True if can merge blocks.
Definition: memory.cc:580
MemoryManager::MemBlock MemBlock
Very useful nonmember functions to operate on MemBlocks and their containers.
Definition: src/bin/libint/memory.h:287