Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

563680 views
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
5
typedef struct {
6
unsigned pt;
7
int noc;
8
} STAT_LIST_TYPE;
9
int SCOUNT = 0, SFLAG = 0;
10
int SHELP = 0;
11
int SMEMORY = 0x10009b50;
12
int PERIOD = 1, PCOUNT = 0, P_ACT = 0;
13
int LIST_SIZE = 0, LIST_USED = 0;
14
unsigned **MALLOC_LIST = NULL;
15
16
int *STAT_LIST_SIZE = NULL;
17
int *STAT_LIST_USED = NULL;
18
STAT_LIST_TYPE **STAT_MALLOC_LIST = NULL;
19
20
#define M_ALLOC_MAGIC -4711
21
22
/*============================================================*\
23
|| ||
24
|| Some tools for finding errors in memory allocation: ||
25
|| ||
26
\*============================================================*/
27
28
/*============================================================*\
29
|| ||
30
|| Bilde eine Liste aller Pointer, die allociert wurden. ||
31
|| Die Liste enthaelt den Pointer selbst und einen Zaehler, ||
32
|| wie oft der Pointer allociert und wieder freigegeben wurde.||
33
|| Aktive Pointer haben einen positiven Zaehlwert, freigege- ||
34
|| bene einen negativen. Mehrfaches Freigeben fuehrt zum ||
35
|| abbruch. ||
36
|| ||
37
\*============================================================*/
38
39
void pointer_statistics(p, status)
40
unsigned *p;
41
int status; /* 1: pointer wurde neu allociert
42
2: pointer wurde freigegeben
43
0: Ausgabe der Liste */
44
{
45
int i,j;
46
int flag;
47
FILE *outfile;
48
STAT_LIST_TYPE *HELP_MALLOC_LIST;
49
unsigned p_cut;
50
51
if(status == 0) { /* Gibt die Liste auf eine Datei aus */
52
outfile = (FILE *)fopen("Malloc_liste", "w");
53
flag = 0;
54
fprintf(outfile,"Liste der allocierten und nicht wieder freigegebenen Pointer:\n");
55
if(STAT_LIST_USED == NULL) {
56
fprintf(outfile, "Keine Pointer mehr aktiv!! \n");
57
fclose(outfile);
58
return;
59
}
60
for(j = 0; j < STAT_LIST_USED[-1]; j++) {
61
HELP_MALLOC_LIST = STAT_MALLOC_LIST[j];
62
for(i = 1; i < STAT_LIST_USED[j]; i++) {
63
if(HELP_MALLOC_LIST[i].noc > 0) {
64
flag = 1;
65
fprintf(outfile, "%x: %d\n", HELP_MALLOC_LIST[i].pt,
66
HELP_MALLOC_LIST[i].noc);
67
}
68
}
69
}
70
if(flag == 0) {
71
fprintf(outfile, "Keine Pointer mehr aktiv!! \n");
72
}
73
fclose(outfile);
74
}
75
76
else if(status == 1) { /* Neuen Pointer einfuegen */
77
if(STAT_MALLOC_LIST == NULL) { /* Liste anlegen */
78
STAT_MALLOC_LIST = (STAT_LIST_TYPE **)malloc(10 *
79
sizeof(STAT_LIST_TYPE *));
80
STAT_LIST_USED = (int *)calloc(11, sizeof(int));
81
STAT_LIST_SIZE = (int *)calloc(11, sizeof(int));
82
STAT_LIST_USED++;
83
STAT_LIST_SIZE++;
84
STAT_LIST_SIZE[-1] = 10;
85
for(i = 0; i < 10; i++) {
86
STAT_MALLOC_LIST[i] = (STAT_LIST_TYPE *)calloc(100,
87
sizeof(STAT_LIST_TYPE));
88
STAT_LIST_SIZE[i] = 100;
89
STAT_LIST_USED[i] = 1;
90
}
91
}
92
p_cut = ((unsigned) p) >> 14; /* Hoffentlich realistische Groesse */
93
i = 0;
94
while((i < STAT_LIST_USED[-1]) &&
95
(p_cut != STAT_MALLOC_LIST[i][0].pt)) i++;
96
if(i < STAT_LIST_USED[-1]){ /* Pointerbereich schon mal allociert */
97
HELP_MALLOC_LIST = STAT_MALLOC_LIST[i];
98
j = 1;
99
while((j < STAT_LIST_USED[i]) &&
100
(p != (unsigned *)HELP_MALLOC_LIST[j].pt)) j++;
101
if(j < STAT_LIST_USED[i]) { /* Pointer schon mal allociert */
102
if(HELP_MALLOC_LIST[j].noc > 0) {
103
fprintf(stderr,"Error in pointer_statistics.\n");
104
}
105
HELP_MALLOC_LIST[j].noc = 1 - HELP_MALLOC_LIST[j].noc;
106
}
107
else {
108
if(STAT_LIST_USED[i] == STAT_LIST_SIZE[i]) {
109
HELP_MALLOC_LIST = (STAT_LIST_TYPE *)realloc(
110
HELP_MALLOC_LIST, (STAT_LIST_SIZE[i]+100)
111
*sizeof(STAT_LIST_TYPE ));
112
STAT_LIST_SIZE[i] += 100;
113
STAT_MALLOC_LIST[i] = HELP_MALLOC_LIST;
114
}
115
HELP_MALLOC_LIST[STAT_LIST_USED[i] ].pt = (unsigned )p;
116
HELP_MALLOC_LIST[STAT_LIST_USED[i]++].noc = 1;
117
}
118
}
119
else {
120
if(STAT_LIST_USED[-1] == STAT_LIST_SIZE[-1]) {
121
STAT_LIST_USED--;
122
STAT_LIST_USED = (int *)realloc(STAT_LIST_USED,
123
(STAT_LIST_SIZE[-1]+11)*sizeof(int));
124
STAT_LIST_USED++;
125
STAT_LIST_SIZE--;
126
STAT_LIST_SIZE = (int *)realloc(STAT_LIST_SIZE,
127
(STAT_LIST_USED[-1]+11)*sizeof(int));
128
STAT_LIST_SIZE++;
129
STAT_LIST_SIZE[-1] += 10;
130
STAT_MALLOC_LIST = (STAT_LIST_TYPE **)realloc(
131
STAT_MALLOC_LIST, (STAT_LIST_SIZE[-1])
132
*sizeof(STAT_LIST_TYPE *));
133
for(j = STAT_LIST_USED[-1]; j < STAT_LIST_SIZE[-1]; j++) {
134
STAT_MALLOC_LIST[j] = (STAT_LIST_TYPE *)calloc
135
(100,sizeof(STAT_LIST_TYPE));
136
STAT_LIST_SIZE[j] = 100;
137
STAT_LIST_USED[j] = 1;
138
}
139
}
140
STAT_MALLOC_LIST[i][0 ].pt = p_cut;
141
STAT_MALLOC_LIST[i][STAT_LIST_USED[i] ].pt = (unsigned)p;
142
STAT_MALLOC_LIST[i][STAT_LIST_USED[i]++].noc = 1;
143
STAT_LIST_USED[-1]++;
144
}
145
}
146
else if(status == 2) {
147
i = 0;
148
p_cut = ((unsigned)p)>> 14;
149
while((i<STAT_LIST_USED[-1])&&(p_cut!=STAT_MALLOC_LIST[i][0].pt))
150
i++;
151
if(i < STAT_LIST_USED[-1]) { /*Pointerbereich schon mal allociert */
152
j = 1;
153
HELP_MALLOC_LIST = STAT_MALLOC_LIST[i];
154
while((j < STAT_LIST_USED[i]) &&
155
(p != (unsigned *)HELP_MALLOC_LIST[j].pt))
156
j++;
157
if(j < STAT_LIST_USED[i]) { /*Pointer schon mal allociert */
158
if(HELP_MALLOC_LIST[j].noc > 0) { /* Alles okay */
159
HELP_MALLOC_LIST[j].noc *= (-1);
160
}
161
else {
162
fprintf(stderr,
163
"Fehler: Doppeltes Freigeben eines Pointers.\n");
164
}
165
}
166
else {
167
fprintf(stderr,
168
"Fehler: Freigeben von nicht allociertem Pointer.\n");
169
}
170
}
171
else {
172
fprintf(stderr,
173
"Fehler: Freigeben eines nicht allocierten Pointers.\n");
174
}
175
}
176
}
177
178
/*============================================================*\
179
|| ||
180
|| First version for allocation-error-diagnostics: ||
181
|| Get 4 more bytes before and after the array and write ||
182
|| certain values in it. ||
183
|| Check the values when freeing it. ||
184
|| ||
185
\*============================================================*/
186
int *m_alloc_d1(int size_t)
187
{
188
int *p;
189
int newsize;
190
191
newsize = (size_t-1) / 4 + 1;
192
193
if( (p = (int*)malloc(4*newsize+32)) == NULL) {
194
fprintf(stderr,"Fehler in malloc \n");
195
exit(2);
196
}
197
if(SFLAG) pointer_statistics(p, 1);
198
p[3] = newsize;
199
p[1] = p[2] = p[0] = M_ALLOC_MAGIC;
200
p += 4;
201
p[newsize] = p[newsize+1] = p[newsize+2] = p[newsize+3] = M_ALLOC_MAGIC;
202
if(p == (int *)SMEMORY) {
203
SHELP++;
204
}
205
SCOUNT++;
206
return(p);
207
}
208
209
int *c_alloc_d1(int size_t, int size_n)
210
{
211
int *p;
212
int newsize;
213
214
newsize = (size_t*size_n -1) / 4 + 1;
215
if( (p = (int*)malloc(4*newsize+32)) == NULL) {
216
fprintf(stderr,"Fehler in calloc \n");
217
exit(2);
218
}
219
if(SFLAG) pointer_statistics(p, 1);
220
p[3] = newsize;
221
p[1] = p[2] = p[0] = M_ALLOC_MAGIC;
222
p += 4;
223
p[newsize] = p[newsize+1] = p[newsize+2] = p[newsize+3] = M_ALLOC_MAGIC;
224
while((--newsize) >= 0) {
225
p[newsize] = 0;
226
}
227
if(p == (int *)SMEMORY) {
228
SHELP++;
229
}
230
SCOUNT++;
231
return(p);
232
}
233
234
int *re_alloc_d1(int *old_p, int size_t)
235
{
236
int *p;
237
int oldsize, sizestore;
238
239
if(old_p == NULL)
240
return(m_alloc_d1(size_t));
241
242
old_p -= 4;
243
if((old_p[1]!=M_ALLOC_MAGIC)||(old_p[2]!=M_ALLOC_MAGIC)||(old_p[0]!=M_ALLOC_MAGIC)) {
244
fprintf(stderr,"Error in re_alloc-control sequence\n");
245
exit(2);
246
}
247
oldsize = old_p[3]+4;
248
if((old_p[oldsize ] != M_ALLOC_MAGIC) || (old_p[oldsize+1] != M_ALLOC_MAGIC) ||
249
(old_p[oldsize+2] != M_ALLOC_MAGIC) || (old_p[oldsize+3] != M_ALLOC_MAGIC)) {
250
fprintf(stderr,"Error in re_alloc-control sequence\n");
251
exit(2);
252
}
253
254
oldsize -= 4;
255
sizestore = (size_t-1) / 4 + 1;
256
if(SFLAG) pointer_statistics(old_p,2);
257
if( (p = (int*)realloc(old_p, sizestore*4 +32)) == NULL) {
258
fprintf(stderr,"Fehler in realloc \n");
259
exit(2);
260
}
261
if(SFLAG) pointer_statistics(p,1);
262
p[3] = sizestore;
263
p[1] = p[2] = p[0] = M_ALLOC_MAGIC;
264
p += 4;
265
if(p == (int *)SMEMORY) {
266
SHELP++;
267
}
268
if(sizestore > oldsize) {
269
memset(p + oldsize, 0, (sizestore-oldsize) * sizeof(int));
270
}
271
p[sizestore] = p[sizestore+1] = p[sizestore+2] = p[sizestore+3] = M_ALLOC_MAGIC;
272
return(p);
273
}
274
275
void fr_ee_d1(p)
276
int *p;
277
{
278
int oldsize;
279
280
if(p == (int *)SMEMORY) {
281
SHELP++;
282
}
283
p -= 4;
284
oldsize = p[3] + 4;
285
if((p[1] != M_ALLOC_MAGIC) || (p[2] != M_ALLOC_MAGIC) || (p[0] != M_ALLOC_MAGIC)) {
286
fprintf(stderr,"Error in fr_ee-control sequence\n");
287
exit(2);
288
}
289
if( (p[oldsize ] != M_ALLOC_MAGIC) || (p[oldsize+1] != M_ALLOC_MAGIC) ||
290
(p[oldsize+2] != M_ALLOC_MAGIC) || (p[oldsize+3] != M_ALLOC_MAGIC)) {
291
fprintf(stderr,"Error in fr_ee-control sequence\n");
292
exit(2);
293
}
294
SCOUNT--;
295
if(SFLAG) pointer_statistics(p, 2);
296
free(p);
297
}
298
299
300
/*============================================================*\
301
|| ||
302
|| Second version for allocation-error-diagnostics: ||
303
|| Get 4 more bytes before and after the array and write ||
304
|| certain values in it. ||
305
|| Create a list of all allocated pointers and check the ||
306
|| values periodically. ||
307
|| ||
308
\*============================================================*/
309
310
void add_pointer(p)
311
unsigned *p;
312
313
{
314
int i, oldsize;
315
316
if(MALLOC_LIST == NULL) {
317
MALLOC_LIST = (unsigned **)calloc(100,sizeof(unsigned *));
318
LIST_SIZE = 100;
319
LIST_USED = 0;
320
}
321
if(LIST_SIZE == LIST_USED) {
322
MALLOC_LIST = (unsigned **)realloc(MALLOC_LIST,
323
(100+LIST_SIZE)* sizeof(unsigned *));
324
memset(MALLOC_LIST+LIST_SIZE,0,100*sizeof(unsigned ));
325
LIST_SIZE += 100;
326
}
327
while(MALLOC_LIST[P_ACT] != NULL) P_ACT = (P_ACT + 1)%LIST_SIZE;
328
MALLOC_LIST[P_ACT] = p;
329
LIST_USED++;
330
PCOUNT ++;
331
if(PCOUNT == PERIOD) {
332
PCOUNT = 0;
333
for(i = 0; i < LIST_SIZE; i++) {
334
if(MALLOC_LIST[i] != NULL) {
335
p = MALLOC_LIST[i] - 4;
336
oldsize = p[3] + 4;
337
if((p[1] != M_ALLOC_MAGIC) || (p[2] != M_ALLOC_MAGIC) || (p[0] != M_ALLOC_MAGIC)) {
338
fprintf(stderr,"Error in alloc-control sequence\n");
339
exit(2);
340
}
341
if( (p[oldsize ] != M_ALLOC_MAGIC) || (p[oldsize+1] != M_ALLOC_MAGIC) ||
342
(p[oldsize+2] != M_ALLOC_MAGIC) || (p[oldsize+3] != M_ALLOC_MAGIC)) {
343
fprintf(stderr,"Error in alloc-control sequence\n");
344
exit(2);
345
}
346
}
347
}
348
}
349
}
350
351
void delete_pointer(p)
352
unsigned *p;
353
354
{
355
int i, oldsize;
356
357
i = 0;
358
while((i < LIST_SIZE) && MALLOC_LIST[i] != p) i++;
359
if(i == LIST_SIZE) {
360
fprintf(stderr,"Error in alloc-sequence:\n");
361
fprintf(stderr,"Free-call on non-allocated pointer\n");
362
exit(2);
363
}
364
MALLOC_LIST[i] = NULL;
365
LIST_USED--;
366
PCOUNT ++;
367
p -= 4;
368
if((p[1]!=M_ALLOC_MAGIC)||(p[2]!=M_ALLOC_MAGIC)||(p[0]!=M_ALLOC_MAGIC)) {
369
fprintf(stderr,"Error in alloc-control sequence\n");
370
exit(2);
371
}
372
oldsize = p[3]+4;
373
if((p[oldsize ] != M_ALLOC_MAGIC) || (p[oldsize+1] != M_ALLOC_MAGIC) ||
374
(p[oldsize+2] != M_ALLOC_MAGIC) || (p[oldsize+3] != M_ALLOC_MAGIC)) {
375
fprintf(stderr,"Error in re_alloc-control sequence\n");
376
exit(2);
377
}
378
if(PCOUNT == PERIOD) {
379
PCOUNT = 0;
380
for(i = 0; i < LIST_SIZE; i++) {
381
if(MALLOC_LIST[i] != NULL) {
382
p = MALLOC_LIST[i] - 4;
383
oldsize = p[3] + 4;
384
if((p[1] != M_ALLOC_MAGIC) || (p[2] != M_ALLOC_MAGIC) || (p[0] != M_ALLOC_MAGIC)) {
385
fprintf(stderr,"Error in alloc-control sequence\n");
386
exit(2);
387
}
388
if( (p[oldsize ] != M_ALLOC_MAGIC) || (p[oldsize+1] != M_ALLOC_MAGIC) ||
389
(p[oldsize+2] != M_ALLOC_MAGIC) || (p[oldsize+3] != M_ALLOC_MAGIC)) {
390
fprintf(stderr,"Error in alloc-control sequence\n");
391
exit(2);
392
}
393
}
394
}
395
}
396
}
397
398
399
int *m_alloc_d2(int size_t)
400
{
401
int *p;
402
int newsize;
403
404
newsize = size_t / 4 + 1;
405
406
if( (p = (int*)malloc(4*newsize+32)) == NULL) {
407
fprintf(stderr,"Fehler in malloc \n");
408
exit(2);
409
}
410
if(SFLAG) pointer_statistics(p, 1);
411
p[3] = newsize;
412
p[1] = p[2] = p[0] = M_ALLOC_MAGIC;
413
p += 4;
414
p[newsize] = p[newsize+1] = p[newsize+2] = p[newsize+3] = M_ALLOC_MAGIC;
415
if(p == (int *)SMEMORY) {
416
SHELP++;
417
}
418
SCOUNT++;
419
420
add_pointer(p);
421
422
return(p);
423
}
424
425
int *c_alloc_d2(int size_t, int size_n)
426
{
427
int *p;
428
int newsize;
429
if( (p = (int*)m_alloc_d2(size_t*size_n)) == NULL) {
430
fprintf(stderr,"Fehler in calloc \n");
431
exit(2);
432
}
433
memset(p,0,size_t*size_n);
434
return(p);
435
}
436
437
int *re_alloc_d2(int *old_p, int size_t)
438
{
439
int *p;
440
int sizestore;
441
442
if(old_p == NULL)
443
return(m_alloc_d2(size_t));
444
445
delete_pointer(old_p);
446
447
old_p -= 4;
448
449
sizestore = size_t / 4 + 1;
450
if(SFLAG) pointer_statistics(old_p, 2);
451
if( (p = (int*)realloc(old_p, sizestore*4 +32)) == NULL) {
452
fprintf(stderr,"Fehler in realloc \n");
453
exit(2);
454
}
455
if(SFLAG) pointer_statistics(p, 1);
456
p[3] = sizestore;
457
p[1] = p[2] = p[0] = M_ALLOC_MAGIC;
458
p += 4;
459
p[sizestore] = p[sizestore+1] = p[sizestore+2] = p[sizestore+3] = M_ALLOC_MAGIC;
460
if(p == (int *)SMEMORY) {
461
SHELP++;
462
}
463
add_pointer(p);
464
465
return(p);
466
}
467
468
void fr_ee_d2(p)
469
int *p;
470
{
471
int oldsize;
472
473
delete_pointer(p);
474
if(p == (int *)SMEMORY) SHELP++;
475
p -= 4;
476
SCOUNT--;
477
if(SFLAG) pointer_statistics(p, 2);
478
free(p);
479
}
480
481
482