Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MemoryAllocator.h « intern « dualcon « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6ad4ceb5773fb681292466a610db2321bad5894c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifndef __MEMORYALLOCATOR_H__
#define __MEMORYALLOCATOR_H__

#include <stdio.h>
#include <stdlib.h>

#define HEAP_BASE 16
#define UCHAR unsigned char

/**
 * Customized memory allocators that allocates/deallocates memory in chunks
 *
 * @author Tao Ju
 */

/**
 * Base class of memory allocators
 */
class VirtualMemoryAllocator {
 public:
  virtual ~VirtualMemoryAllocator()
  {
  }

  virtual void *allocate() = 0;
  virtual void deallocate(void *obj) = 0;
  virtual void destroy() = 0;
  virtual void printInfo() = 0;

  virtual int getAllocated() = 0;
  virtual int getAll() = 0;
  virtual int getBytes() = 0;

#ifdef WITH_CXX_GUARDEDALLOC
  MEM_CXX_CLASS_ALLOC_FUNCS("DUALCON:VirtualMemoryAllocator")
#endif
};

/**
 * Dynamic memory allocator - allows allocation/deallocation
 *
 * NOTE: there are 4 bytes overhead for each allocated yet unused object.
 */
template<int N> class MemoryAllocator : public VirtualMemoryAllocator {
 private:
  /// Constants
  int HEAP_UNIT, HEAP_MASK;

  /// Data array
  UCHAR **data;

  /// Allocation stack
  UCHAR ***stack;

  /// Number of data blocks
  int datablocknum;

  /// Number of stack blocks
  int stackblocknum;

  /// Size of stack
  int stacksize;

  /// Number of available objects on stack
  int available;

  /**
   * Allocate a memory block
   */
  void allocateDataBlock()
  {
    // Allocate a data block
    datablocknum += 1;
    data = (UCHAR **)realloc(data, sizeof(UCHAR *) * datablocknum);
    data[datablocknum - 1] = (UCHAR *)malloc(HEAP_UNIT * N);

    // Update allocation stack
    for (int i = 0; i < HEAP_UNIT; i++) {
      stack[0][i] = (data[datablocknum - 1] + i * N);
    }
    available = HEAP_UNIT;
  }

  /**
   * Allocate a stack block, to store more deallocated objects
   */
  void allocateStackBlock()
  {
    // Allocate a stack block
    stackblocknum += 1;
    stacksize += HEAP_UNIT;
    stack = (UCHAR ***)realloc(stack, sizeof(UCHAR **) * stackblocknum);
    stack[stackblocknum - 1] = (UCHAR **)malloc(HEAP_UNIT * sizeof(UCHAR *));
  }

 public:
  /**
   * Constructor
   */
  MemoryAllocator()
  {
    HEAP_UNIT = 1 << HEAP_BASE;
    HEAP_MASK = (1 << HEAP_BASE) - 1;

    data = (UCHAR **)malloc(sizeof(UCHAR *));
    data[0] = (UCHAR *)malloc(HEAP_UNIT * N);
    datablocknum = 1;

    stack = (UCHAR ***)malloc(sizeof(UCHAR **));
    stack[0] = (UCHAR **)malloc(HEAP_UNIT * sizeof(UCHAR *));
    stackblocknum = 1;
    stacksize = HEAP_UNIT;
    available = HEAP_UNIT;

    for (int i = 0; i < HEAP_UNIT; i++) {
      stack[0][i] = (data[0] + i * N);
    }
  }

  /**
   * Destructor
   */
  void destroy()
  {
    int i;
    for (i = 0; i < datablocknum; i++) {
      free(data[i]);
    }
    for (i = 0; i < stackblocknum; i++) {
      free(stack[i]);
    }
    free(data);
    free(stack);
  }

  /**
   * Allocation method
   */
  void *allocate()
  {
    if (available == 0) {
      allocateDataBlock();
    }

    // printf("Allocating %d\n", header[ allocated ]) ;
    available--;
    return (void *)stack[available >> HEAP_BASE][available & HEAP_MASK];
  }

  /**
   * De-allocation method
   */
  void deallocate(void *obj)
  {
    if (available == stacksize) {
      allocateStackBlock();
    }

    // printf("De-allocating %d\n", ( obj - data ) / N ) ;
    stack[available >> HEAP_BASE][available & HEAP_MASK] = (UCHAR *)obj;
    available++;
    // printf("%d %d\n", allocated, header[ allocated ]) ;
  }

  /**
   * Print information
   */
  void printInfo()
  {
    printf("Bytes: %d Used: %d Allocated: %d Maxfree: %d\n",
           getBytes(),
           getAllocated(),
           getAll(),
           stacksize);
  }

  /**
   * Query methods
   */
  int getAllocated()
  {
    return HEAP_UNIT * datablocknum - available;
  };

  int getAll()
  {
    return HEAP_UNIT * datablocknum;
  };

  int getBytes()
  {
    return N;
  };

#ifdef WITH_CXX_GUARDEDALLOC
  MEM_CXX_CLASS_ALLOC_FUNCS("DUALCON:MemoryAllocator")
#endif
};

#endif /* __MEMORYALLOCATOR_H__ */