-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlibmemory.hpp
More file actions
400 lines (328 loc) · 9.3 KB
/
libmemory.hpp
File metadata and controls
400 lines (328 loc) · 9.3 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* Version: MPL 1.1
*
* "The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is The Amiga Research OS, short AROS.
*
* Copyright (C) 1998-2001 AROS - The Amiga Research OS
* Copyright (C) 2004-2011 Ethatron <niels@paradice-insight.us>.
* All Rights Reserved.
*/
#ifndef LIBMEMORY_HPP
#define LIBMEMORY_HPP
/* compiler specific pragmas */
#pragma warning (disable : 4996)
/* leak-detection */
#ifdef WIN32
#ifdef _DEBUG
#define _CRTDBG_MAPALLOC
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#endif
#endif
/* system-definitions */
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <stddef.h>
/* integer for pointer storage */
typedef unsigned char memloc;
typedef uintptr_t pointer;
typedef uintptr_t ptrsize;
/* aligned allocations */
void *__stdcall mallocAligned(ptrsize size);
void *__stdcall callocAligned(ptrsize size);
void __stdcall freeAligned(void *ret);
#include "libmemory-0.0.0/Defines.hpp"
#include "libmemory-0.0.0/Memory.hpp"
#include "libmemory-0.0.0/MemoryHeader.hpp"
#include "libmemory-0.0.0/Pools.hpp"
#include "libmemory-0.0.0/Structures.hpp"
template<ptrsize alignment = AROS_WORSTALIGN>
class MemQueue : private MemPool<alignment> {
public:
MemQueue(const ptrsize blsize, const long int lmt) :
MemPool<alignment>(MEMF_ANY /*| MEMF_CLEAR*/, 16384 * blsize, blsize) {
blocksize = blsize;
blocklimit = lmt;
head = NULL;
nums = 0;
allc = 0;
sizedsize = 0;
}
//~MemQueue() {
// printf("used %d bytes of memory (%d blocks of size %d, %d buffered)\n", getMemUsed(), blocksize, nums, allc);
//}
ptrsize blocksize;
ptrsize sizedsize;
long int blocklimit;
long int nums, allc;
protected:
struct MemBlock {
struct MemBlock *next;
} *head;
public:
/* receive a piece of memory the dimension given the initializer
* this type of allocation is maintained in a reusable cache
*/
inline void *obtainBlock() {
void *block;
if (!(block = head))
block = head = (struct MemBlock *)MemPool<alignment>::AllocPooled(blocksize);
else
allc--;
head = head->next;
nums++;
return block;
}
/* receive a piece of memory of custom dimension
* this type of allocation is uncached
*/
inline void *obtainBlock(ptrsize size) {
if (size > 0) {
sizedsize += size;
return MemPool<alignment>::AllocPooled(size);
}
return NULL;
}
/* receive a piece of memory of custom dimension
* this type of allocation is uncached
*/
inline void *obtainPuddle(ptrsize size) {
if (size > 0) {
sizedsize += size;
return MemPool<alignment>::AllocPuddled(size);
}
return NULL;
}
/* receive multiple pieces of memory the dimension given the initializer
* this type of allocation is uncached
*/
inline void *obtainBlocks(long int num) {
sizedsize += blocksize * num;
return MemPool<alignment>::AllocPooled(blocksize * num);
}
/* giveaway a piece of memory the dimension given the initializer
* this type of allocation is maintained in a reusable cache
*/
inline void releaseBlock(void *block) {
struct MemBlock *mem = (struct MemBlock *)block;
mem->next = head;
head = mem;
nums--;
allc++;
}
/* giveaway a piece of memory of custom dimension
* this type of allocation is uncached
*/
inline void releaseBlock(void *block, ptrsize size) {
if (size > 0) {
sizedsize -= size;
MemPool<alignment>::FreePooled(block, size);
}
}
/* giveaway a piece of memory of custom dimension
* this type of allocation is uncached
*/
inline void releasePuddle(void *block, ptrsize size) {
if (size > 0) {
sizedsize -= size;
MemPool<alignment>::FreePuddled(block, size);
}
}
/* giveaway multiple pieces of memory the dimension given the initializer
* this type of allocation is uncached
*/
inline void releaseBlocks(void *block, long int num) {
sizedsize -= blocksize * num;
MemPool<alignment>::FreePooled(block, blocksize * num);
}
inline void releaseMemAllocated() {
ResetPool();
head = NULL;
}
/* return the amount of memory actively in use, thus not in cache */
inline ptrsize getMemUsed() {
return blocksize * (nums) + sizedsize;
}
/* return the amount of memory taken from the system, thus also the cached */
inline ptrsize getMemAllocated() {
return blocksize * (nums + allc);
}
/* answer if the resource-limit has been reached, this is without effect on
* the allocator, but may be used for implementing memory-reuse algorithms
*/
inline bool reachedLimit() {
return (blocklimit < nums ? true : false);
}
};
class MemQueueable {
public:
/* **************************************************************************
*
* if order > lastbarrier
* use lastbarrier-memorypool
* if order > firstbarrier
* use firstbarrier-memorypool
* if order > basic
* use basic-memorypool
*
* use basic-memorypool[size];
*/
static void *operator new[] (size_t size, class MemQueue<4> *queue) {
void *res;
/* make the allocator thread-safe */
#pragma omp critical
res = queue->obtainBlock(size);
return res;
}
static void operator delete[] (void *adr, size_t size, class MemQueue<4> *queue) {
/* make the allocator thread-safe */
#pragma omp critical
queue->releaseBlock(adr, size);
}
static void operator delete[] (void *adr, class MemQueue<4> *queue) {
/* standard-allocator are thread-safe */
abort();
}
static void *operator new[] (size_t size) {
/* standard-allocator are thread-safe */
abort(); return malloc(size);
}
static void operator delete[] (void *adr, size_t size) {
/* standard-allocator are thread-safe */
abort(); free(adr);
}
static void operator delete[] (void *adr) {
/* standard-allocator are thread-safe */
abort(); free(adr);
}
static void *operator new(size_t size, class MemQueue<4> *queue) {
void *res;
/* make the allocator thread-safe */
#pragma omp critical
res = queue->obtainBlock();
return res;
}
static void operator delete(void *adr, size_t size, class MemQueue<4> *queue) {
/* make the allocator thread-safe */
#pragma omp critical
queue->releaseBlock(adr);
}
static void operator delete(void *adr, class MemQueue<4> *queue) {
/* make the allocator thread-safe */
#pragma omp critical
queue->releaseBlock(adr);
}
static void *operator new(size_t size) {
/* standard-allocator are thread-safe */
return malloc(size);
}
static void operator delete(void *adr, size_t size) {
/* standard-allocator are thread-safe */
free(adr);
}
static void operator delete(void *adr) {
/* standard-allocator are thread-safe */
free(adr);
}
};
class MemAligned {
public:
static void *operator new[] (size_t size) {
return mallocAligned(size);
}
static void operator delete[] (void *adr, size_t size) {
freeAligned(adr);
}
static void operator delete[] (void *adr) {
freeAligned(adr);
}
static void *operator new(size_t size) {
return mallocAligned(size);
}
static void operator delete(void *adr, size_t size) {
freeAligned(adr);
}
static void operator delete(void *adr) {
freeAligned(adr);
}
};
#include <map>
#include <vector>
template<class T>
class MemAllocator {
public:
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
MemAllocator(class MemQueue<4> *q) {
queue = q;
}
// copy constructor
MemAllocator(const MemAllocator &obj) {
queue = obj.queue;
}
private:
void operator = (const MemAllocator &);
public:
class MemQueue<4> *queue;
template<class _Other>
MemAllocator(const MemAllocator<_Other> &other) {
queue = other.queue;
}
~MemAllocator() {
}
template <class U>
struct rebind {
typedef MemAllocator<U> other;
};
pointer address(reference r) const {
return &r;
}
const_pointer address(const_reference r) const {
return &r;
}
pointer allocate(size_type n, const void * /*hint*/=0 ) {
void *ret;
/* make the allocator thread-safe */
#pragma omp critical
ret = queue->obtainPuddle(n * sizeof(T));
return (pointer)ret;
}
void deallocate(pointer p, size_type n) {
/* make the allocator thread-safe */
#pragma omp critical
queue->releasePuddle(p, n * sizeof(T));
}
void construct(pointer p, const T& val) {
new (p) T(val);
}
void destroy(pointer p) {
p->~T();
}
size_type max_size() const {
return ULONG_MAX / sizeof(T);
}
};
template<class T>
bool operator == (const MemAllocator<T>& left, const MemAllocator<T>& right) {
return (left.queue == right.queue);
}
template<class T>
bool operator != (const MemAllocator<T>& left, const MemAllocator<T>& right) {
return (left.queue != right.queue);
}
#endif //LIBMEMORY_HPP