Blender  V2.93
MemoryAllocator.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #ifndef __MEMORYALLOCATOR_H__
18 #define __MEMORYALLOCATOR_H__
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 #define HEAP_BASE 16
24 #define UCHAR unsigned char
25 
36  public:
38  {
39  }
40 
41  virtual void *allocate() = 0;
42  virtual void deallocate(void *obj) = 0;
43  virtual void destroy() = 0;
44  virtual void printInfo() = 0;
45 
46  virtual int getAllocated() = 0;
47  virtual int getAll() = 0;
48  virtual int getBytes() = 0;
49 
50 #ifdef WITH_CXX_GUARDEDALLOC
51  MEM_CXX_CLASS_ALLOC_FUNCS("DUALCON:VirtualMemoryAllocator")
52 #endif
53 };
54 
60 template<int N> class MemoryAllocator : public VirtualMemoryAllocator {
61  private:
63  int HEAP_UNIT, HEAP_MASK;
64 
66  UCHAR **data;
67 
69  UCHAR ***stack;
70 
72  int datablocknum;
73 
75  int stackblocknum;
76 
78  int stacksize;
79 
81  int available;
82 
86  void allocateDataBlock()
87  {
88  // Allocate a data block
89  datablocknum += 1;
90  data = (UCHAR **)realloc(data, sizeof(UCHAR *) * datablocknum);
91  data[datablocknum - 1] = (UCHAR *)malloc(HEAP_UNIT * N);
92 
93  // Update allocation stack
94  for (int i = 0; i < HEAP_UNIT; i++) {
95  stack[0][i] = (data[datablocknum - 1] + i * N);
96  }
97  available = HEAP_UNIT;
98  }
99 
103  void allocateStackBlock()
104  {
105  // Allocate a stack block
106  stackblocknum += 1;
107  stacksize += HEAP_UNIT;
108  stack = (UCHAR ***)realloc(stack, sizeof(UCHAR **) * stackblocknum);
109  stack[stackblocknum - 1] = (UCHAR **)malloc(HEAP_UNIT * sizeof(UCHAR *));
110  }
111 
112  public:
117  {
118  HEAP_UNIT = 1 << HEAP_BASE;
119  HEAP_MASK = (1 << HEAP_BASE) - 1;
120 
121  data = (UCHAR **)malloc(sizeof(UCHAR *));
122  data[0] = (UCHAR *)malloc(HEAP_UNIT * N);
123  datablocknum = 1;
124 
125  stack = (UCHAR ***)malloc(sizeof(UCHAR **));
126  stack[0] = (UCHAR **)malloc(HEAP_UNIT * sizeof(UCHAR *));
127  stackblocknum = 1;
128  stacksize = HEAP_UNIT;
129  available = HEAP_UNIT;
130 
131  for (int i = 0; i < HEAP_UNIT; i++) {
132  stack[0][i] = (data[0] + i * N);
133  }
134  }
135 
139  void destroy()
140  {
141  int i;
142  for (i = 0; i < datablocknum; i++) {
143  free(data[i]);
144  }
145  for (i = 0; i < stackblocknum; i++) {
146  free(stack[i]);
147  }
148  free(data);
149  free(stack);
150  }
151 
155  void *allocate()
156  {
157  if (available == 0) {
158  allocateDataBlock();
159  }
160 
161  // printf("Allocating %d\n", header[ allocated ]) ;
162  available--;
163  return (void *)stack[available >> HEAP_BASE][available & HEAP_MASK];
164  }
165 
169  void deallocate(void *obj)
170  {
171  if (available == stacksize) {
172  allocateStackBlock();
173  }
174 
175  // printf("De-allocating %d\n", ( obj - data ) / N ) ;
176  stack[available >> HEAP_BASE][available & HEAP_MASK] = (UCHAR *)obj;
177  available++;
178  // printf("%d %d\n", allocated, header[ allocated ]) ;
179  }
180 
184  void printInfo()
185  {
186  printf("Bytes: %d Used: %d Allocated: %d Maxfree: %d\n",
187  getBytes(),
188  getAllocated(),
189  getAll(),
190  stacksize);
191  }
192 
197  {
198  return HEAP_UNIT * datablocknum - available;
199  };
200 
201  int getAll()
202  {
203  return HEAP_UNIT * datablocknum;
204  };
205 
206  int getBytes()
207  {
208  return N;
209  };
210 
211 #ifdef WITH_CXX_GUARDEDALLOC
212  MEM_CXX_CLASS_ALLOC_FUNCS("DUALCON:MemoryAllocator")
213 #endif
214 };
215 
216 #endif /* __MEMORYALLOCATOR_H__ */
void BLI_kdtree_nd_() free(KDTree *tree)
Definition: kdtree_impl.h:116
#define UCHAR
#define HEAP_BASE
void deallocate(void *obj)
virtual int getBytes()=0
virtual int getAllocated()=0
virtual int getAll()=0
virtual ~VirtualMemoryAllocator()
virtual void * allocate()=0
virtual void deallocate(void *obj)=0
virtual void destroy()=0
virtual void printInfo()=0
params N