Bullet Collision Detection & Physics Library
btAlignedAllocator.cpp
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10 
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15 
16 #include "btAlignedAllocator.h"
17 
20 int gTotalBytesAlignedAllocs = 0;//detect memory leaks
21 
22 static void *btAllocDefault(size_t size)
23 {
24  return malloc(size);
25 }
26 
27 static void btFreeDefault(void *ptr)
28 {
29  free(ptr);
30 }
31 
34 
35 
36 
37 #if defined (BT_HAS_ALIGNED_ALLOCATOR)
38 #include <malloc.h>
39 static void *btAlignedAllocDefault(size_t size, int alignment)
40 {
41  return _aligned_malloc(size, (size_t)alignment);
42 }
43 
44 static void btAlignedFreeDefault(void *ptr)
45 {
46  _aligned_free(ptr);
47 }
48 #elif defined(__CELLOS_LV2__)
49 #include <stdlib.h>
50 
51 static inline void *btAlignedAllocDefault(size_t size, int alignment)
52 {
53  return memalign(alignment, size);
54 }
55 
56 static inline void btAlignedFreeDefault(void *ptr)
57 {
58  free(ptr);
59 }
60 #else
61 
62 
63 
64 
65 
66 static inline void *btAlignedAllocDefault(size_t size, int alignment)
67 {
68  void *ret;
69  char *real;
70  real = (char *)sAllocFunc(size + sizeof(void *) + (alignment-1));
71  if (real) {
72  ret = btAlignPointer(real + sizeof(void *),alignment);
73  *((void **)(ret)-1) = (void *)(real);
74  } else {
75  ret = (void *)(real);
76  }
77  return (ret);
78 }
79 
80 static inline void btAlignedFreeDefault(void *ptr)
81 {
82  void* real;
83 
84  if (ptr) {
85  real = *((void **)(ptr)-1);
86  sFreeFunc(real);
87  }
88 }
89 #endif
90 
91 
94 
96 {
97  sAlignedAllocFunc = allocFunc ? allocFunc : btAlignedAllocDefault;
98  sAlignedFreeFunc = freeFunc ? freeFunc : btAlignedFreeDefault;
99 }
100 
101 void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc)
102 {
103  sAllocFunc = allocFunc ? allocFunc : btAllocDefault;
104  sFreeFunc = freeFunc ? freeFunc : btFreeDefault;
105 }
106 
107 #ifdef BT_DEBUG_MEMORY_ALLOCATIONS
108 //this generic allocator provides the total allocated number of bytes
109 #include <stdio.h>
110 
111 void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filename)
112 {
113  void *ret;
114  char *real;
115 
118 
119 
120  real = (char *)sAllocFunc(size + 2*sizeof(void *) + (alignment-1));
121  if (real) {
122  ret = (void*) btAlignPointer(real + 2*sizeof(void *), alignment);
123  *((void **)(ret)-1) = (void *)(real);
124  *((int*)(ret)-2) = size;
125 
126  } else {
127  ret = (void *)(real);//??
128  }
129 
130  printf("allocation#%d at address %x, from %s,line %d, size %d\n",gNumAlignedAllocs,real, filename,line,size);
131 
132  int* ptr = (int*)ret;
133  *ptr = 12;
134  return (ret);
135 }
136 
137 void btAlignedFreeInternal (void* ptr,int line,char* filename)
138 {
139 
140  void* real;
141  gNumAlignedFree++;
142 
143  if (ptr) {
144  real = *((void **)(ptr)-1);
145  int size = *((int*)(ptr)-2);
147 
148  printf("free #%d at address %x, from %s,line %d, size %d\n",gNumAlignedFree,real, filename,line,size);
149 
150  sFreeFunc(real);
151  } else
152  {
153  printf("NULL ptr\n");
154  }
155 }
156 
157 #else //BT_DEBUG_MEMORY_ALLOCATIONS
158 
159 void* btAlignedAllocInternal (size_t size, int alignment)
160 {
162  void* ptr;
163  ptr = sAlignedAllocFunc(size, alignment);
164 // printf("btAlignedAllocInternal %d, %x\n",size,ptr);
165  return ptr;
166 }
167 
168 void btAlignedFreeInternal (void* ptr)
169 {
170  if (!ptr)
171  {
172  return;
173  }
174 
175  gNumAlignedFree++;
176 // printf("btAlignedFreeInternal %x\n",ptr);
177  sAlignedFreeFunc(ptr);
178 }
179 
180 #endif //BT_DEBUG_MEMORY_ALLOCATIONS
181 
static void * btAlignedAllocDefault(size_t size, int alignment)
static btFreeFunc * sFreeFunc
int gNumAlignedFree
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
static btAlignedAllocFunc * sAlignedAllocFunc
int gTotalBytesAlignedAllocs
void( btFreeFunc)(void *memblock)
void *( btAlignedAllocFunc)(size_t size, int alignment)
static btAlignedFreeFunc * sAlignedFreeFunc
void( btAlignedFreeFunc)(void *memblock)
static void btAlignedFreeDefault(void *ptr)
static void btFreeDefault(void *ptr)
void btAlignedAllocSetCustomAligned(btAlignedAllocFunc *allocFunc, btAlignedFreeFunc *freeFunc)
If the developer has already an custom aligned allocator, then btAlignedAllocSetCustomAligned can be ...
int gNumAlignedAllocs
void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc)
The developer can let all Bullet memory allocations go through a custom memory allocator, using btAlignedAllocSetCustom.
void *( btAllocFunc)(size_t size)
void btAlignedFreeInternal(void *ptr)
void * btAlignedAllocInternal(size_t size, int alignment)
we probably replace this with our own aligned memory allocator so we replace _aligned_malloc and _ali...
T * btAlignPointer(T *unalignedPtr, size_t alignment)
align a pointer to the provided alignment, upwards
Definition: btScalar.h:760
static btAllocFunc * sAllocFunc
static void * btAllocDefault(size_t size)