LIBINT 2.9.0
src/bin/libint/memory.h
1/*
2 * Copyright (C) 2004-2024 Edward F. Valeev
3 *
4 * This file is part of Libint compiler.
5 *
6 * Libint compiler is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Libint compiler is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Libint compiler. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <limits.h>
22#include <smart_ptr.h>
23
24#include <list>
25
26#ifndef _libint2_src_bin_libint_memory_h_
27#define _libint2_src_bin_libint_memory_h_
28
29namespace libint2 {
30
35template <typename A, typename S>
37 public:
38 typedef A Address;
39 typedef S Size;
40
41 MemoryBlock(const Address& address, const Size& size, bool free,
42 const std::shared_ptr<MemoryBlock>& left,
43 const std::shared_ptr<MemoryBlock>& right)
44 : address_(address),
45 size_(size),
46 free_(free),
47 left_(left),
48 right_(right) {}
49 MemoryBlock(const MemoryBlock& other)
50 : address_(other.address_),
51 size_(other.size_),
52 free_(other.free_),
53 left_(other.left_),
54 right_(other.right_) {}
55
56 ~MemoryBlock() {}
57
59 const MemoryBlock& operator=(const MemoryBlock& other) {
60 address_ = other.address_;
61 size_ = other.size_;
62 free_ = other.free_;
63 left_ = other.left_;
64 right_ = other.right_;
65 return *this;
66 }
67
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; }
82
84 void set_address(const Address& address) { address_ = address; }
86 void set_size(const Size& size) { size_ = size; }
88 void set_free(bool free) { free_ = free; }
89
91 static bool size_less_than(const std::shared_ptr<MemoryBlock>& i,
92 const std::shared_ptr<MemoryBlock>& j) {
93 return i->size() < j->size();
94 }
99 static bool size_eq(std::shared_ptr<MemoryBlock> i, Size sz) {
100 return i->size() == sz;
101 }
106 static bool size_geq(std::shared_ptr<MemoryBlock> i, Size sz) {
107 return i->size() >= sz;
108 }
110 static bool address_less_than(const std::shared_ptr<MemoryBlock>& i,
111 const std::shared_ptr<MemoryBlock>& j) {
112 return i->address() < j->address();
113 }
118 static bool address_eq(std::shared_ptr<MemoryBlock> i, Address a) {
119 return i->address() == a;
120 }
122 static bool is_free(const std::shared_ptr<MemoryBlock>& i) {
123 return i->free();
124 }
125
128 const MemoryBlock& merge(const MemoryBlock& other) {
129 if (address() > other.address()) {
130 address_ = other.address_;
131 }
132 size_ += other.size();
133 return *this;
134 }
135
136 private:
137 Address address_;
138 Size size_;
139 bool free_;
140 typedef MemoryBlock<Address, Size> this_type;
141 std::shared_ptr<this_type> left_;
142 std::shared_ptr<MemoryBlock> right_;
143
144 MemoryBlock();
145};
146
153 public:
156 typedef intptr_t Address;
157 typedef size_t Size;
159
160 static const Address InvalidAddress = -1;
161
162 protected:
163 typedef std::list<std::shared_ptr<MemBlock> > memblkset;
164
165 private:
167 Size maxmem_;
169 memblkset blks_;
171 std::shared_ptr<MemBlock> superblock_;
173 Size max_memory_used_;
174
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();
181
182 public:
183 virtual ~MemoryManager();
184
186 virtual Address alloc(const Size& size) = 0;
188 virtual void free(const Address& address);
190 Size max_memory_used() const { return max_memory_used_; }
191
193 void reset();
194
195 protected:
196 MemoryManager(const Size& maxmem);
197
199 Size maxmem() const { return maxmem_; }
201 memblkset& blocks() { return blks_; }
203 std::shared_ptr<MemBlock> superblock() const { return superblock_; }
205 std::shared_ptr<MemBlock> steal_from_block(
206 const std::shared_ptr<MemBlock>& blk, const Size& size);
208 std::shared_ptr<MemBlock> find_block(const Address& a);
209};
210
216 public:
217 WorstFitMemoryManager(bool search_exact = true,
218 const Size& maxsize = ULONG_MAX);
219 virtual ~WorstFitMemoryManager();
220
222 Address alloc(const Size& size) override;
223
224 private:
226 bool search_exact_;
227};
228
236 public:
237 BestFitMemoryManager(bool search_exact = true, const Size& tight_fit = 0,
238 const Size& maxsize = ULONG_MAX);
239 virtual ~BestFitMemoryManager();
240
242 Address alloc(const Size& size) override;
243
244 private:
246 bool search_exact_;
248 Size tight_fit_;
249};
250
256 public:
257 FirstFitMemoryManager(bool search_exact = true,
258 const Size& maxsize = ULONG_MAX);
259 virtual ~FirstFitMemoryManager();
260
262 Address alloc(const Size& size) override;
263
264 private:
266 bool search_exact_;
267};
268
275 public:
276 LastFitMemoryManager(bool search_exact = true,
277 const Size& maxsize = ULONG_MAX);
278 virtual ~LastFitMemoryManager();
279
281 Address alloc(const Size& size) override;
282
283 private:
285 bool search_exact_;
286};
287
292 public:
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;
296};
297
302typedef std::list<MemoryManager::MemBlock> MemBlockSet;
303bool size_lessthan(const MemoryManager::MemBlock& A,
304 const MemoryManager::MemBlock& B);
305bool address_lessthan(const MemoryManager::MemBlock& A,
306 const MemoryManager::MemBlock& B);
309 const MemoryManager::MemBlock& B);
311void merge(MemBlockSet& blocks);
312
313}; // namespace libint2
314
315#endif
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