Blender V2.61 - r43446
|
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 static inline void *btAlignedAllocDefault(size_t size, int alignment) 00062 { 00063 void *ret; 00064 char *real; 00065 unsigned long offset; 00066 00067 real = (char *)sAllocFunc(size + sizeof(void *) + (alignment-1)); 00068 if (real) { 00069 offset = (alignment - (unsigned long)(real + sizeof(void *))) & (alignment-1); 00070 ret = (void *)((real + sizeof(void *)) + offset); 00071 *((void **)(ret)-1) = (void *)(real); 00072 } else { 00073 ret = (void *)(real); 00074 } 00075 return (ret); 00076 } 00077 00078 static inline void btAlignedFreeDefault(void *ptr) 00079 { 00080 void* real; 00081 00082 if (ptr) { 00083 real = *((void **)(ptr)-1); 00084 sFreeFunc(real); 00085 } 00086 } 00087 #endif 00088 00089 00090 static btAlignedAllocFunc *sAlignedAllocFunc = btAlignedAllocDefault; 00091 static btAlignedFreeFunc *sAlignedFreeFunc = btAlignedFreeDefault; 00092 00093 void btAlignedAllocSetCustomAligned(btAlignedAllocFunc *allocFunc, btAlignedFreeFunc *freeFunc) 00094 { 00095 sAlignedAllocFunc = allocFunc ? allocFunc : btAlignedAllocDefault; 00096 sAlignedFreeFunc = freeFunc ? freeFunc : btAlignedFreeDefault; 00097 } 00098 00099 void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc) 00100 { 00101 sAllocFunc = allocFunc ? allocFunc : btAllocDefault; 00102 sFreeFunc = freeFunc ? freeFunc : btFreeDefault; 00103 } 00104 00105 #ifdef BT_DEBUG_MEMORY_ALLOCATIONS 00106 //this generic allocator provides the total allocated number of bytes 00107 #include <stdio.h> 00108 00109 void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filename) 00110 { 00111 void *ret; 00112 char *real; 00113 unsigned long offset; 00114 00115 gTotalBytesAlignedAllocs += size; 00116 gNumAlignedAllocs++; 00117 00118 00119 real = (char *)sAllocFunc(size + 2*sizeof(void *) + (alignment-1)); 00120 if (real) { 00121 offset = (alignment - (unsigned long)(real + 2*sizeof(void *))) & 00122 (alignment-1); 00123 ret = (void *)((real + 2*sizeof(void *)) + offset); 00124 *((void **)(ret)-1) = (void *)(real); 00125 *((int*)(ret)-2) = size; 00126 00127 } else { 00128 ret = (void *)(real);//?? 00129 } 00130 00131 printf("allocation#%d at address %x, from %s,line %d, size %d\n",gNumAlignedAllocs,real, filename,line,size); 00132 00133 int* ptr = (int*)ret; 00134 *ptr = 12; 00135 return (ret); 00136 } 00137 00138 void btAlignedFreeInternal (void* ptr,int line,char* filename) 00139 { 00140 00141 void* real; 00142 gNumAlignedFree++; 00143 00144 if (ptr) { 00145 real = *((void **)(ptr)-1); 00146 int size = *((int*)(ptr)-2); 00147 gTotalBytesAlignedAllocs -= size; 00148 00149 printf("free #%d at address %x, from %s,line %d, size %d\n",gNumAlignedFree,real, filename,line,size); 00150 00151 sFreeFunc(real); 00152 } else 00153 { 00154 printf("NULL ptr\n"); 00155 } 00156 } 00157 00158 #else //BT_DEBUG_MEMORY_ALLOCATIONS 00159 00160 void* btAlignedAllocInternal (size_t size, int alignment) 00161 { 00162 gNumAlignedAllocs++; 00163 void* ptr; 00164 ptr = sAlignedAllocFunc(size, alignment); 00165 // printf("btAlignedAllocInternal %d, %x\n",size,ptr); 00166 return ptr; 00167 } 00168 00169 void btAlignedFreeInternal (void* ptr) 00170 { 00171 if (!ptr) 00172 { 00173 return; 00174 } 00175 00176 gNumAlignedFree++; 00177 // printf("btAlignedFreeInternal %x\n",ptr); 00178 sAlignedFreeFunc(ptr); 00179 } 00180 00181 #endif //BT_DEBUG_MEMORY_ALLOCATIONS 00182