- Main Page
- Data Structures
- Files

- File List

# E:/DevIL/src-IL/src/il_alloc.c

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Sources
    00004 // Last modified: 08/17/2008
    00005 //
    00006 // Filename: src-IL/src/il_alloc.c
    00007 //
    00008 // Description: Memory allocation functions
    00009 //
    00010 //-----------------------------------------------------------------------------
    00011
    00012
    00013 #define __ALLOC_C
    00014
    00015 // Memory leak detection
    00016 #ifdef _WIN32
    00017         #ifdef _DEBUG
    00018                 #define _CRTDBG_MAP_ALLOC
    00019                 #include <stdlib.h>
    00020                 #ifndef _WIN32_WCE  // Does not have this header.
    00021                         #include <crtdbg.h>
    00022                 #endif
    00023         #endif
    00024 #endif//_WIN32
    00025
    00026 #include "il_internal.h"
    00027 #include <stdlib.h>
    00028 #include <math.h>
    00029
    00030 #ifdef MM_MALLOC
    00031 #include <mm_malloc.h>
    00032 #endif
    00033
    00034 static void ILAPIENTRY DefaultFreeFunc(const void * CONST_RESTRICT Ptr);
    00035 static void* ILAPIENTRY DefaultAllocFunc(const ILsizei Size);
    00036
    00037 static mAlloc ialloc_ptr = DefaultAllocFunc;
    00038 static mFree  ifree_ptr = DefaultFreeFunc;
    00039
    00040 /*** Vector Allocation/Deallocation Function ***/
    00041 #ifdef VECTORMEM
    00042 void *vec_malloc(const ILsizei size)
    00043 {
    00044         const ILsizei _size =  size % 16 > 0 ? size + 16 - (size % 16) : size; // align size value
    00045
    00046 #ifdef MM_MALLOC
    00047         return _mm_malloc(_size,16);
    00048 #else
    00049 #ifdef VALLOC
    00050         return valloc( _size );
    00051 #else
    00052 #ifdef POSIX_MEMALIGN
    00053         char *buffer;
    00054         return posix_memalign((void**)&buffer, 16, _size) == 0 ? buffer : NULL;
    00055 #else
    00056 #ifdef MEMALIGN
    00057         return memalign( 16, _size );
    00058 #else
    00059         // Memalign hack from ffmpeg for MinGW
    00060         void *ptr;
    00061         int diff;
    00062         ptr = malloc(_size+16+1);
    00063         diff= ((-(int)ptr - 1)&15) + 1;
    00064         ptr = (void*)(((char*)ptr)+diff);
    00065         ((char*)ptr)[-1]= diff;
    00066         return ptr;
    00067 #endif //MEMALIGN
    00068 #endif //POSIX_MEMALIGN
    00069 #endif //VALLOC
    00070 #endif //MM_MALLOC
    00071 }
    00072
    00073 void *ivec_align_buffer(void *buffer, const ILsizei size)
    00074 {
    00075         if( (ILsizei)buffer % 16 != 0 ) {
    00076         void *aligned_buffer = vec_malloc( size );
    00077         memcpy( aligned_buffer, buffer, size );
    00078         ifree( buffer );
    00079         return aligned_buffer;
    00080     }
    00081     return buffer;
    00082 }
    00083 #endif
    00084
    00085
    00086 /*** Allocation/Deallocation Function ***/
    00087 void* ILAPIENTRY ialloc(const ILsizei Size)
    00088 {
    00089         void *Ptr = ialloc_ptr(Size);
    00090         if (Ptr == NULL)
    00091                 ilSetError(IL_OUT_OF_MEMORY);
    00092         return Ptr;
    00093 }
    00094
    00095 void ILAPIENTRY ifree(const void * CONST_RESTRICT Ptr)
    00096 {
    00097         ifree_ptr(Ptr);
    00098         return;
    00099 }
    00100
    00101 void* ILAPIENTRY icalloc(const ILsizei Size, const ILsizei Num)
    00102 {
    00103     void *Ptr = ialloc(Size * Num);
    00104     if (Ptr == NULL)
    00105         return Ptr;
    00106     imemclear(Ptr, Size * Num);
    00107     return Ptr;
    00108 }
    00109
    00110 /*** Default Allocation/Deallocation Function ***/
    00111 static void* ILAPIENTRY DefaultAllocFunc(const ILsizei Size)
    00112 {
    00113 #ifdef VECTORMEM
    00114         return (void*)vec_malloc(Size);
    00115 #else
    00116         return malloc(Size);
    00117 #endif //VECTORMEM
    00118 }
    00119
    00120 static void ILAPIENTRY DefaultFreeFunc(const void * CONST_RESTRICT ptr)
    00121 {
    00122         if (ptr)
    00123         {
    00124 #ifdef MM_MALLOC
    00125             _mm_free((void*)ptr);
    00126 #else
    00127 #if defined(VECTORMEM) & !defined(POSIX_MEMALIGN) & !defined(VALLOC) & !defined(MEMALIGN) & !defined(MM_MALLOC)
    00128             free(((char*)Ptr) - ((char*)Ptr)[-1]);
    00129 #else
    00130             free((void*)ptr);
    00131 #endif //OTHERS...
    00132 #endif //MM_MALLOC
    00133         }
    00134 }
    00135
    00136 /*** Manipulate Allocation/Deallocation Function ***/
    00137 void ILAPIENTRY ilResetMemory()  // Deprecated
    00138 {
    00139         ialloc_ptr = DefaultAllocFunc;
    00140         ifree_ptr = DefaultFreeFunc;
    00141 }
    00142
    00143 void ILAPIENTRY ilSetMemory(mAlloc AllocFunc, mFree FreeFunc)
    00144 {
    00145         ialloc_ptr = AllocFunc == NULL ? DefaultAllocFunc : AllocFunc;
    00146         ifree_ptr = FreeFunc == NULL ? DefaultFreeFunc : FreeFunc;
    00147 }
    00148
    00149
    00150 /*#if defined(_WIN32) && defined(_MEM_DEBUG)
    00151 #include <windows.h>
    00152
    00153 int bAtexit = 0;
    00154
    00155
    00156 typedef struct ALLOC_INFO
    00157 {
    00158         unsigned long   address;
    00159         unsigned long   size;
    00160         char                    file[64];
    00161         unsigned long   line;
    00162         struct ALLOC_INFO *Next;
    00163 } ALLOC_INFO;
    00164 ALLOC_INFO *AllocList;
    00165
    00166
    00167 void AddTrack(unsigned long addr, unsigned long size, const char *file, unsigned long line)
    00168 {
    00169         ALLOC_INFO *Temp;
    00170
    00171         if (AllocList == NULL) {
    00172                 AllocList = (ALLOC_INFO*)malloc(sizeof(ALLOC_INFO));  // Just assume it succeeds.
    00173                 AllocList->address = addr;
    00174                 AllocList->size = size;
    00175                 AllocList->line = line;
    00176                 strncpy(AllocList->file, file, 63);
    00177                 AllocList->Next = NULL;
    00178         }
    00179         else {
    00180                 Temp = AllocList;
    00181                 AllocList = (ALLOC_INFO*)malloc(sizeof(ALLOC_INFO));  // Just assume it succeeds.
    00182                 AllocList->address = addr;
    00183                 AllocList->size = size;
    00184                 AllocList->line = line;
    00185                 strncpy(AllocList->file, file, 63);
    00186                 AllocList->Next = Temp;
    00187         }
    00188
    00189         return;
    00190 }
    00191
    00192
    00193 void RemoveTrack(unsigned long addr)
    00194 {
    00195         ALLOC_INFO *Temp, *Prev;
    00196
    00197         Temp = AllocList;
    00198         Prev = NULL;
    00199
    00200         if (Temp == NULL)
    00201                 return;
    00202
    00203         while (Temp != NULL) {
    00204                 if (Temp->address == addr) {
    00205                         if (Prev == NULL) {
    00206                                 AllocList = Temp->Next;
    00207                                 free(Temp);
    00208                         }
    00209                         else {
    00210                                 Prev->Next = Temp->Next;
    00211                                 free(Temp);
    00212                         }
    00213                         break;
    00214                 }
    00215                 Prev = Temp;
    00216                 Temp = Temp->Next;
    00217         }
    00218
    00219         return;
    00220 }
    00221
    00222
    00223 void DumpUnfreed(void)
    00224 {
    00225         unsigned long TotalSize = 0;
    00226         char buf[1024];
    00227         ALLOC_INFO *i = AllocList;
    00228
    00229         OutputDebugString("DevIL Unfreed Information:\n");
    00230         while (i != NULL) {
    00231                 sprintf(buf, "%s(%d) : %d bytes unfreed at %d\n", i->file, i->line, i->size, i->address);
    00232                 OutputDebugString(buf);
    00233                 TotalSize += i->size;
    00234
    00235                 AllocList = i->Next;
    00236                 free(i);
    00237                 i = AllocList;
    00238         }
    00239
    00240         sprintf(buf, "-----------------------------------------------------------\n");
    00241         OutputDebugString(buf);
    00242         sprintf(buf, "Total Unfreed: %d bytes\n\n\n", TotalSize);
    00243         OutputDebugString(buf);
    00244 }
    00245
    00246 void AddToAtexit()
    00247 {
    00248         if (bAtexit)
    00249                 return;
    00250         atexit(DumpUnfreed);
    00251         bAtexit = 1;
    00252 }
    00253
    00254 void *c_alloc(unsigned long size, unsigned long num, const char *file, unsigned long line)
    00255 {
    00256         void *ptr;
    00257         ptr = calloc(size, num);
    00258         if (!ptr)
    00259                 return NULL;
    00260         AddToAtexit();
    00261         AddTrack((unsigned long)ptr, size * num, file, line);
    00262         return ptr;
    00263 }
    00264
    00265
    00266 void *m_alloc(unsigned long size, const char *file, unsigned long line)
    00267 {
    00268         void *ptr;
    00269         ptr = malloc(size);
    00270         if (!ptr)
    00271                 return NULL;
    00272         AddToAtexit();
    00273         AddTrack((unsigned long)ptr, size, file, line);
    00274         return ptr;
    00275 }
    00276
    00277
    00278 void f_ree(void *ptr)
    00279 {
    00280         RemoveTrack((unsigned long)ptr);
    00281         free(ptr);
    00282         return;
    00283 }
    00284
    00285 #endif//defined(_WIN32) && defined(_MEM_DEBUG)*/