Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2830 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

  • Property svn:eol-style set to native
File size: 5.2 KB
Line 
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
22#if defined (BT_HAS_ALIGNED_ALLOCATOR)
23#include <malloc.h>
24static void *btAlignedAllocDefault(size_t size, int alignment)
25{
26        return _aligned_malloc(size, (size_t)alignment);
27}
28
29static void btAlignedFreeDefault(void *ptr)
30{
31        _aligned_free(ptr);
32}
33#elif defined(__CELLOS_LV2__)
34#include <stdlib.h>
35
36static inline void *btAlignedAllocDefault(size_t size, int alignment)
37{
38        return memalign(alignment, size);
39}
40
41static inline void btAlignedFreeDefault(void *ptr)
42{
43        free(ptr);
44}
45#else
46static inline void *btAlignedAllocDefault(size_t size, int alignment)
47{
48  void *ret;
49  char *real;
50  unsigned long offset;
51
52  real = (char *)malloc(size + sizeof(void *) + (alignment-1));
53  if (real) {
54    offset = (alignment - (unsigned long)(real + sizeof(void *))) & (alignment-1);
55    ret = (void *)((real + sizeof(void *)) + offset);
56    *((void **)(ret)-1) = (void *)(real);
57  } else {
58    ret = (void *)(real);
59  }
60  return (ret);
61}
62
63static inline void btAlignedFreeDefault(void *ptr)
64{
65  void* real;
66
67  if (ptr) {
68    real = *((void **)(ptr)-1);
69    free(real);
70  }
71}
72#endif
73
74static void *btAllocDefault(size_t size)
75{
76        return malloc(size);
77}
78
79static void btFreeDefault(void *ptr)
80{
81        free(ptr);
82}
83
84static btAlignedAllocFunc *sAlignedAllocFunc = btAlignedAllocDefault;
85static btAlignedFreeFunc *sAlignedFreeFunc = btAlignedFreeDefault;
86static btAllocFunc *sAllocFunc = btAllocDefault;
87static btFreeFunc *sFreeFunc = btFreeDefault;
88
89void btAlignedAllocSetCustomAligned(btAlignedAllocFunc *allocFunc, btAlignedFreeFunc *freeFunc)
90{
91  sAlignedAllocFunc = allocFunc ? allocFunc : btAlignedAllocDefault;
92  sAlignedFreeFunc = freeFunc ? freeFunc : btAlignedFreeDefault;
93}
94
95void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc)
96{
97  sAllocFunc = allocFunc ? allocFunc : btAllocDefault;
98  sFreeFunc = freeFunc ? freeFunc : btFreeDefault;
99}
100
101#ifdef BT_DEBUG_MEMORY_ALLOCATIONS
102//this generic allocator provides the total allocated number of bytes
103#include <stdio.h>
104
105void*   btAlignedAllocInternal  (size_t size, int alignment,int line,char* filename)
106{
107 void *ret;
108 char *real;
109 unsigned long offset;
110
111 gTotalBytesAlignedAllocs += size;
112 gNumAlignedAllocs++;
113
114 
115 real = (char *)sAllocFunc(size + 2*sizeof(void *) + (alignment-1));
116 if (real) {
117   offset = (alignment - (unsigned long)(real + 2*sizeof(void *))) &
118(alignment-1);
119   ret = (void *)((real + 2*sizeof(void *)) + offset);
120   *((void **)(ret)-1) = (void *)(real);
121       *((int*)(ret)-2) = size;
122
123 } else {
124   ret = (void *)(real);//??
125 }
126
127 printf("allocation#%d at address %x, from %s,line %d, size %d\n",gNumAlignedAllocs,real, filename,line,size);
128
129 int* ptr = (int*)ret;
130 *ptr = 12;
131 return (ret);
132}
133
134void    btAlignedFreeInternal   (void* ptr,int line,char* filename)
135{
136
137 void* real;
138 gNumAlignedFree++;
139
140 if (ptr) {
141   real = *((void **)(ptr)-1);
142       int size = *((int*)(ptr)-2);
143       gTotalBytesAlignedAllocs -= size;
144
145           printf("free #%d at address %x, from %s,line %d, size %d\n",gNumAlignedFree,real, filename,line,size);
146
147   sFreeFunc(real);
148 } else
149 {
150         printf("NULL ptr\n");
151 }
152}
153
154#else //BT_DEBUG_MEMORY_ALLOCATIONS
155
156void*   btAlignedAllocInternal  (size_t size, int alignment)
157{
158        gNumAlignedAllocs++;
159  void* ptr;
160#if defined (BT_HAS_ALIGNED_ALLOCATOR) || defined(__CELLOS_LV2__)
161        ptr = sAlignedAllocFunc(size, alignment);
162#else
163  char *real;
164  unsigned long offset;
165
166  real = (char *)sAllocFunc(size + sizeof(void *) + (alignment-1));
167  if (real) {
168    offset = (alignment - (unsigned long)(real + sizeof(void *))) & (alignment-1);
169    ptr = (void *)((real + sizeof(void *)) + offset);
170    *((void **)(ptr)-1) = (void *)(real);
171  } else {
172    ptr = (void *)(real);
173  }
174#endif  // defined (BT_HAS_ALIGNED_ALLOCATOR) || defined(__CELLOS_LV2__)
175//      printf("btAlignedAllocInternal %d, %x\n",size,ptr);
176        return ptr;
177}
178
179void    btAlignedFreeInternal   (void* ptr)
180{
181        if (!ptr)
182        {
183                return;
184        }
185
186        gNumAlignedFree++;
187//      printf("btAlignedFreeInternal %x\n",ptr);
188#if defined (BT_HAS_ALIGNED_ALLOCATOR) || defined(__CELLOS_LV2__)
189        sAlignedFreeFunc(ptr);
190#else
191  void* real;
192
193  if (ptr) {
194    real = *((void **)(ptr)-1);
195    sFreeFunc(real);
196  }
197#endif  // defined (BT_HAS_ALIGNED_ALLOCATOR) || defined(__CELLOS_LV2__)
198}
199
200#endif //BT_DEBUG_MEMORY_ALLOCATIONS
201
Note: See TracBrowser for help on using the repository browser.