[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / Bullet3Common / b3AlignedAllocator.cpp
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2013 Erwin Coumans  http://bulletphysics.org
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 "b3AlignedAllocator.h"
17
18 #ifdef B3_ALLOCATOR_STATISTICS
19 int b3g_numAlignedAllocs = 0;
20 int b3g_numAlignedFree = 0;
21 int b3g_totalBytesAlignedAllocs = 0;  //detect memory leaks
22 #endif
23
24 static void *b3AllocDefault(size_t size)
25 {
26         return malloc(size);
27 }
28
29 static void b3FreeDefault(void *ptr)
30 {
31         free(ptr);
32 }
33
34 static b3AllocFunc *b3s_allocFunc = b3AllocDefault;
35 static b3FreeFunc *b3s_freeFunc = b3FreeDefault;
36
37 #if defined(B3_HAS_ALIGNED_ALLOCATOR)
38 #include <malloc.h>
39 static void *b3AlignedAllocDefault(size_t size, int alignment)
40 {
41         return _aligned_malloc(size, (size_t)alignment);
42 }
43
44 static void b3AlignedFreeDefault(void *ptr)
45 {
46         _aligned_free(ptr);
47 }
48 #elif defined(__CELLOS_LV2__)
49 #include <stdlib.h>
50
51 static inline void *b3AlignedAllocDefault(size_t size, int alignment)
52 {
53         return memalign(alignment, size);
54 }
55
56 static inline void b3AlignedFreeDefault(void *ptr)
57 {
58         free(ptr);
59 }
60 #else
61
62 static inline void *b3AlignedAllocDefault(size_t size, int alignment)
63 {
64         void *ret;
65         char *real;
66         real = (char *)b3s_allocFunc(size + sizeof(void *) + (alignment - 1));
67         if (real)
68         {
69                 ret = b3AlignPointer(real + sizeof(void *), alignment);
70                 *((void **)(ret)-1) = (void *)(real);
71         }
72         else
73         {
74                 ret = (void *)(real);
75         }
76         return (ret);
77 }
78
79 static inline void b3AlignedFreeDefault(void *ptr)
80 {
81         void *real;
82
83         if (ptr)
84         {
85                 real = *((void **)(ptr)-1);
86                 b3s_freeFunc(real);
87         }
88 }
89 #endif
90
91 static b3AlignedAllocFunc *b3s_alignedAllocFunc = b3AlignedAllocDefault;
92 static b3AlignedFreeFunc *b3s_alignedFreeFunc = b3AlignedFreeDefault;
93
94 void b3AlignedAllocSetCustomAligned(b3AlignedAllocFunc *allocFunc, b3AlignedFreeFunc *freeFunc)
95 {
96         b3s_alignedAllocFunc = allocFunc ? allocFunc : b3AlignedAllocDefault;
97         b3s_alignedFreeFunc = freeFunc ? freeFunc : b3AlignedFreeDefault;
98 }
99
100 void b3AlignedAllocSetCustom(b3AllocFunc *allocFunc, b3FreeFunc *freeFunc)
101 {
102         b3s_allocFunc = allocFunc ? allocFunc : b3AllocDefault;
103         b3s_freeFunc = freeFunc ? freeFunc : b3FreeDefault;
104 }
105
106 #ifdef B3_DEBUG_MEMORY_ALLOCATIONS
107 //this generic allocator provides the total allocated number of bytes
108 #include <stdio.h>
109
110 void *b3AlignedAllocInternal(size_t size, int alignment, int line, char *filename)
111 {
112         void *ret;
113         char *real;
114 #ifdef B3_ALLOCATOR_STATISTICS
115         b3g_totalBytesAlignedAllocs += size;
116         b3g_numAlignedAllocs++;
117 #endif
118         real = (char *)b3s_allocFunc(size + 2 * sizeof(void *) + (alignment - 1));
119         if (real)
120         {
121                 ret = (void *)b3AlignPointer(real + 2 * sizeof(void *), alignment);
122                 *((void **)(ret)-1) = (void *)(real);
123                 *((int *)(ret)-2) = size;
124         }
125         else
126         {
127                 ret = (void *)(real);  //??
128         }
129
130         b3Printf("allocation#%d at address %x, from %s,line %d, size %d\n", b3g_numAlignedAllocs, real, filename, line, size);
131
132         int *ptr = (int *)ret;
133         *ptr = 12;
134         return (ret);
135 }
136
137 void b3AlignedFreeInternal(void *ptr, int line, char *filename)
138 {
139         void *real;
140 #ifdef B3_ALLOCATOR_STATISTICS
141         b3g_numAlignedFree++;
142 #endif
143         if (ptr)
144         {
145                 real = *((void **)(ptr)-1);
146                 int size = *((int *)(ptr)-2);
147 #ifdef B3_ALLOCATOR_STATISTICS
148                 b3g_totalBytesAlignedAllocs -= size;
149 #endif
150                 b3Printf("free #%d at address %x, from %s,line %d, size %d\n", b3g_numAlignedFree, real, filename, line, size);
151
152                 b3s_freeFunc(real);
153         }
154         else
155         {
156                 b3Printf("NULL ptr\n");
157         }
158 }
159
160 #else  //B3_DEBUG_MEMORY_ALLOCATIONS
161
162 void *b3AlignedAllocInternal(size_t size, int alignment)
163 {
164 #ifdef B3_ALLOCATOR_STATISTICS
165         b3g_numAlignedAllocs++;
166 #endif
167         void *ptr;
168         ptr = b3s_alignedAllocFunc(size, alignment);
169         //      b3Printf("b3AlignedAllocInternal %d, %x\n",size,ptr);
170         return ptr;
171 }
172
173 void b3AlignedFreeInternal(void *ptr)
174 {
175         if (!ptr)
176         {
177                 return;
178         }
179 #ifdef B3_ALLOCATOR_STATISTICS
180         b3g_numAlignedFree++;
181 #endif
182         //      b3Printf("b3AlignedFreeInternal %x\n",ptr);
183         b3s_alignedFreeFunc(ptr);
184 }
185
186 #endif  //B3_DEBUG_MEMORY_ALLOCATIONS