ergo
Allocator.h
Go to the documentation of this file.
1/* Ergo, version 3.8.2, a program for linear scaling electronic structure
2 * calculations.
3 * Copyright (C) 2023 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
4 * and Anastasia Kruchinina.
5 *
6 * This program 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 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Primary academic reference:
20 * Ergo: An open-source program for linear-scaling electronic structure
21 * calculations,
22 * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
23 * Kruchinina,
24 * SoftwareX 7, 107 (2018),
25 * <http://dx.doi.org/10.1016/j.softx.2018.03.005>
26 *
27 * For further information about Ergo, see <http://www.ergoscf.org>.
28 */
29
43#ifndef MAT_ALLOCATOR_HEADER
44#define MAT_ALLOCATOR_HEADER
45
46#include <stdexcept>
47
48namespace mat {
49
50template<class Treal>
52{
53 public:
54 Allocator(int noOfRealsPerBuffer_,
55 int noOfBuffers_) :
56 noOfRealsPerBuffer(noOfRealsPerBuffer_),
57 noOfBuffers(noOfBuffers_)
58 {
61 // Initialize nextFreeIndexList to indicate that all slots are free.
62 for(int i = 0; i < noOfBuffers-1; i++)
63 nextFreeIndexList[i] = i + 1;
64 nextFreeIndexList[noOfBuffers-1] = -1; // last one points to -1
67 }
69 {
70 delete [] buffer;
71 delete [] nextFreeIndexList;
72 }
73 Treal* alloc() {
74 if(firstFreeIndex < 0)
75 throw std::runtime_error("Error in Allocator::alloc(): no free slots.");
76 Treal* ptrToReturn = &buffer[firstFreeIndex*noOfRealsPerBuffer];
77 int firstFreeIndex_new = nextFreeIndexList[firstFreeIndex];
79 firstFreeIndex = firstFreeIndex_new;
81 return ptrToReturn;
82 }
83 void free(Treal* ptr) {
84 if(ptr < buffer || ptr >= &buffer[noOfBuffers * noOfRealsPerBuffer])
85 throw std::runtime_error("Error in Allocator::free(): unknown ptr.");
86 int count = ptr - buffer;
87 if((count % noOfRealsPerBuffer) != 0)
88 throw std::runtime_error("Error in Allocator::free(): bad ptr.");
89 int bufferIdx = count / noOfRealsPerBuffer;
90 if(nextFreeIndexList[bufferIdx] != -1)
91 throw std::runtime_error("Error in Allocator::free(): -1 not found.");
93 firstFreeIndex = bufferIdx;
95 }
96 bool isFull() {
98 return true;
99 return false;
100 }
101 bool isEmpty() {
102 if(noOfOccupiedSlots == 0)
103 return true;
104 return false;
105 }
106 bool ownsPtr(Treal* ptr) {
107 if(ptr < buffer || ptr >= &buffer[noOfBuffers * noOfRealsPerBuffer])
108 return false;
109 return true;
110 }
112 return noOfOccupiedSlots;
113 }
114 private:
117 Treal* buffer;
121}; // end class Allocator
122
123} /* end namespace mat */
124
125#endif
Definition: Allocator.h:52
bool ownsPtr(Treal *ptr)
Definition: Allocator.h:106
Treal * buffer
Definition: Allocator.h:117
int noOfOccupiedSlots
Definition: Allocator.h:120
Treal * alloc()
Definition: Allocator.h:73
int noOfBuffers
Definition: Allocator.h:116
int noOfRealsPerBuffer
Definition: Allocator.h:115
void free(Treal *ptr)
Definition: Allocator.h:83
~Allocator()
Definition: Allocator.h:68
bool isEmpty()
Definition: Allocator.h:101
int firstFreeIndex
Definition: Allocator.h:119
bool isFull()
Definition: Allocator.h:96
Allocator(int noOfRealsPerBuffer_, int noOfBuffers_)
Definition: Allocator.h:54
int getNoOfOccupiedSlots()
Definition: Allocator.h:111
int * nextFreeIndexList
Definition: Allocator.h:118
Definition: allocate.cc:39