+
+/********** MEM CHEQQER ***********/
+
+#ifdef DEBUG_MEMORY_LEAKS
+
+#undef malloc
+#undef realloc
+#undef strdup
+#undef free
+
+void *tracked_malloc(size_t size, char *file, int line) {
+ struct igheap *thisheap;
+ void *block;
+
+ block = malloc(size);
+ if (block == NULL) return(block);
+
+ thisheap = malloc(sizeof(struct igheap));
+ if (thisheap == NULL) {
+ free(block);
+ return(NULL);
+ }
+
+ thisheap->block = block;
+ strcpy(thisheap->file, file);
+ thisheap->line = line;
+
+ begin_critical_section(S_DEBUGMEMLEAKS);
+ thisheap->next = igheap;
+ igheap = thisheap;
+ end_critical_section(S_DEBUGMEMLEAKS);
+
+ return(block);
+}
+
+
+void *tracked_realloc(void *ptr, size_t size, char *file, int line) {
+ struct igheap *thisheap;
+ void *block;
+
+ block = realloc(ptr, size);
+ if (block == NULL) return(block);
+
+ thisheap = malloc(sizeof(struct igheap));
+ if (thisheap == NULL) {
+ free(block);
+ return(NULL);
+ }
+
+ thisheap->block = block;
+ strcpy(thisheap->file, file);
+ thisheap->line = line;
+
+ begin_critical_section(S_DEBUGMEMLEAKS);
+ thisheap->next = igheap;
+ igheap = thisheap;
+ end_critical_section(S_DEBUGMEMLEAKS);
+
+ return(block);
+}
+
+
+
+void tracked_free(void *ptr) {
+ struct igheap *thisheap;
+ struct igheap *trash;
+
+ free(ptr);
+
+ if (igheap == NULL) return;
+ begin_critical_section(S_DEBUGMEMLEAKS);
+ for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
+ if (thisheap->next != NULL) {
+ if (thisheap->next->block == ptr) {
+ trash = thisheap->next;
+ thisheap->next = thisheap->next->next;
+ free(trash);
+ }
+ }
+ }
+ if (igheap->block == ptr) {
+ trash = igheap;
+ igheap = igheap->next;
+ free(trash);
+ }
+ end_critical_section(S_DEBUGMEMLEAKS);
+}
+
+char *tracked_strdup(const char *s, char *file, int line) {
+ char *ptr;
+
+ if (s == NULL) return(NULL);
+ ptr = tracked_malloc(strlen(s) + 1, file, line);
+ if (ptr == NULL) return(NULL);
+ strncpy(ptr, s, strlen(s));
+ return(ptr);
+}
+
+void dump_heap(void) {
+ struct igheap *thisheap;
+
+ for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
+ lprintf(CTDL_DEBUG, "%30s : %d\n", thisheap->file, thisheap->line);
+ }
+}
+
+#endif /* DEBUG_MEMORY_LEAKS */