26#ifndef _libint2_src_bin_libint_memory_h_
27#define _libint2_src_bin_libint_memory_h_
35template <
typename A,
typename S>
42 const std::shared_ptr<MemoryBlock>&
left,
43 const std::shared_ptr<MemoryBlock>&
right)
50 : address_(other.address_),
54 right_(other.right_) {}
60 address_ = other.address_;
64 right_ = other.right_;
69 Address
address()
const {
return address_; }
71 Size
size()
const {
return size_; }
73 bool free()
const {
return free_; }
75 std::shared_ptr<MemoryBlock>
left()
const {
return left_; }
77 std::shared_ptr<MemoryBlock>
right()
const {
return right_; }
79 void left(
const std::shared_ptr<MemoryBlock>& l) { left_ = l; }
81 void right(
const std::shared_ptr<MemoryBlock>& r) { right_ = r; }
92 const std::shared_ptr<MemoryBlock>& j) {
93 return i->size() < j->size();
99 static bool size_eq(std::shared_ptr<MemoryBlock> i, Size sz) {
100 return i->size() == sz;
106 static bool size_geq(std::shared_ptr<MemoryBlock> i, Size sz) {
107 return i->size() >= sz;
111 const std::shared_ptr<MemoryBlock>& j) {
112 return i->address() < j->address();
118 static bool address_eq(std::shared_ptr<MemoryBlock> i, Address a) {
119 return i->address() == a;
122 static bool is_free(
const std::shared_ptr<MemoryBlock>& i) {
130 address_ = other.address_;
132 size_ += other.
size();
141 std::shared_ptr<this_type> left_;
142 std::shared_ptr<MemoryBlock> right_;
160 static const Address InvalidAddress = -1;
163 typedef std::list<std::shared_ptr<MemBlock> > memblkset;
171 std::shared_ptr<MemBlock> superblock_;
173 Size max_memory_used_;
175 std::shared_ptr<MemBlock> merge_blocks(
176 const std::shared_ptr<MemBlock>& left,
177 const std::shared_ptr<MemBlock>& right);
178 std::shared_ptr<MemBlock> merge_to_superblock(
179 const std::shared_ptr<MemBlock>& blk);
180 void update_max_memory();
203 std::shared_ptr<MemBlock>
superblock()
const {
return superblock_; }
206 const std::shared_ptr<MemBlock>& blk,
const Size& size);
218 const Size& maxsize = ULONG_MAX);
238 const Size& maxsize = ULONG_MAX);
258 const Size& maxsize = ULONG_MAX);
277 const Size& maxsize = ULONG_MAX);
293 static const unsigned int ntypes = 8;
294 std::shared_ptr<MemoryManager> memman(
unsigned int type)
const;
295 std::string label(
unsigned int type)
const;
302typedef std::list<MemoryManager::MemBlock> MemBlockSet;
311void 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:235
Address alloc(const Size &size) override
Implementation of MemoryManager::alloc()
Definition memory.cc:281
FirstFitMemoryManager allocates memory by finding first suitable free block.
Definition src/bin/libint/memory.h:255
Address alloc(const Size &size) override
Implementation of MemoryManager::alloc()
Definition memory.cc:364
LastFitMemoryManager allocates memory by finding last suitable free block.
Definition src/bin/libint/memory.h:274
Address alloc(const Size &size) override
Implementation of MemoryManager::alloc()
Definition memory.cc:434
MemoryBlock<Address,Size> describes a block of raw memory addressed via Address and size described by...
Definition src/bin/libint/memory.h:36
static bool address_less_than(const std::shared_ptr< MemoryBlock > &i, const std::shared_ptr< MemoryBlock > &j)
Returns true if the address of *i is less than the address of *j.
Definition src/bin/libint/memory.h:110
void set_size(const Size &size)
Sets the size.
Definition src/bin/libint/memory.h:86
static bool is_free(const std::shared_ptr< MemoryBlock > &i)
Returns true if *i is free.
Definition src/bin/libint/memory.h:122
static bool size_less_than(const std::shared_ptr< MemoryBlock > &i, const std::shared_ptr< MemoryBlock > &j)
Returns true if the size of *i is less than the size of *j.
Definition src/bin/libint/memory.h:91
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:128
static bool size_geq(std::shared_ptr< MemoryBlock > i, Size sz)
Returns true if the size of *i greater or equal than sz.
Definition src/bin/libint/memory.h:106
Size size() const
Returns size.
Definition src/bin/libint/memory.h:71
std::shared_ptr< MemoryBlock > left() const
Returns the left adjacent block.
Definition src/bin/libint/memory.h:75
void set_address(const Address &address)
Sets the address.
Definition src/bin/libint/memory.h:84
bool free() const
Returns true if the block is free.
Definition src/bin/libint/memory.h:73
static bool size_eq(std::shared_ptr< MemoryBlock > i, Size sz)
Returns true if the size of *i equals sz.
Definition src/bin/libint/memory.h:99
std::shared_ptr< MemoryBlock > right() const
Returns the right adjacent block.
Definition src/bin/libint/memory.h:77
void set_free(bool free)
Sets block's free status.
Definition src/bin/libint/memory.h:88
void right(const std::shared_ptr< MemoryBlock > &r)
Sets the right adjacent block.
Definition src/bin/libint/memory.h:81
const MemoryBlock & operator=(const MemoryBlock &other)
copy A to this
Definition src/bin/libint/memory.h:59
void left(const std::shared_ptr< MemoryBlock > &l)
Sets the left adjacent block.
Definition src/bin/libint/memory.h:79
static bool address_eq(std::shared_ptr< MemoryBlock > i, Address a)
Returns true if the address of *i equals a.
Definition src/bin/libint/memory.h:118
Address address() const
Returns address.
Definition src/bin/libint/memory.h:69
MemoryManagerFactory is a very dumb factory for MemoryManagers.
Definition src/bin/libint/memory.h:291
Class MemoryManager handles allocation and deallocation of raw memory (stack) provided at runtime of ...
Definition src/bin/libint/memory.h:152
memblkset & blocks()
Returns blocks.
Definition src/bin/libint/memory.h:201
Size max_memory_used() const
Returns the max amount of memory used up to this moment.
Definition src/bin/libint/memory.h:190
Size maxmem() const
Returns maxmem.
Definition src/bin/libint/memory.h:199
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:156
std::shared_ptr< MemBlock > superblock() const
Returns the superblock.
Definition src/bin/libint/memory.h:203
void reset()
resets the state of MemoryManager; does not invalidate stats, however
Definition memory.cc:183
virtual void free(const Address &address)
Release a block previously reserved using alloc.
Definition memory.cc:93
std::shared_ptr< MemBlock > steal_from_block(const std::shared_ptr< MemBlock > &blk, const Size &size)
steals size memory from block blk and returns the new block
Definition memory.cc:49
std::shared_ptr< MemBlock > find_block(const Address &a)
finds the block at Address a
Definition memory.cc:80
WorstFitMemoryManager allocates memory by trying to find the largest-possible free block.
Definition src/bin/libint/memory.h:215
Address alloc(const Size &size) override
Implementation of MemoryManager::alloc()
Definition memory.cc:208
Defaults definitions for various parameters assumed by Libint.
Definition algebra.cc:24
void merge(MemBlockSet &blocks)
Merge blocks, if possible.
Definition memory.cc:577
bool can_merge(const MemoryManager::MemBlock &A, const MemoryManager::MemBlock &B)
True if can merge blocks.
Definition memory.cc:564
MemoryManager::MemBlock MemBlock
Very useful nonmember functions to operate on MemBlocks and their containers.
Definition src/bin/libint/memory.h:301