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