Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/bullet/LinearMath/btAlignedAllocator.cpp @ 3979

Last change on this file since 3979 was 2882, checked in by rgrieder, 16 years ago

Update from Bullet 2.73 to 2.74.

  • Property svn:eol-style set to native
File size: 5.2 KB
RevLine 
[1963]1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16#include "btAlignedAllocator.h"
17
18int gNumAlignedAllocs = 0;
19int gNumAlignedFree = 0;
20int gTotalBytesAlignedAllocs = 0;//detect memory leaks
21
[2882]22static void *btAllocDefault(size_t size)
23{
24        return malloc(size);
25}
26
27static void btFreeDefault(void *ptr)
28{
29        free(ptr);
30}
31
32static btAllocFunc *sAllocFunc = btAllocDefault;
33static btFreeFunc *sFreeFunc = btFreeDefault;
34
35
36
[1963]37#if defined (BT_HAS_ALIGNED_ALLOCATOR)
38#include <malloc.h>
39static void *btAlignedAllocDefault(size_t size, int alignment)
40{
41        return _aligned_malloc(size, (size_t)alignment);
42}
43
44static void btAlignedFreeDefault(void *ptr)
45{
46        _aligned_free(ptr);
47}
48#elif defined(__CELLOS_LV2__)
49#include <stdlib.h>
50
51static inline void *btAlignedAllocDefault(size_t size, int alignment)
52{
53        return memalign(alignment, size);
54}
55
56static inline void btAlignedFreeDefault(void *ptr)
57{
58        free(ptr);
59}
60#else
61static inline void *btAlignedAllocDefault(size_t size, int alignment)
62{
63  void *ret;
64  char *real;
65  unsigned long offset;
66
[2882]67  real = (char *)sAllocFunc(size + sizeof(void *) + (alignment-1));
[1963]68  if (real) {
69    offset = (alignment - (unsigned long)(real + sizeof(void *))) & (alignment-1);
70    ret = (void *)((real + sizeof(void *)) + offset);
71    *((void **)(ret)-1) = (void *)(real);
72  } else {
73    ret = (void *)(real);
74  }
75  return (ret);
76}
77
78static inline void btAlignedFreeDefault(void *ptr)
79{
80  void* real;
81
82  if (ptr) {
83    real = *((void **)(ptr)-1);
[2882]84    sFreeFunc(real);
[1963]85  }
86}
87#endif
88
89
90static btAlignedAllocFunc *sAlignedAllocFunc = btAlignedAllocDefault;
91static btAlignedFreeFunc *sAlignedFreeFunc = btAlignedFreeDefault;
92
93void btAlignedAllocSetCustomAligned(btAlignedAllocFunc *allocFunc, btAlignedFreeFunc *freeFunc)
94{
95  sAlignedAllocFunc = allocFunc ? allocFunc : btAlignedAllocDefault;
96  sAlignedFreeFunc = freeFunc ? freeFunc : btAlignedFreeDefault;
97}
98
99void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc)
100{
101  sAllocFunc = allocFunc ? allocFunc : btAllocDefault;
102  sFreeFunc = freeFunc ? freeFunc : btFreeDefault;
103}
104
105#ifdef BT_DEBUG_MEMORY_ALLOCATIONS
106//this generic allocator provides the total allocated number of bytes
107#include <stdio.h>
108
109void*   btAlignedAllocInternal  (size_t size, int alignment,int line,char* filename)
110{
111 void *ret;
112 char *real;
113 unsigned long offset;
114
115 gTotalBytesAlignedAllocs += size;
116 gNumAlignedAllocs++;
117
118 
119 real = (char *)sAllocFunc(size + 2*sizeof(void *) + (alignment-1));
120 if (real) {
121   offset = (alignment - (unsigned long)(real + 2*sizeof(void *))) &
122(alignment-1);
123   ret = (void *)((real + 2*sizeof(void *)) + offset);
124   *((void **)(ret)-1) = (void *)(real);
125       *((int*)(ret)-2) = size;
126
127 } else {
128   ret = (void *)(real);//??
129 }
130
131 printf("allocation#%d at address %x, from %s,line %d, size %d\n",gNumAlignedAllocs,real, filename,line,size);
132
133 int* ptr = (int*)ret;
134 *ptr = 12;
135 return (ret);
136}
137
138void    btAlignedFreeInternal   (void* ptr,int line,char* filename)
139{
140
141 void* real;
142 gNumAlignedFree++;
143
144 if (ptr) {
145   real = *((void **)(ptr)-1);
146       int size = *((int*)(ptr)-2);
147       gTotalBytesAlignedAllocs -= size;
148
149           printf("free #%d at address %x, from %s,line %d, size %d\n",gNumAlignedFree,real, filename,line,size);
150
151   sFreeFunc(real);
152 } else
153 {
154         printf("NULL ptr\n");
155 }
156}
157
158#else //BT_DEBUG_MEMORY_ALLOCATIONS
159
160void*   btAlignedAllocInternal  (size_t size, int alignment)
161{
162        gNumAlignedAllocs++;
163  void* ptr;
164#if defined (BT_HAS_ALIGNED_ALLOCATOR) || defined(__CELLOS_LV2__)
165        ptr = sAlignedAllocFunc(size, alignment);
166#else
167  char *real;
168  unsigned long offset;
169
170  real = (char *)sAllocFunc(size + sizeof(void *) + (alignment-1));
171  if (real) {
172    offset = (alignment - (unsigned long)(real + sizeof(void *))) & (alignment-1);
173    ptr = (void *)((real + sizeof(void *)) + offset);
174    *((void **)(ptr)-1) = (void *)(real);
175  } else {
176    ptr = (void *)(real);
177  }
178#endif  // defined (BT_HAS_ALIGNED_ALLOCATOR) || defined(__CELLOS_LV2__)
179//      printf("btAlignedAllocInternal %d, %x\n",size,ptr);
180        return ptr;
181}
182
183void    btAlignedFreeInternal   (void* ptr)
184{
185        if (!ptr)
186        {
187                return;
188        }
189
190        gNumAlignedFree++;
191//      printf("btAlignedFreeInternal %x\n",ptr);
192#if defined (BT_HAS_ALIGNED_ALLOCATOR) || defined(__CELLOS_LV2__)
193        sAlignedFreeFunc(ptr);
194#else
195  void* real;
196
197  if (ptr) {
198    real = *((void **)(ptr)-1);
199    sFreeFunc(real);
200  }
201#endif  // defined (BT_HAS_ALIGNED_ALLOCATOR) || defined(__CELLOS_LV2__)
202}
203
204#endif //BT_DEBUG_MEMORY_ALLOCATIONS
205
Note: See TracBrowser for help on using the repository browser.