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


xsens_mti_driver
Author(s):
autogenerated on Sun Sep 3 2023 02:43:20