#include #include #include #include #include #include #include "Tools.h" extern void Quit(void); struct MemNode { struct MemNode *child; /* Next block in list */ char *file; /* File that allocated us */ ULONG line; /* Line we were allocated on */ ULONG size; /* Size of memory block */ }; static struct MemNode *head = NULL; /****************************************************************************************************************/ /**** ****/ /** DisplayMsg **/ /**** ****/ /****************************************************************************************************************/ void DisplayMsg(char *Msg) { struct EasyStruct RequestMsg = { sizeof(struct EasyStruct), 0, "GenCode C Error", "%s", "OK" }; EasyRequest(NULL,&RequestMsg,NULL,Msg); } /****************************************************************************************************************/ /**** ****/ /** safe_AllocMemory **/ /**** ****/ /****************************************************************************************************************/ void *safe_AllocMemory(ULONG byteSize,char *File,ULONG Line) { static char msg[80]; struct MemNode *t; if (head) t = head; else t = NULL; if (!(head = AllocVec(byteSize + sizeof(struct MemNode),MEMF_PUBLIC|MEMF_CLEAR))) { sprintf(msg,"ERROR !!!!! NOT ENOUGH MEMORY !!!\nCan't allocate %ld bytes\n",byteSize); DisplayMsg(msg); return NULL; } head->child = t; head->file = File; head->line = Line; head->size = byteSize; return ((void *)((ULONG)head + sizeof(struct MemNode))); } /****************************************************************************************************************/ /**** ****/ /** safe_FreeMemory **/ /**** ****/ /****************************************************************************************************************/ void safe_FreeMemory(void *ptr,char* File,ULONG Line) { static char msg[81]; struct MemNode *t1,*t2; if (ptr == NULL) return; t1 = head; t2 = NULL; while(t1) { if((ULONG)ptr == (ULONG)t1 + sizeof(struct MemNode)) { if(t2) t2->child = t1->child; /* Remove block from the list */ else head = t1->child; FreeVec(t1); return; } t2 = t1; t1 = t1->child; } sprintf(msg,"%s %ld: Free twice (?) FreeVec($%lx)",File,Line,(ULONG)ptr - sizeof(struct MemNode)); DisplayMsg(msg); safe_ShowMemory(File,Line); } /****************************************************************************************************************/ /**** ****/ /** safe_ShowMemory **/ /**** ****/ /****************************************************************************************************************/ void safe_ShowMemory(char* File,ULONG Line) { struct MemNode *t; BPTR con; if(head) { if (!(con = Open("CON:0/10/400/100/ShowMemory .../AUTO/CLOSE/WAIT/INACTIVE",MODE_NEWFILE))) return; t = head; FPrintf(con, "%s %ld: Memory List\n", File, Line); FPrintf(con," %-12s %-12s %-16s %-4s\n","Address", "Size", "File", "Line"); FPrintf(con," %-12s %-12s %-16s %-4s\n","-------", "----", "----", "----"); while(t) { FPrintf(con," $%-11lx %-12ld %-16s %-4ld\n",t,t->size,t->file,t->line); t = t->child; } } } /****************************************************************************************************************/ /**** ****/ /** safe_ClearMemory **/ /**** ****/ /****************************************************************************************************************/ void safe_ClearMemory(char* File,ULONG Line) { static char msg[210]; struct MemNode *t1, *t2; if(head) { t1 = head; sprintf(msg, "Oups !! there is a BUG (a memory allocation problem)\nPlease report this bug to GenCode'sAuthors\nwith the next parameters (in ShowMemory Window)\n%s %ld: Freeing Memory List", File, Line); DisplayMsg(msg); safe_ShowMemory(File,Line); while(t1) { t2 = t1->child; FreeVec(t1); t1 = t2; } } head = NULL; } /****************************************************************************************************************/ /**** ****/ /** CopyBlock **/ /**** ****/ /****************************************************************************************************************/ char *CopyBlock(FILE *file,char *Filechar,char *String, char *begin,char *end, char *MsgErrorBegin,char *MsgErrorEnd, char *MainFile) { char ctmp; char *str,*str1; if (!(str = strstr(String,begin))) { char *msg; if (!(msg = (char *)AllocMemory(80+strlen(MsgErrorBegin)+strlen(MainFile)))) { fclose(file); FreeMemory(Filechar); Quit(); } sprintf(msg,"Can't find the next line in old file %s !!! :\n \"%s\"",MainFile,MsgErrorBegin); DisplayMsg(msg); FreeMemory(msg); fclose(file); FreeMemory(Filechar); Quit(); } if (end) { if (!(str1 = strstr(str,end))) { char *msg; if (!(msg = (char *)AllocMemory(80+strlen(MsgErrorEnd)+strlen(MainFile)))) { fclose(file); FreeMemory(Filechar); Quit(); } sprintf(msg,"Can't find the next line in old file %s !!! :\n \"%s\"",MainFile,MsgErrorEnd); DisplayMsg(msg); FreeMemory(msg); fclose(file); FreeMemory(Filechar); Quit(); } ctmp=*str1; *str1='\0'; fprintf(file,"%s",str+strlen(begin)); *str1=ctmp; return str1; } else { fprintf(file,"%s",str+strlen(begin)); return NULL; } } /****************************************************************************************************************/ /**** ****/ /** Indent **/ /**** ****/ /****************************************************************************************************************/ void Indent(FILE *file,int nb) { int i; for(i=0;i