-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml_alloc.h
341 lines (240 loc) · 6.65 KB
/
ml_alloc.h
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
/**
* @file stl_alloc.h
* @brief
* @author malin [email protected]
* @date 2015年03月30日 星期一 14时43分30秒
* @version
* @note
*/
#ifndef __ML_ALLOC_H
#define __ML_ALLOC_H
#include <cstdlib>
#include <cstddef>
#include <cstring>
#include <new>
#include "ml_config.h"
#include "ml_exception.h"
#define __instHROW_BAD_ALLOC throw bad_alloc("no memory!")
__ML_BEGIN_NAMESPACE
template<int inst>
class __malloc_alloc_template
{
private:
static void *oom_malloc(size_t);
static void *oom_realloc(void*, size_t);
static void (*__malloc_alloc_oom_handler) ();
public:
static void *allocate(size_t n)
{
void *res = malloc(n);
if (res == 0) res = oom_malloc(n);
return res;
}
static void deallocate(void *p, size_t /*n*/)
{
free(p);
}
static void *reallocate(void *p, size_t /* old_sz*/, size_t n_sz)
{
void *res = realloc(p, n_sz);
if (res == 0) res = oom_realloc(p, n_sz);
return res;
}
static void (*set_malloc_handler(void (*f)())) ()
{
void (*old)() = __malloc_alloc_oom_handler;
__malloc_alloc_oom_handler = f;
return old;
}
};
template<int inst>
void (*__malloc_alloc_template<inst>::__malloc_alloc_oom_handler)() = 0;
template<int inst>
void* __malloc_alloc_template<inst>::oom_malloc(size_t n)
{
void (*my_malloc_handler)();
void *res = 0;
while (!res)
{
my_malloc_handler = __malloc_alloc_oom_handler;
if (my_malloc_handler == 0) __instHROW_BAD_ALLOC;
my_malloc_handler();
res = malloc(n);
}
return res;
}
template<int inst>
void * __malloc_alloc_template<inst>::oom_realloc(void *p, size_t n)
{
void (*my_malloc_handler)(); void *res = 0;
while (!res)
{
my_malloc_handler = __malloc_alloc_oom_handler;
if (0 == my_malloc_handler) __instHROW_BAD_ALLOC;
res = realloc(p, n);
}
return res;
}
typedef __malloc_alloc_template<0> malloc_alloc;
const size_t __ALIGN = 8, __MAX_BYTES = 128, __NFREELISTS = __MAX_BYTES / __ALIGN;
template<bool thread, int inst>
class __default_alloc_template
{
private:
static size_t bound_up(size_t bytes)
{
return (bytes + __ALIGN - 1) & ~(__ALIGN - 1);
}
static size_t freelist_index(size_t bytes)
{
return (bytes + __ALIGN - 1) / __ALIGN - 1;
}
private:
union obj
{
obj *free_list_link;
char client_data[1];
};
static obj* volatile free_list[__NFREELISTS];
private:
static void *refill(size_t n);
static char *chunk_alloc(size_t size, int &nobjs);
static char *start_free;
static char *end_free;
static size_t heap_size;
public:
static void *allocate(size_t n);
static void deallocate(void *p, size_t n);
static void *reallocate(void *p, size_t old_sz, size_t new_sz);
};
template<bool thread, int inst> char* __default_alloc_template<thread, inst>::start_free = 0;
template<bool thread, int inst> char* __default_alloc_template<thread, inst>::end_free = 0;
template<bool thread, int inst> size_t __default_alloc_template<thread, inst>::heap_size = 0;
template<bool thread, int inst> typename __default_alloc_template<thread, inst>::obj* volatile
__default_alloc_template<thread, inst>::free_list[__NFREELISTS] = {0};
template<bool thread, int inst>
void* __default_alloc_template<thread, inst>::allocate(size_t n)
{
if (n > 128) return malloc_alloc::allocate(n);
obj* volatile *my_free_list = free_list + freelist_index(n); obj *result = *my_free_list;
if (result == 0)
{
return refill(bound_up(n));
}
else *my_free_list = result->free_list_link;
return result;
}
template<bool thread, int inst>
void __default_alloc_template<thread, inst>::deallocate(void *p, size_t n)
{
if (n > 128) malloc_alloc::allocate(n);
else
{
obj* q = (obj*)p, *volatile *my_free_list = free_list + freelist_index(n);
q->free_list_link = *my_free_list; *my_free_list = q;
}
}
template<bool thread, int inst>
void* __default_alloc_template<thread, inst>::reallocate(void *p, size_t old_sz, size_t new_sz)
{
if (old_sz > __MAX_BYTES && new_sz > __MAX_BYTES) return realloc(p, new_sz);
if (bound_up(old_sz) == bound_up(new_sz)) return p;
void *result = allocate(new_sz); size_t copy_sz = old_sz > new_sz ? new_sz : old_sz;
memcpy(result, p, copy_sz); deallocate(p, old_sz);
return result;
}
template<bool thread, int inst>
void* __default_alloc_template<thread, inst>::refill(size_t n)
{
int nobjs = 20; char *chunk = chunk_alloc(n, nobjs);
if (nobjs == 1) return chunk;
obj *result = (obj*)chunk, *cur_obj, *next_obj, *volatile *my_free_list = free_list + freelist_index(n);
*my_free_list = next_obj = (obj*)(chunk + n);
for (int i = 1; i < nobjs - 1; ++i)
{
cur_obj = next_obj; next_obj = (obj*)((char*)next_obj + n);
cur_obj->free_list_link = next_obj;
}
next_obj->free_list_link = 0;
return result;
}
template<bool thread, int inst>
char* __default_alloc_template<thread, inst>::chunk_alloc(size_t size, int &nobjs)
{
size_t total_bytes = size * nobjs, bytes_left = end_free - start_free;
char *result;
if (bytes_left >= total_bytes)
{
result = start_free; start_free += total_bytes;
return result;
}
else if (bytes_left >= size)
{
nobjs = bytes_left / size;
result = start_free; start_free += nobjs * size;
return result;
}
else
{
if (bytes_left > 0)
{
obj* volatile *my_free_list = free_list + freelist_index(bytes_left);
((obj*)start_free)->free_list_link = *my_free_list;
*my_free_list = (obj*)start_free;
}
size_t bytes_to_get = 2 * total_bytes + (heap_size >> 4);
start_free = (char*)malloc(bytes_to_get);
if (start_free == 0)
{
obj *volatile *my_free_list, *p;
for (size_t i = size; i <= __MAX_BYTES; i += __ALIGN)
{
my_free_list = free_list + i / __ALIGN; p = *my_free_list;
if (0 != p)
{
start_free = (char*)p; end_free = start_free + i; *my_free_list = p->free_list_link;
return chunk_alloc(size, nobjs);
}
}
end_free = 0;
start_free = (char*)malloc_alloc::allocate(bytes_to_get);
}
end_free = start_free + bytes_to_get; heap_size += bytes_to_get;
return chunk_alloc(size, nobjs);
}
}
typedef __default_alloc_template<0, 0> alloc;
template<typename T, typename Alloc>
class simple_alloc
{
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef ptrdiff_t diffrence_type;
static T *allocate(size_t n)
{
return 0 == n ? 0 : (T*)Alloc::allocate(n * sizeof(T));
}
static T *allocate()
{
return (T*)Alloc::allocate(sizeof(T));
}
static void deallocate(T *p, size_t n)
{
if (0 != n) Alloc::deallocate(p, sizeof(T) * n);
}
static void deallocate(T *p)
{
Alloc::deallocate(p, sizeof(T));
}
static pointer address(reference x)
{
return &x;
}
};
__ML_END_NAMESPACE
#endif // __ML_ALLOC_H