Debian Codegraph @

3rdparty/xspublic/xstypes/xsmalloc.c

raw
1
2 // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32
33 /*! \file
34 This file contains platform-specific memory allocation routines
35 */
36
37 #include "xsmalloc.h"
38 #if !(defined __ICCARM__) && !(defined _ADI_COMPILER) && !defined(__APPLE__) && !defined(__CRCC__) && !(defined(__arm__) && defined(__ARMCC_VERSION))
39 #include <malloc.h>
40 #endif
41 #include <stdlib.h>
42
43 #ifdef XSENS_ASSERT_MALLOC
44 #include <assert.h>
45 #undef XSENS_ASSERT_MALLOC
46 #define XSENS_ASSERT_MALLOC(x) assert(x)
47 #else
48 #define XSENS_ASSERT_MALLOC(x)
49 #endif
50
51 #if !(defined __ICCARM__) && !(defined _ADI_COMPILER) && defined(XSENS_DEBUG)
52 //#define TRACK_ALLOCS 32
53 #endif
54
55 #ifdef TRACK_ALLOCS
56 int lastAllocIdx = -1;
57 void* lastAllocs[TRACK_ALLOCS];
58 int lastFreeIdx = -1;
59 void* lastFrees[TRACK_ALLOCS];
60
61 int lastAlignedAllocIdx = -1;
62 void* lastAlignedAllocs[TRACK_ALLOCS];
63 int lastAlignedFreeIdx = -1;
64 void* lastAlignedFrees[TRACK_ALLOCS];
65 #endif
66
67 #ifndef _MSC_VER
68 # ifdef __ANDROID__
69 # define _aligned_malloc(size, align) memalign(align, size)
70 # elif (defined __ICCARM__) || (defined _ADI_COMPILER) || (defined __CRCC__) || (defined IAR_ARM_CM3) || (defined __ARMEL__) || (defined(__arm__) && defined(__ARMCC_VERSION))
71 # define _aligned_malloc(a, b) malloc(a)
72 # else
73
74 void* __cdecl _aligned_malloc(size_t _Size, size_t _Alignment)
75 {
76 void* rv = 0;
77 int err = posix_memalign(&rv, _Alignment, _Size);
78 if (err == 0)
79 return rv;
80 return NULL;
81 }
82
83 # endif
84
85 #define _aligned_realloc(p, n, a) realloc(p, n)
86 #define _aligned_free(_Memory) free(_Memory)
87
88 #endif //!_MSC_VER
89
90
91 //! \brief Allocates \a sz bytes of memory, optionally tracking the allocation
92 void* xsMalloc(size_t sz)
93 {
94 #ifdef TRACK_ALLOCS
95 void* ptr = malloc(sz);
96 XSENS_ASSERT_MALLOC(ptr);
97 lastAllocIdx = (lastAllocIdx + 1) & (TRACK_ALLOCS-1);
98 lastAllocs[lastAllocIdx] = ptr;
99 return ptr;
100 #else
101 void* ptr = malloc(sz);
102 XSENS_ASSERT_MALLOC(ptr);
103 return ptr;
104 #endif
105 }
106
107 //! \brief Reallocates \a sz bytes of memory, optionally tracking the allocation
108 void* xsRealloc(void* ptr, size_t sz)
109 {
110 #ifdef TRACK_ALLOCS
111 lastFreeIdx = (lastFreeIdx + 1) & (TRACK_ALLOCS-1);
112 lastFrees[lastFreeIdx] = ptr;
113
114 ptr = realloc(ptr, sz);
115 XSENS_ASSERT_MALLOC(ptr);
116 lastAllocIdx = (lastAllocIdx + 1) & (TRACK_ALLOCS-1);
117 lastAllocs[lastAllocIdx] = ptr;
118 return ptr;
119 #else
120 void* mem = realloc(ptr, sz);
121 XSENS_ASSERT_MALLOC(mem);
122 return mem;
123 #endif
124 }
125
126 //! \brief Frees the memory pointed to by \a ptr, optionally tracking the allocation
127 void xsFree(void* ptr)
128 {
129 #ifdef TRACK_ALLOCS
130 lastFreeIdx = (lastFreeIdx + 1) & (TRACK_ALLOCS-1);
131 lastFrees[lastFreeIdx] = ptr;
132 #endif
133 free(ptr);
134 }
135
136 //! \brief Allocates \a sz bytes of memory on a 16 byte boundary, optionally tracking the allocation
137 void* xsAlignedMalloc(size_t sz)
138 {
139 #ifdef TRACK_ALLOCS
140 void* ptr = _aligned_malloc(sz, 16);
141 XSENS_ASSERT_MALLOC(ptr);
142 lastAlignedAllocIdx = (lastAlignedAllocIdx + 1) & (TRACK_ALLOCS-1);
143 lastAlignedAllocs[lastAlignedAllocIdx] = ptr;
144 return ptr;
145 #else
146 void* ptr = _aligned_malloc(sz, 16);
147 XSENS_ASSERT_MALLOC(ptr);
148 return ptr;
149 #endif
150 }
151
152 //! \brief Reallocates \a sz bytes of memory on a 16 byte boundary, optionally tracking the allocation
153 void* xsAlignedRealloc(void* ptr, size_t sz)
154 {
155 #ifdef TRACK_ALLOCS
156 lastFreeIdx = (lastAlignedFreeIdx + 1) & (TRACK_ALLOCS-1);
157 lastAlignedFrees[lastAlignedFreeIdx] = ptr;
158
159 ptr = _aligned_realloc(ptr, sz, 16);
160 XSENS_ASSERT_MALLOC(ptr);
161 lastAlignedAllocIdx = (lastAlignedAllocIdx + 1) & (TRACK_ALLOCS-1);
162 lastAlignedAllocs[lastAlignedAllocIdx] = ptr;
163 return ptr;
164 #else
165 void* mem = _aligned_realloc(ptr, sz, 16);
166 XSENS_ASSERT_MALLOC(mem);
167 return mem;
168 #endif
169 }
170
171 //! \brief Frees the (aligned) memory pointed to by \a ptr, optionally tracking the allocation
172 void xsAlignedFree(void* ptr)
173 {
174 #ifdef TRACK_ALLOCS
175 lastAlignedFreeIdx = (lastAlignedFreeIdx + 1) & (TRACK_ALLOCS-1);
176 lastAlignedFrees[lastAlignedFreeIdx] = ptr;
177 #endif
178 _aligned_free(ptr);
179 }
180
181
182