~ubuntu-branches/ubuntu/maverick/blender/maverick

« back to all changes in this revision

Viewing changes to extern/bullet2/src/LinearMath/btAlignedAllocator.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Khashayar Naderehvandi, Khashayar Naderehvandi, Alessio Treglia
  • Date: 2009-01-22 16:53:59 UTC
  • mfrom: (14.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090122165359-v0996tn7fbit64ni
Tags: 2.48a+dfsg-1ubuntu1
[ Khashayar Naderehvandi ]
* Merge from debian experimental (LP: #320045), Ubuntu remaining changes:
  - Add patch correcting header file locations.
  - Add libvorbis-dev and libgsm1-dev to Build-Depends.
  - Use avcodec_decode_audio2() in source/blender/src/hddaudio.c

[ Alessio Treglia ]
* Add missing previous changelog entries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
#include "btAlignedAllocator.h"
17
17
 
18
 
 
19
 
#if defined (BT_HAS_ALIGNED_ALOCATOR)
20
 
 
 
18
int gNumAlignedAllocs = 0;
 
19
int gNumAlignedFree = 0;
 
20
int gTotalBytesAlignedAllocs = 0;//detect memory leaks
 
21
 
 
22
#if defined (BT_HAS_ALIGNED_ALLOCATOR)
21
23
#include <malloc.h>
22
 
void*   btAlignedAlloc  (int size, int alignment)
 
24
static void *btAlignedAllocDefault(size_t size, int alignment)
23
25
{
24
 
        return _aligned_malloc(size,alignment);
 
26
        return _aligned_malloc(size, (size_t)alignment);
25
27
}
26
28
 
27
 
void    btAlignedFree   (void* ptr)
 
29
static void btAlignedFreeDefault(void *ptr)
28
30
{
29
31
        _aligned_free(ptr);
30
32
}
31
 
 
32
 
#else
33
 
 
34
 
#ifdef __CELLOS_LV2__
35
 
 
 
33
#elif defined(__CELLOS_LV2__)
36
34
#include <stdlib.h>
37
35
 
38
 
int numAllocs = 0;
39
 
int numFree = 0;
40
 
 
41
 
void*   btAlignedAlloc  (int size, int alignment)
 
36
static inline void *btAlignedAllocDefault(size_t size, int alignment)
42
37
{
43
 
        numAllocs++;
44
38
        return memalign(alignment, size);
45
39
}
46
40
 
47
 
void    btAlignedFree   (void* ptr)
 
41
static inline void btAlignedFreeDefault(void *ptr)
48
42
{
49
 
        numFree++;
50
43
        free(ptr);
51
44
}
52
 
 
53
45
#else
54
 
///todo
55
 
///will add some multi-platform version that works without _aligned_malloc/_aligned_free
56
 
 
57
 
void*   btAlignedAlloc  (int size, int alignment)
58
 
{
59
 
        return new char[size];
60
 
}
61
 
 
62
 
void    btAlignedFree   (void* ptr)
63
 
{
64
 
        delete [] (char*) ptr;
65
 
}
66
 
#endif //
67
 
 
 
46
static 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
 
 
63
static inline void btAlignedFreeDefault(void *ptr)
 
64
{
 
65
  void* real;
 
66
 
 
67
  if (ptr) {
 
68
    real = *((void **)(ptr)-1);
 
69
    free(real);
 
70
  }
 
71
}
68
72
#endif
69
73
 
 
74
static void *btAllocDefault(size_t size)
 
75
{
 
76
        return malloc(size);
 
77
}
 
78
 
 
79
static void btFreeDefault(void *ptr)
 
80
{
 
81
        free(ptr);
 
82
}
 
83
 
 
84
static btAlignedAllocFunc *sAlignedAllocFunc = btAlignedAllocDefault;
 
85
static btAlignedFreeFunc *sAlignedFreeFunc = btAlignedFreeDefault;
 
86
static btAllocFunc *sAllocFunc = btAllocDefault;
 
87
static btFreeFunc *sFreeFunc = btFreeDefault;
 
88
 
 
89
void btAlignedAllocSetCustomAligned(btAlignedAllocFunc *allocFunc, btAlignedFreeFunc *freeFunc)
 
90
{
 
91
  sAlignedAllocFunc = allocFunc ? allocFunc : btAlignedAllocDefault;
 
92
  sAlignedFreeFunc = freeFunc ? freeFunc : btAlignedFreeDefault;
 
93
}
 
94
 
 
95
void 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
 
 
105
void*   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
 
 
134
void    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
 
 
156
void*   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
 
 
179
void    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
70
201