- Main Page
- Data Structures
- Files

- File List

# E:/DevIL/src-ILU/src/ilu_alloc.c

    00001 #include <stdio.h>
    00002
    00003
    00004 #if defined(_WIN32) && defined(_MEM_DEBUG)
    00005         #include <windows.h>
    00006
    00007         int bAtexit = 0;
    00008
    00009
    00010         typedef struct ALLOC_INFO
    00011         {
    00012                 unsigned long   address;
    00013                 unsigned long   size;
    00014                 char                    file[64];
    00015                 unsigned long   line;
    00016                 struct ALLOC_INFO *Next;
    00017         } ALLOC_INFO;
    00018         ALLOC_INFO *AllocList;
    00019
    00020
    00021         void AddTrack(unsigned long addr, unsigned long size, const char *file, unsigned long line)
    00022         {
    00023                 ALLOC_INFO *Temp;
    00024
    00025                 if (AllocList == NULL) {
    00026                         AllocList = (ALLOC_INFO*)malloc(sizeof(ALLOC_INFO));  // Just assume it succeeds.
    00027                         AllocList->address = addr;
    00028                         AllocList->size = size;
    00029                         AllocList->line = line;
    00030                         strncpy(AllocList->file, file, 63);
    00031                         AllocList->Next = NULL;
    00032                 }
    00033                 else {
    00034                         Temp = AllocList;
    00035                         AllocList = (ALLOC_INFO*)malloc(sizeof(ALLOC_INFO));  // Just assume it succeeds.
    00036                         AllocList->address = addr;
    00037                         AllocList->size = size;
    00038                         AllocList->line = line;
    00039                         strncpy(AllocList->file, file, 63);
    00040                         AllocList->Next = Temp;
    00041                 }
    00042
    00043                 return;
    00044         }
    00045
    00046
    00047         void RemoveTrack(unsigned long addr)
    00048         {
    00049                 ALLOC_INFO *Temp, *Prev;
    00050
    00051                 Temp = AllocList;
    00052                 Prev = NULL;
    00053
    00054                 if (Temp == NULL)
    00055                         return;
    00056
    00057                 while (Temp != NULL) {
    00058                         if (Temp->address == addr) {
    00059                                 if (Prev == NULL) {
    00060                                         AllocList = Temp->Next;
    00061                                         free(Temp);
    00062                                 }
    00063                                 else {
    00064                                         Prev->Next = Temp->Next;
    00065                                         free(Temp);
    00066                                 }
    00067                                 break;
    00068                         }
    00069                         Prev = Temp;
    00070                         Temp = Temp->Next;
    00071                 }
    00072
    00073                 return;
    00074         }
    00075
    00076
    00077         void DumpUnfreed(void)
    00078         {
    00079                 unsigned long TotalSize = 0;
    00080                 char buf[1024];
    00081                 ALLOC_INFO *i = AllocList;
    00082
    00083                 OutputDebugString("ILU Unfreed Information:\n");
    00084                 while (i != NULL) {
    00085                         sprintf(buf, "%s(%d) : %d bytes unfreed at %d\n", i->file, i->line, i->size, i->address);
    00086                         OutputDebugString(buf);
    00087                         TotalSize += i->size;
    00088
    00089                         AllocList = i->Next;
    00090                         free(i);
    00091                         i = AllocList;
    00092                 }
    00093
    00094                 sprintf(buf, "-----------------------------------------------------------\n");
    00095                 OutputDebugString(buf);
    00096                 sprintf(buf, "Total Unfreed: %d bytes\n\n\n", TotalSize);
    00097                 OutputDebugString(buf);
    00098         }
    00099
    00100         void AddToAtexit()
    00101         {
    00102                 if (bAtexit)
    00103                         return;
    00104                 atexit(DumpUnfreed);
    00105                 bAtexit = 1;
    00106         }
    00107
    00108         void *c_alloc(unsigned long size, unsigned long num, const char *file, unsigned long line)
    00109         {
    00110                 ILvoid *ptr;
    00111                 ptr = calloc(size, num);
    00112                 if (!ptr)
    00113                         return NULL;
    00114                 AddToAtexit();
    00115                 AddTrack((unsigned long)ptr, size * num, file, line);
    00116                 return ptr;
    00117         }
    00118
    00119
    00120         void *m_alloc(unsigned long size, const char *file, unsigned long line)
    00121         {
    00122                 ILvoid *ptr;
    00123                 ptr = malloc(size);
    00124                 if (!ptr)
    00125                         return NULL;
    00126                 AddToAtexit();
    00127                 AddTrack((unsigned long)ptr, size, file, line);
    00128                 return ptr;
    00129         }
    00130
    00131
    00132         void f_ree(void *ptr)
    00133         {
    00134                 RemoveTrack((unsigned long)ptr);
    00135                 free(ptr);
    00136                 return;
    00137         }
    00138
    00139 #endif//defined(_WIN32) && defined(_MEM_DEBUG)