LIBINT  2.6.0
src/bin/libint/memory.h
1 /*
2  * Copyright (C) 2004-2019 Edward F. Valeev
3  *
4  * This file is part of Libint.
5  *
6  * Libint 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 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. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #include <limits.h>
22 #include <list>
23 #include <smart_ptr.h>
24 
25 #ifndef _libint2_src_bin_libint_memory_h_
26 #define _libint2_src_bin_libint_memory_h_
27 
28 using namespace std;
29 
30 namespace libint2 {
31 
35  template <typename A, typename S>
37  {
38  public:
39  typedef A Address;
40  typedef S Size;
41 
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)
47  {
48  }
49  MemoryBlock(const MemoryBlock& other) :
50  address_(other.address_), size_(other.size_), free_(other.free_),
51  left_(other.left_), right_(other.right_)
52  {
53  }
54 
55  ~MemoryBlock() {}
56 
58  const MemoryBlock& operator=(const MemoryBlock& other) {
59  address_ = other.address_;
60  size_ = other.size_;
61  free_ = other.free_;
62  left_ = other.left_;
63  right_ = other.right_;
64  return *this;
65  }
66 
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; }
81 
83  void set_address(const Address& address) { address_ = address; }
85  void set_size(const Size& size) { size_ = size; }
87  void set_free(bool free) { free_ = free; }
88 
90  static bool size_less_than(const SafePtr<MemoryBlock>& i,
91  const SafePtr<MemoryBlock>& j) {
92  return i->size() < j->size();
93  }
97  static bool size_eq(SafePtr<MemoryBlock> i, Size sz) {
98  return i->size() == sz;
99  }
103  static bool size_geq(SafePtr<MemoryBlock> i, Size sz) {
104  return i->size() >= sz;
105  }
107  static bool address_less_than(const SafePtr<MemoryBlock>& i,
108  const SafePtr<MemoryBlock>& j) {
109  return i->address() < j->address();
110  }
114  static bool address_eq(SafePtr<MemoryBlock> i, Address a) {
115  return i->address() == a;
116  }
118  static bool is_free(const SafePtr<MemoryBlock>& i) {
119  return i->free();
120  }
121 
123  const MemoryBlock& merge(const MemoryBlock& other) {
124  if (address() > other.address()) {
125  address_ = other.address_;
126  }
127  size_ += other.size();
128  return *this;
129  }
130 
131  private:
132  Address address_;
133  Size size_;
134  bool free_;
135  typedef MemoryBlock<Address,Size> this_type;
136  SafePtr<this_type> left_;
137  SafePtr<MemoryBlock> right_;
138 
139  MemoryBlock();
140  };
141 
148  public:
150  typedef intptr_t Address;
151  typedef size_t Size;
153 
154  static const Address InvalidAddress = -1;
155 
156  protected:
157  typedef std::list< SafePtr<MemBlock> > memblkset;
158 
159  private:
161  Size maxmem_;
163  memblkset blks_;
165  SafePtr<MemBlock> superblock_;
167  Size max_memory_used_;
168 
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();
172 
173 
174  public:
175  virtual ~MemoryManager();
176 
178  virtual Address alloc(const Size& size) =0;
180  virtual void free(const Address& address);
182  Size max_memory_used() const { return max_memory_used_; }
183 
185  void reset();
186 
187  protected:
188  MemoryManager(const Size& maxmem);
189 
191  Size maxmem() const { return maxmem_; }
193  memblkset& blocks() { return blks_;}
195  SafePtr<MemBlock> superblock() const { return superblock_; }
197  SafePtr<MemBlock> steal_from_block(const SafePtr<MemBlock>& blk, const Size& size);
199  SafePtr<MemBlock> find_block(const Address& a);
200 
201  };
202 
203 
209  public:
210  WorstFitMemoryManager(bool search_exact = true, const Size& maxsize = ULONG_MAX);
211  virtual ~WorstFitMemoryManager();
212 
214  Address alloc(const Size& size);
215 
216  private:
218  bool search_exact_;
219  };
220 
228  public:
229  BestFitMemoryManager(bool search_exact = true, const Size& tight_fit = 0, const Size& maxsize = ULONG_MAX);
230  virtual ~BestFitMemoryManager();
231 
233  Address alloc(const Size& size);
234 
235  private:
237  bool search_exact_;
239  Size tight_fit_;
240  };
241 
247  public:
248  FirstFitMemoryManager(bool search_exact = true, const Size& maxsize = ULONG_MAX);
249  virtual ~FirstFitMemoryManager();
250 
252  Address alloc(const Size& size);
253 
254  private:
256  bool search_exact_;
257  };
258 
264  public:
265  LastFitMemoryManager(bool search_exact = true, const Size& maxsize = ULONG_MAX);
266  virtual ~LastFitMemoryManager();
267 
269  Address alloc(const Size& size);
270 
271  private:
273  bool search_exact_;
274  };
275 
280  public:
281  static const unsigned int ntypes = 8;
282  SafePtr<MemoryManager> memman(unsigned int type) const;
283  std::string label(unsigned int type) const;
284  };
285 
290  typedef std::list< MemoryManager::MemBlock > MemBlockSet;
291  bool size_lessthan(const MemoryManager::MemBlock& A, const MemoryManager::MemBlock& B);
292  bool address_lessthan(const MemoryManager::MemBlock& A, const MemoryManager::MemBlock& B);
296  void merge(MemBlockSet& blocks);
297 
298 };
299 
300 #endif
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