Back to index

supertuxkart  0.5+dfsg1
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 #ifdef BT_DEBUG_MEMORY_ALLOCATIONS
00023 //this generic allocator provides the total allocated number of bytes
00024 #include <stdio.h>
00025 
00026 void*   btAlignedAllocInternal  (size_t size, int alignment,int line,char* filename)
00027 {
00028  void *ret;
00029  char *real;
00030  unsigned long offset;
00031 
00032  gTotalBytesAlignedAllocs += size;
00033  gNumAlignedAllocs++;
00034 
00035  
00036  real = (char *)malloc(size + 2*sizeof(void *) + (alignment-1));
00037  if (real) {
00038    offset = (alignment - (unsigned long)(real + 2*sizeof(void *))) &
00039 (alignment-1);
00040    ret = (void *)((real + 2*sizeof(void *)) + offset);
00041    *((void **)(ret)-1) = (void *)(real);
00042        *((int*)(ret)-2) = size;
00043 
00044  } else {
00045    ret = (void *)(real);//??
00046  }
00047 
00048  printf("allocation#%d at address %x, from %s,line %d, size %d\n",gNumAlignedAllocs,real, filename,line,size);
00049 
00050  int* ptr = (int*)ret;
00051  *ptr = 12;
00052  return (ret);
00053 }
00054 #include <stdio.h>
00055 void    btAlignedFreeInternal   (void* ptr,int line,char* filename)
00056 {
00057 
00058  void* real;
00059  gNumAlignedFree++;
00060 
00061  if (ptr) {
00062    real = *((void **)(ptr)-1);
00063        int size = *((int*)(ptr)-2);
00064        gTotalBytesAlignedAllocs -= size;
00065 
00066           printf("free #%d at address %x, from %s,line %d, size %d\n",gNumAlignedFree,real, filename,line,size);
00067 
00068    free(real);
00069  } else
00070  {
00071         printf("NULL ptr\n");
00072  }
00073 }
00074 
00075 #else //BT_DEBUG_MEMORY_ALLOCATIONS
00076 
00077 
00078 #if defined (BT_HAS_ALIGNED_ALLOCATOR)
00079 
00080 
00081 
00082 
00083 
00084 #include <malloc.h>
00085 void*  btAlignedAllocInternal      (size_t size, int alignment)
00086 {
00087        gNumAlignedAllocs++;
00088 
00089        void* ptr = _aligned_malloc(size,alignment);
00090 //     printf("btAlignedAllocInternal %d, %x\n",size,ptr);
00091        return ptr;
00092 }
00093 
00094 void   btAlignedFreeInternal       (void* ptr)
00095 {
00096        gNumAlignedFree++;
00097 //     printf("btAlignedFreeInternal %x\n",ptr);
00098        _aligned_free(ptr);
00099 }
00100 
00101 #else
00102 
00103 #ifdef __CELLOS_LV2__
00104 
00105 #include <stdlib.h>
00106 
00107 
00108 
00109 void*  btAlignedAllocInternal      (size_t size, int alignment)
00110 {
00111        gNumAlignedAllocs++;
00112        return memalign(alignment, size);
00113 }
00114 
00115 void   btAlignedFreeInternal       (void* ptr)
00116 {
00117        gNumAlignedFree++;
00118        free(ptr);
00119 }
00120 
00121 #else
00122 
00123 void*  btAlignedAllocInternal      (size_t size, int alignment)
00124 {
00125  void *ret;
00126   char *real;
00127   unsigned long offset;
00128  
00129   gNumAlignedAllocs++;
00130 
00131   real = (char *)malloc(size + sizeof(void *) + (alignment-1));
00132   if (real) {
00133     offset = (alignment - (unsigned long)(real + sizeof(void *))) & (alignment-1);
00134     ret = (void *)((real + sizeof(void *)) + offset);
00135     *((void **)(ret)-1) = (void *)(real);
00136   } else {
00137     ret = (void *)(real);
00138   }
00139   return (ret);
00140 }
00141 
00142 void   btAlignedFreeInternal       (void* ptr)
00143 {
00144 
00145  void* real;
00146  gNumAlignedFree++;
00147 
00148   if (ptr) {
00149     real = *((void **)(ptr)-1);
00150     free(real);
00151   }
00152 }
00153 #endif //
00154 
00155 #endif
00156 
00157 #endif //BT_DEBUG_MEMORY_ALLOCATIONS
00158