btAlignedAllocator.cpp

Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose,
00008 including commercial applications, and to alter it and redistribute it freely,
00009 subject to the following restrictions:
00010 
00011 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 #include "btAlignedAllocator.h"
00017 
00018 int gNumAlignedAllocs = 0;
00019 int gNumAlignedFree = 0;
00020 int gTotalBytesAlignedAllocs = 0;//detect memory leaks
00021 
00022 static void *btAllocDefault(size_t size)
00023 {
00024         return malloc(size);
00025 }
00026 
00027 static void btFreeDefault(void *ptr)
00028 {
00029         free(ptr);
00030 }
00031 
00032 static btAllocFunc *sAllocFunc = btAllocDefault;
00033 static btFreeFunc *sFreeFunc = btFreeDefault;
00034 
00035 
00036 
00037 #if defined (BT_HAS_ALIGNED_ALLOCATOR)
00038 #include <malloc.h>
00039 static void *btAlignedAllocDefault(size_t size, int alignment)
00040 {
00041         return _aligned_malloc(size, (size_t)alignment);
00042 }
00043 
00044 static void btAlignedFreeDefault(void *ptr)
00045 {
00046         _aligned_free(ptr);
00047 }
00048 #elif defined(__CELLOS_LV2__)
00049 #include <stdlib.h>
00050 
00051 static inline void *btAlignedAllocDefault(size_t size, int alignment)
00052 {
00053         return memalign(alignment, size);
00054 }
00055 
00056 static inline void btAlignedFreeDefault(void *ptr)
00057 {
00058         free(ptr);
00059 }
00060 #else
00061 
00062 
00063 
00064 
00065 
00066 static inline void *btAlignedAllocDefault(size_t size, int alignment)
00067 {
00068   void *ret;
00069   char *real;
00070   real = (char *)sAllocFunc(size + sizeof(void *) + (alignment-1));
00071   if (real) {
00072         ret = btAlignPointer(real + sizeof(void *),alignment);
00073     *((void **)(ret)-1) = (void *)(real);
00074   } else {
00075     ret = (void *)(real);
00076   }
00077   return (ret);
00078 }
00079 
00080 static inline void btAlignedFreeDefault(void *ptr)
00081 {
00082   void* real;
00083 
00084   if (ptr) {
00085     real = *((void **)(ptr)-1);
00086     sFreeFunc(real);
00087   }
00088 }
00089 #endif
00090 
00091 
00092 static btAlignedAllocFunc *sAlignedAllocFunc = btAlignedAllocDefault;
00093 static btAlignedFreeFunc *sAlignedFreeFunc = btAlignedFreeDefault;
00094 
00095 void btAlignedAllocSetCustomAligned(btAlignedAllocFunc *allocFunc, btAlignedFreeFunc *freeFunc)
00096 {
00097   sAlignedAllocFunc = allocFunc ? allocFunc : btAlignedAllocDefault;
00098   sAlignedFreeFunc = freeFunc ? freeFunc : btAlignedFreeDefault;
00099 }
00100 
00101 void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc)
00102 {
00103   sAllocFunc = allocFunc ? allocFunc : btAllocDefault;
00104   sFreeFunc = freeFunc ? freeFunc : btFreeDefault;
00105 }
00106 
00107 #ifdef BT_DEBUG_MEMORY_ALLOCATIONS
00108 //this generic allocator provides the total allocated number of bytes
00109 #include <stdio.h>
00110 
00111 void*   btAlignedAllocInternal  (size_t size, int alignment,int line,char* filename)
00112 {
00113  void *ret;
00114  char *real;
00115 
00116  gTotalBytesAlignedAllocs += size;
00117  gNumAlignedAllocs++;
00118 
00119  
00120  real = (char *)sAllocFunc(size + 2*sizeof(void *) + (alignment-1));
00121  if (real) {
00122    ret = (void*) btAlignPointer(real + 2*sizeof(void *), alignment);
00123    *((void **)(ret)-1) = (void *)(real);
00124        *((int*)(ret)-2) = size;
00125 
00126  } else {
00127    ret = (void *)(real);//??
00128  }
00129 
00130  printf("allocation#%d at address %x, from %s,line %d, size %d\n",gNumAlignedAllocs,real, filename,line,size);
00131 
00132  int* ptr = (int*)ret;
00133  *ptr = 12;
00134  return (ret);
00135 }
00136 
00137 void    btAlignedFreeInternal   (void* ptr,int line,char* filename)
00138 {
00139 
00140  void* real;
00141  gNumAlignedFree++;
00142 
00143  if (ptr) {
00144    real = *((void **)(ptr)-1);
00145        int size = *((int*)(ptr)-2);
00146        gTotalBytesAlignedAllocs -= size;
00147 
00148            printf("free #%d at address %x, from %s,line %d, size %d\n",gNumAlignedFree,real, filename,line,size);
00149 
00150    sFreeFunc(real);
00151  } else
00152  {
00153          printf("NULL ptr\n");
00154  }
00155 }
00156 
00157 #else //BT_DEBUG_MEMORY_ALLOCATIONS
00158 
00159 void*   btAlignedAllocInternal  (size_t size, int alignment)
00160 {
00161         gNumAlignedAllocs++;
00162         void* ptr;
00163         ptr = sAlignedAllocFunc(size, alignment);
00164 //      printf("btAlignedAllocInternal %d, %x\n",size,ptr);
00165         return ptr;
00166 }
00167 
00168 void    btAlignedFreeInternal   (void* ptr)
00169 {
00170         if (!ptr)
00171         {
00172                 return;
00173         }
00174 
00175         gNumAlignedFree++;
00176 //      printf("btAlignedFreeInternal %x\n",ptr);
00177         sAlignedFreeFunc(ptr);
00178 }
00179 
00180 #endif //BT_DEBUG_MEMORY_ALLOCATIONS
00181