Blender  V3.3
MemoryAllocator.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #ifndef __MEMORYALLOCATOR_H__
4 #define __MEMORYALLOCATOR_H__
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 
9 #define HEAP_BASE 16
10 #define UCHAR unsigned char
11 
22  public:
24  {
25  }
26 
27  virtual void *allocate() = 0;
28  virtual void deallocate(void *obj) = 0;
29  virtual void destroy() = 0;
30  virtual void printInfo() = 0;
31 
32  virtual int getAllocated() = 0;
33  virtual int getAll() = 0;
34  virtual int getBytes() = 0;
35 
36 #ifdef WITH_CXX_GUARDEDALLOC
37  MEM_CXX_CLASS_ALLOC_FUNCS("DUALCON:VirtualMemoryAllocator")
38 #endif
39 };
40 
46 template<int N> class MemoryAllocator : public VirtualMemoryAllocator {
47  private:
49  int HEAP_UNIT, HEAP_MASK;
50 
52  UCHAR **data;
53 
55  UCHAR ***stack;
56 
58  int datablocknum;
59 
61  int stackblocknum;
62 
64  int stacksize;
65 
67  int available;
68 
72  void allocateDataBlock()
73  {
74  // Allocate a data block
75  datablocknum += 1;
76  data = (UCHAR **)realloc(data, sizeof(UCHAR *) * datablocknum);
77  data[datablocknum - 1] = (UCHAR *)malloc(HEAP_UNIT * N);
78 
79  // Update allocation stack
80  for (int i = 0; i < HEAP_UNIT; i++) {
81  stack[0][i] = (data[datablocknum - 1] + i * N);
82  }
83  available = HEAP_UNIT;
84  }
85 
89  void allocateStackBlock()
90  {
91  // Allocate a stack block
92  stackblocknum += 1;
93  stacksize += HEAP_UNIT;
94  stack = (UCHAR ***)realloc(stack, sizeof(UCHAR **) * stackblocknum);
95  stack[stackblocknum - 1] = (UCHAR **)malloc(HEAP_UNIT * sizeof(UCHAR *));
96  }
97 
98  public:
103  {
104  HEAP_UNIT = 1 << HEAP_BASE;
105  HEAP_MASK = (1 << HEAP_BASE) - 1;
106 
107  data = (UCHAR **)malloc(sizeof(UCHAR *));
108  data[0] = (UCHAR *)malloc(HEAP_UNIT * N);
109  datablocknum = 1;
110 
111  stack = (UCHAR ***)malloc(sizeof(UCHAR **));
112  stack[0] = (UCHAR **)malloc(HEAP_UNIT * sizeof(UCHAR *));
113  stackblocknum = 1;
114  stacksize = HEAP_UNIT;
115  available = HEAP_UNIT;
116 
117  for (int i = 0; i < HEAP_UNIT; i++) {
118  stack[0][i] = (data[0] + i * N);
119  }
120  }
121 
125  void destroy()
126  {
127  int i;
128  for (i = 0; i < datablocknum; i++) {
129  free(data[i]);
130  }
131  for (i = 0; i < stackblocknum; i++) {
132  free(stack[i]);
133  }
134  free(data);
135  free(stack);
136  }
137 
141  void *allocate()
142  {
143  if (available == 0) {
144  allocateDataBlock();
145  }
146 
147  // printf("Allocating %d\n", header[ allocated ]) ;
148  available--;
149  return (void *)stack[available >> HEAP_BASE][available & HEAP_MASK];
150  }
151 
155  void deallocate(void *obj)
156  {
157  if (available == stacksize) {
158  allocateStackBlock();
159  }
160 
161  // printf("De-allocating %d\n", ( obj - data ) / N ) ;
162  stack[available >> HEAP_BASE][available & HEAP_MASK] = (UCHAR *)obj;
163  available++;
164  // printf("%d %d\n", allocated, header[ allocated ]) ;
165  }
166 
170  void printInfo()
171  {
172  printf("Bytes: %d Used: %d Allocated: %d Maxfree: %d\n",
173  getBytes(),
174  getAllocated(),
175  getAll(),
176  stacksize);
177  }
178 
183  {
184  return HEAP_UNIT * datablocknum - available;
185  };
186 
187  int getAll()
188  {
189  return HEAP_UNIT * datablocknum;
190  };
191 
192  int getBytes()
193  {
194  return N;
195  };
196 
197 #ifdef WITH_CXX_GUARDEDALLOC
198  MEM_CXX_CLASS_ALLOC_FUNCS("DUALCON:MemoryAllocator")
199 #endif
200 };
201 
202 #endif /* __MEMORYALLOCATOR_H__ */
void BLI_kdtree_nd_() free(KDTree *tree)
Definition: kdtree_impl.h:102
#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
#define N