-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory-manager.c
564 lines (499 loc) · 16.5 KB
/
memory-manager.c
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*============================================================================
This file is part of FLINT.
FLINT 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.
FLINT 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 FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===============================================================================*/
/****************************************************************************
memory-manager.c: FLINT-wide memory management
Copyright (C) 2007, William Hart and David Harvey
*****************************************************************************/
#include <stdlib.h>
#include <gmp.h>
#include <string.h>
#include <stdio.h>
#include "flint.h"
#include "memory-manager.h"
#define DEBUG 0 // Switches to debugging memory allocators
#define DEBUG_PRINT 0 // Prints info about all allocations and releases
#if DEBUG
/*-----------------------------------------------------------------------------------------------
Debug version of stack based memory managers
------------------------------------------------------------------------------------------------*/
void * mempts[200000];
unsigned long upto = 0;
void * flint_stack_alloc(unsigned long length)
{
#if DEBUG_PRINT
printf("Allocating %ld limbs\n", length);
#endif
if (upto == 200000)
{
printf("Error: no free stack nodes in flint_stack_alloc\n");
abort();
}
unsigned long * block = malloc((length+101)*sizeof(unsigned long));
if (block == NULL)
{
printf("Error: unable to allocate memory in flint_stack_alloc\n");
abort();
}
mempts[upto] = (void*) block;
upto++;
block[0] = length;
unsigned long i;
for (i = 0; i < 100; i++)
block[length+i+1] = 0;
return (void*) (block+1);
}
void * flint_stack_alloc_bytes(unsigned long bytes)
{
unsigned long length = ((bytes-1)>>FLINT_LG_BYTES_PER_LIMB)+1;
#if DEBUG_PRINT
printf("Allocating %ld limbs\n", length);
#endif
if (upto == 200000)
{
printf("Error: no free stack nodes in flint_stack_alloc_bytes\n");
abort();
}
unsigned long * block = malloc(sizeof(unsigned long)*(length + 101));
if (block == NULL)
{
printf("Error: unable to allocate memory in flint_stack_alloc_bytes\n");
abort();
}
mempts[upto] = (void*) block;
upto++;
block[0] = length;
unsigned long i;
for (i = 0; i < 100; i++)
block[length+i+1] = 0L;
return (void*) (block+1);
}
void flint_stack_release()
{
if (upto == 0)
{
printf("Error: attempt to free unallocated block in flint_stack_release\n");
abort();
}
upto--;
unsigned long * block = mempts[upto];
unsigned long length = block[0];
#if DEBUG_PRINT
printf("Releasing %ld limbs\n", length);
#endif
unsigned long i;
for (i = 0; i < 100; i++)
if (block[length+i+1] != 0L)
{
printf("Error: Block overrun detected by stack memory allocator!!\n");
abort();
}
free(mempts[upto]);
}
void * flint_stack_alloc_small(unsigned long length)
{
return flint_stack_alloc(length);
}
void flint_stack_release_small(void)
{
flint_stack_release();
}
void flint_stack_cleanup(void)
{
if (upto) printf("Error: stack allocator detected mismatch on cleanup!\n");
}
#else
/*
Stack based memory manager to allocate an array of limbs of the given length.
It returns a (void*) which needs to be typecast to the required object,
e.g. mp_limb_t* or mp_limb_t**, etc.
Limbs must be released in the reverse order to that in which they were allocated.
*/
#define EXPIRE_AFTER 3 // 1/4-th number of allocs before expiring unused allocated space
#define RESALLOC 100 //allocate this many mp_limb_t's at once to save on overheads
typedef struct limb_mem_t //record for managing all allocations of limbs of a certain bitsize
{
unsigned long remaining; //number of remaining limbs which are available for allocation
unsigned long length; //total length of array of limbs this record manages
mp_limb_t* point; //pointer to the next available mp_limb_t
int expire; //how long till we expire
int allocated; //1 if some of the space is allocated, 0 otherwise (will not expire if allocated)
struct limb_mem_t* next; //next record
struct limb_mem_t* prev; //previous record
} limb_mem_t;
typedef struct limb_memp_t //record for managing a particular allocation of memory for a limb array
{
limb_mem_t* point; //which record controls this allocation
unsigned long length; //how many limbs allocated
} limb_memp_t;
THREAD limb_mem_t* head_mpn = NULL; //start of doubly linked list of records
THREAD limb_mem_t* last_mpn = NULL; //last record in linked list
THREAD limb_memp_t* top_mpn = NULL; //top of stack of limb_memp_t's
THREAD limb_memp_t* reservoir_mpn; //array of preallocated limb_memp_t's
THREAD unsigned int rescount_mpn = 0; //counter for which limb_memp_t we are upto in the reservoir_mpn
// todo: deal with possible out of memory situation when allocating
void* flint_stack_alloc(unsigned long length)
{
static THREAD limb_mem_t* curr;
static THREAD limb_mem_t* temp;
static THREAD int initialised = 0; //has limb_alloc been initialised
static THREAD unsigned int currentalloc = 0; //total number of limb_memp_t's in reservoir_mpn
static THREAD limb_memp_t* tempres;
static THREAD int check=0;
void* alloc_d;
check++;
//allocate another block of limb_memp_t's if none are currently allocated, or the reservoir_mpn is depleted
if (rescount_mpn==currentalloc) // need more limb_memp_t's
{
if (!initialised)
{
reservoir_mpn = (limb_memp_t*)malloc(RESALLOC*sizeof(limb_memp_t));
rescount_mpn=0;
initialised = 1;
currentalloc = RESALLOC;
} else
{
//copy old reservoir_mpn into larger one
tempres = reservoir_mpn;
reservoir_mpn = (limb_memp_t*)malloc((currentalloc+RESALLOC)*sizeof(limb_memp_t));
memcpy(reservoir_mpn,tempres,currentalloc*sizeof(limb_memp_t));
currentalloc+=RESALLOC;
//free old reservoir_mpn
free(tempres);
}
}
curr = head_mpn;
if (curr != NULL)
{
do
{
//search for data of requested size
if ((curr->remaining >= length) && (curr->remaining < 2*length))//appropriate unallocated space found, so allocate it
{
alloc_d = (void*)curr->point;
curr->point+=length;
curr->remaining-=length;
curr->allocated=1;
top_mpn=&reservoir_mpn[rescount_mpn];
top_mpn->point=curr;
top_mpn->length=length;
//check if any remaining nodes have expired, expire them and return
if ((check&3)==0)
{
do
{
if (!curr->allocated)
{
curr->expire--;
if (curr->expire == 0)
{
free(curr->point);
if (curr==last_mpn) last_mpn = curr->prev;
else curr->next->prev = curr->prev;
if (curr==head_mpn) head_mpn=curr->next;
else (curr->prev->next = curr->next);
temp=curr;
curr = curr->next;
free(temp);
} else curr = curr->next;
} else curr = curr->next;
} while (curr != NULL);
}
rescount_mpn++;
return alloc_d;
}
//update expiry information for curr if necessary and possibly expire space
if (((check&3)==0)&&(!curr->allocated))
{
curr->expire--;
if (curr->expire == 0)
{
free(curr->point);
if (curr==last_mpn) last_mpn = curr->prev;
else curr->next->prev = curr->prev;
if (curr==head_mpn) head_mpn=curr->next;
else (curr->prev->next = curr->next);
temp=curr;
curr = curr->next;
free(temp);
} else curr = curr->next;
} else curr = curr->next;
} while (curr != NULL);
//Nothing suitable found, so initialise data of the requested type
//attach to last_mpn->next
alloc_d = malloc(length*sizeof(mp_limb_t));
//set up the new record, and last_mpn to point to this new record
last_mpn->next = (limb_mem_t*) malloc(sizeof(limb_mem_t));
last_mpn->next->prev = last_mpn;
last_mpn=last_mpn->next;
last_mpn->point = (mp_limb_t*)alloc_d+length;
last_mpn->next = NULL;
last_mpn->remaining=0;
last_mpn->allocated=1;
last_mpn->length=length;
top_mpn=&reservoir_mpn[rescount_mpn];
top_mpn->point=last_mpn;
top_mpn->length=length;
rescount_mpn++;
return alloc_d;
}
/*first time anything has been allocated
so do the actual allocation of limbs*/
//set up the new record, and head_mpn and last_mpn to point to this single new record
alloc_d = malloc(length*sizeof(mp_limb_t));
head_mpn = (limb_mem_t*) malloc(sizeof(limb_mem_t));
head_mpn->point = (mp_limb_t*)alloc_d+length;
head_mpn->next = NULL;
head_mpn->prev = NULL;
head_mpn->remaining=0;
head_mpn->allocated=1;
head_mpn->length=length;
last_mpn = head_mpn;
top_mpn=&reservoir_mpn[rescount_mpn];
top_mpn->point=head_mpn;
top_mpn->length=length;
rescount_mpn++;
return alloc_d;
}
void* flint_stack_alloc_bytes(unsigned long bytes)
{
return flint_stack_alloc((bytes-1)/FLINT_BYTES_PER_LIMB+1);
}
void flint_stack_release()
{
unsigned long length = top_mpn->length;
//adjust record to reflect the fact that the limbs have been released back to the stack
top_mpn->point->point-=length;
top_mpn->point->remaining+=length;
//if no limbs of that size are allocated any more set them to expire if not eventually used
if (top_mpn->point->remaining == top_mpn->point->length)
{
top_mpn->point->allocated=0;
top_mpn->point->expire=EXPIRE_AFTER;
}
//release limb_memp_t back into reservoir_mpn
top_mpn--;
rescount_mpn--;
}
/*-----------------------------------------------------------------------------------------------*/
#define FLINT_SMALL_BLOCK_SIZE 10000L
THREAD mp_limb_t * block_ptr = NULL;
THREAD unsigned long block_left = 0;
void * flint_stack_alloc_small(unsigned long length)
{
if (length + 1L > block_left) // not enough space left, allocate a new block
{
if (length + 3L > FLINT_SMALL_BLOCK_SIZE)
{
printf("Error: attempt to allocate %ld limbs in small stack memory manager!\n", length);
abort();
}
if (block_ptr == NULL)
{
block_ptr = (mp_limb_t *) flint_heap_alloc(FLINT_SMALL_BLOCK_SIZE);
block_left = FLINT_SMALL_BLOCK_SIZE - 2;
block_ptr[0] = 0;
block_ptr[1] = (unsigned long) NULL;
block_ptr += 2;
} else
{
mp_limb_t * temp = (mp_limb_t *) flint_heap_alloc(FLINT_SMALL_BLOCK_SIZE);
temp[0] = block_left;
temp[1] = (unsigned long) block_ptr;
block_ptr = temp + 2;
block_left = FLINT_SMALL_BLOCK_SIZE - 2;
}
}
block_ptr[length] = length;
block_ptr += (length+1L);
block_left -= (length+1L);
return (void *) (block_ptr - (length + 1L));
}
void flint_stack_release_small(void)
{
if (block_left == FLINT_SMALL_BLOCK_SIZE - 2)
{
block_ptr -= 2;
block_left = block_ptr[0];
mp_limb_t * temp = block_ptr;
block_ptr = (mp_limb_t *) block_ptr[1];
flint_heap_free(temp);
}
block_ptr--;
unsigned long temp = (*block_ptr);
block_left += (temp+1);
block_ptr -= temp;
}
void flint_stack_cleanup()
{
limb_mem_t* curr = head_mpn;
limb_mem_t* temp;
if (curr != NULL)
{
do
{
if (curr->allocated)
{
printf("Warning: FLINT stack memory allocation cleanup detected mismatched allocation/releases\n");
}
free(curr->point);
if (curr==last_mpn) last_mpn = curr->prev;
else curr->next->prev = curr->prev;
if (curr==head_mpn) head_mpn=curr->next;
else (curr->prev->next = curr->next);
temp=curr;
curr = curr->next;
free(temp);
} while (curr != NULL);
free(reservoir_mpn);
}
if (block_ptr != NULL)
{
if (block_left != FLINT_SMALL_BLOCK_SIZE - 2)
{
printf("Warning: FLINT small stack memory allocator detected mismatched alloc/release\n");
while (block_left != FLINT_SMALL_BLOCK_SIZE - 2) flint_stack_release_small();
}
block_ptr -= 2;
flint_heap_free(block_ptr);
}
}
#endif
/*-----------------------------------------------------------------------------------------------*/
void flint_memory_failure(void)
{
printf("Error: unable to alloc/realloc memory\n");
abort();
}
#if DEBUG
void* flint_heap_alloc(unsigned long limbs)
{
#if DEBUG_PRINT
printf("Allocating %ld limbs on heap\n", limbs);
#endif
unsigned long * buf = malloc((101+limbs) * sizeof(mp_limb_t));
if (!buf)
flint_memory_failure();
buf[0] = limbs;
unsigned long i;
for (i = 0; i < 100; i++)
buf[limbs+i+1] = 0;
return (void*) (buf+1);
}
void* flint_heap_alloc_bytes(unsigned long bytes)
{
unsigned long limbs = ((bytes-1)>>FLINT_LG_BYTES_PER_LIMB)+1;
#if DEBUG_PRINT
printf("Allocating %ld limbs on heap\n", limbs);
#endif
unsigned long * buf = malloc((101+limbs) * sizeof(mp_limb_t));
if (!buf)
flint_memory_failure();
buf[0] = limbs;
unsigned long i;
for (i = 0; i < 100; i++)
buf[limbs+i+1] = 0;
return (void*) (buf+1);
}
void* flint_heap_realloc(void * block_void, unsigned long limbs)
{
unsigned long * block = (unsigned long *) block_void;
block--;
unsigned long length = block[0];
unsigned long i;
for (i = 0; i < 100; i++)
if (block[length+i+1] != 0L)
{
printf("Error: Block overrun detected by heap memory (re)allocator!!\n");
abort();
}
#if DEBUG_PRINT
printf("Reallocing from %ld to %ld limbs on heap\n", length, limbs);
#endif
unsigned long* buf = realloc(block, (limbs+101) * sizeof(mp_limb_t));
if (!buf)
flint_memory_failure();
buf[0] = limbs;
unsigned long i;
for (i = 0; i < 100; i++)
buf[limbs+i+1] = 0;
return (void*) (buf+1);
}
void* flint_heap_realloc_bytes(void * block_void, unsigned long bytes)
{
unsigned long * block = (unsigned long *) block_void;
unsigned long limbs = ((bytes-1)>>FLINT_LG_BYTES_PER_LIMB)+1;
#if DEBUG_PRINT
printf("Reallocing to %ld limbs on heap\n", limbs);
#endif
unsigned long* buf = realloc(block-1, (limbs+101) * sizeof(mp_limb_t));
if (!buf)
flint_memory_failure();
buf[0] = limbs;
unsigned long i;
for (i = 0; i < 100; i++)
buf[limbs+i+1] = 0;
return (void*) (buf+1);
}
void flint_heap_free(void * block_void)
{
unsigned long * block = (unsigned long *) block_void;
block--;
unsigned long length = block[0];
#if DEBUG_PRINT
printf("Releasing %ld limbs from heap\n", length);
#endif
unsigned long i;
for (i = 0; i < 100; i++)
if (block[length+i+1] != 0L)
{
printf("Error: Block overrun detected by heap memory allocator!!\n");
abort();
}
free(block);
}
#else
void* flint_heap_alloc(unsigned long limbs)
{
void* buf = malloc(limbs * sizeof(mp_limb_t));
if (!buf)
flint_memory_failure();
return buf;
}
void* flint_heap_alloc_bytes(unsigned long bytes)
{
void* buf = malloc(bytes);
if (!buf)
flint_memory_failure();
return buf;
}
void* flint_heap_realloc(void* block, unsigned long limbs)
{
void* buf = realloc(block, limbs * sizeof(mp_limb_t));
if (!buf)
flint_memory_failure();
return buf;
}
void* flint_heap_realloc_bytes(void* block, unsigned long bytes)
{
void* buf = realloc(block, bytes);
if (!buf)
flint_memory_failure();
return buf;
}
void flint_heap_free(void* block)
{
free(block);
}
#endif