ipdbg: fix double free of virtual-ir data
[openocd.git] / src / helper / types.h
blob53249e5b79e407c3279df7b9334c05f06531bc86
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * Copyright (C) 2004, 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
6 * *
7 * Copyright (C) 2007,2008 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
9 ***************************************************************************/
11 #ifndef OPENOCD_HELPER_TYPES_H
12 #define OPENOCD_HELPER_TYPES_H
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
18 #include <stddef.h>
19 #include <assert.h>
20 #ifdef HAVE_SYS_TYPES_H
21 #include <sys/types.h>
22 #endif
23 #ifdef HAVE_STDINT_H
24 #include <stdint.h>
25 #endif
26 #ifdef HAVE_INTTYPES_H
27 #include <inttypes.h>
28 #endif
30 #ifdef HAVE_STDBOOL_H
31 #include <stdbool.h>
32 #else /* HAVE_STDBOOL_H */
33 #define __bool_true_false_are_defined 1
35 #ifndef HAVE__BOOL
36 #ifndef __cplusplus
38 #define false 0
39 #define true 1
41 #endif /* __cplusplus */
42 #endif /* HAVE__BOOL */
43 #endif /* HAVE_STDBOOL_H */
45 /// turns a macro argument into a string constant
46 #define stringify(s) __stringify(s)
47 #define __stringify(s) #s
50 /**
51 * Compute the number of elements of a variable length array.
52 * <code>
53 * const char *strs[] = { "a", "b", "c" };
54 * unsigned num_strs = ARRAY_SIZE(strs);
55 * </code>
57 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
60 /**
61 * Cast a member of a structure out to the containing structure.
62 * @param ptr The pointer to the member.
63 * @param type The type of the container struct this is embedded in.
64 * @param member The name of the member within the struct.
66 * This is a mechanism which is used throughout the Linux kernel.
68 #define container_of(ptr, type, member) ({ \
69 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
70 (type *)( (void *) ( (char *)__mptr - offsetof(type,member) ) );})
73 /**
74 * Rounds @c m up to the nearest multiple of @c n using division.
75 * @param m The value to round up to @c n.
76 * @param n Round @c m up to a multiple of this number.
77 * @returns The rounded integer value.
79 #define DIV_ROUND_UP(m, n) (((m) + (n) - 1) / (n))
82 /* DANGER!!!! here be dragons!
84 * Leave these fn's as byte accesses because it is safe
85 * across architectures. Clever usage of 32 bit access
86 * will create problems on some hosts.
88 * Note that the "buf" pointer in memory is probably unaligned.
90 * Were these functions to be re-written to take a 32 bit wide or 16 bit wide
91 * memory access shortcut, then on some CPU's, i.e. ARM7, the 2 lsbytes of the address are
92 * ignored for 32 bit access, whereas on other CPU's a 32 bit wide unaligned memory access
93 * will cause an exception, and lastly on x86, an unaligned "greater than bytewide"
94 * memory access works as if aligned. So what follows below will work for all
95 * platforms and gives the compiler leeway to do its own platform specific optimizations.
97 * Again, note that the "buf" pointer in memory is probably unaligned.
100 static inline uint64_t le_to_h_u64(const uint8_t *buf)
102 return (uint64_t)((uint64_t)buf[0] |
103 (uint64_t)buf[1] << 8 |
104 (uint64_t)buf[2] << 16 |
105 (uint64_t)buf[3] << 24 |
106 (uint64_t)buf[4] << 32 |
107 (uint64_t)buf[5] << 40 |
108 (uint64_t)buf[6] << 48 |
109 (uint64_t)buf[7] << 56);
112 static inline uint32_t le_to_h_u32(const uint8_t *buf)
114 return (uint32_t)((uint32_t)buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16 | (uint32_t)buf[3] << 24);
117 static inline uint32_t le_to_h_u24(const uint8_t *buf)
119 return (uint32_t)((uint32_t)buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16);
122 static inline uint16_t le_to_h_u16(const uint8_t *buf)
124 return (uint16_t)((uint16_t)buf[0] | (uint16_t)buf[1] << 8);
127 static inline uint64_t be_to_h_u64(const uint8_t *buf)
129 return (uint64_t)((uint64_t)buf[7] |
130 (uint64_t)buf[6] << 8 |
131 (uint64_t)buf[5] << 16 |
132 (uint64_t)buf[4] << 24 |
133 (uint64_t)buf[3] << 32 |
134 (uint64_t)buf[2] << 40 |
135 (uint64_t)buf[1] << 48 |
136 (uint64_t)buf[0] << 56);
139 static inline uint32_t be_to_h_u32(const uint8_t *buf)
141 return (uint32_t)((uint32_t)buf[3] | (uint32_t)buf[2] << 8 | (uint32_t)buf[1] << 16 | (uint32_t)buf[0] << 24);
144 static inline uint32_t be_to_h_u24(const uint8_t *buf)
146 return (uint32_t)((uint32_t)buf[2] | (uint32_t)buf[1] << 8 | (uint32_t)buf[0] << 16);
149 static inline uint16_t be_to_h_u16(const uint8_t *buf)
151 return (uint16_t)((uint16_t)buf[1] | (uint16_t)buf[0] << 8);
154 static inline void h_u64_to_le(uint8_t *buf, uint64_t val)
156 buf[7] = (uint8_t) (val >> 56);
157 buf[6] = (uint8_t) (val >> 48);
158 buf[5] = (uint8_t) (val >> 40);
159 buf[4] = (uint8_t) (val >> 32);
160 buf[3] = (uint8_t) (val >> 24);
161 buf[2] = (uint8_t) (val >> 16);
162 buf[1] = (uint8_t) (val >> 8);
163 buf[0] = (uint8_t) (val >> 0);
166 static inline void h_u64_to_be(uint8_t *buf, uint64_t val)
168 buf[0] = (uint8_t) (val >> 56);
169 buf[1] = (uint8_t) (val >> 48);
170 buf[2] = (uint8_t) (val >> 40);
171 buf[3] = (uint8_t) (val >> 32);
172 buf[4] = (uint8_t) (val >> 24);
173 buf[5] = (uint8_t) (val >> 16);
174 buf[6] = (uint8_t) (val >> 8);
175 buf[7] = (uint8_t) (val >> 0);
178 static inline void h_u32_to_le(uint8_t *buf, uint32_t val)
180 buf[3] = (val >> 24) & 0xff;
181 buf[2] = (val >> 16) & 0xff;
182 buf[1] = (val >> 8) & 0xff;
183 buf[0] = (val >> 0) & 0xff;
186 static inline void h_u32_to_be(uint8_t *buf, uint32_t val)
188 buf[0] = (val >> 24) & 0xff;
189 buf[1] = (val >> 16) & 0xff;
190 buf[2] = (val >> 8) & 0xff;
191 buf[3] = (val >> 0) & 0xff;
194 static inline void h_u24_to_le(uint8_t *buf, unsigned int val)
196 buf[2] = (val >> 16) & 0xff;
197 buf[1] = (val >> 8) & 0xff;
198 buf[0] = (val >> 0) & 0xff;
201 static inline void h_u24_to_be(uint8_t *buf, unsigned int val)
203 buf[0] = (val >> 16) & 0xff;
204 buf[1] = (val >> 8) & 0xff;
205 buf[2] = (val >> 0) & 0xff;
208 static inline void h_u16_to_le(uint8_t *buf, uint16_t val)
210 buf[1] = (val >> 8) & 0xff;
211 buf[0] = (val >> 0) & 0xff;
214 static inline void h_u16_to_be(uint8_t *buf, uint16_t val)
216 buf[0] = (val >> 8) & 0xff;
217 buf[1] = (val >> 0) & 0xff;
221 * Byte-swap buffer 16-bit.
223 * Len must be even, dst and src must be either the same or non-overlapping.
225 * @param dst Destination buffer.
226 * @param src Source buffer.
227 * @param len Length of source (and destination) buffer, in bytes.
229 static inline void buf_bswap16(uint8_t *dst, const uint8_t *src, size_t len)
231 assert(len % 2 == 0);
232 assert(dst == src || dst + len <= src || src + len <= dst);
234 for (size_t n = 0; n < len; n += 2) {
235 uint16_t x = be_to_h_u16(src + n);
236 h_u16_to_le(dst + n, x);
241 * Byte-swap buffer 32-bit.
243 * Len must be divisible by four, dst and src must be either the same or non-overlapping.
245 * @param dst Destination buffer.
246 * @param src Source buffer.
247 * @param len Length of source (and destination) buffer, in bytes.
249 static inline void buf_bswap32(uint8_t *dst, const uint8_t *src, size_t len)
251 assert(len % 4 == 0);
252 assert(dst == src || dst + len <= src || src + len <= dst);
254 for (size_t n = 0; n < len; n += 4) {
255 uint32_t x = be_to_h_u32(src + n);
256 h_u32_to_le(dst + n, x);
261 * Calculate the (even) parity of a 32-bit datum.
262 * @param x The datum.
263 * @return 1 if the number of set bits in x is odd, 0 if it is even.
265 static inline int parity_u32(uint32_t x)
267 #ifdef __GNUC__
268 return __builtin_parityl(x);
269 #else
270 x ^= x >> 16;
271 x ^= x >> 8;
272 x ^= x >> 4;
273 x ^= x >> 2;
274 x ^= x >> 1;
275 return x & 1;
276 #endif
279 #if defined(__ECOS)
281 /* eCos plain lacks these definition... A series of upstream patches
282 * could probably repair it, but it seems like too much work to be
283 * worth it.
286 #if !defined(_STDINT_H)
287 #define PRId32 "d"
288 #define PRIi32 "i"
289 #define PRIo32 "o"
290 #define PRIu32 "u"
291 #define PRIx32 "x"
292 #define PRIX32 "X"
293 #define SCNx32 "x"
294 #define PRId8 PRId32
295 #define SCNx64 "llx"
296 #define PRId64 "lld"
297 #define PRIi64 "lli"
298 #define PRIo64 "llo"
299 #define PRIu64 "llu"
300 #define PRIx64 "llx"
301 #define PRIX64 "llX"
303 typedef CYG_ADDRWORD intptr_t;
304 typedef int64_t intmax_t;
305 typedef uint64_t uintmax_t;
306 #define INT8_MAX 0x7f
307 #define INT8_MIN (-INT8_MAX - 1)
308 # define UINT8_MAX (255)
309 #define INT16_MAX 0x7fff
310 #define INT16_MIN (-INT16_MAX - 1)
311 # define UINT16_MAX (65535)
312 #define INT32_MAX 0x7fffffffL
313 #define INT32_MIN (-INT32_MAX - 1L)
314 # define UINT32_MAX (4294967295U)
315 #define INT64_MAX 0x7fffffffffffffffLL
316 #define INT64_MIN (-INT64_MAX - 1LL)
317 #define UINT64_MAX (__CONCAT(INT64_MAX, U) * 2ULL + 1ULL)
318 #endif
320 #ifndef LLONG_MAX
321 #define ULLONG_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
322 #define LLONG_MAX INT64_C(0x7FFFFFFFFFFFFFFF)
323 #define LLONG_MIN ULLONG_MAX
324 #endif
327 #define ULLONG_MAX 18446744073709551615
329 /* C99, eCos is C90 compliant (with bits of C99) */
330 #define isblank(c) ((c) == ' ' || (c) == '\t')
333 #endif
335 typedef uint64_t target_addr_t;
336 #define TARGET_ADDR_MAX UINT64_MAX
337 #define TARGET_PRIdADDR PRId64
338 #define TARGET_PRIuADDR PRIu64
339 #define TARGET_PRIoADDR PRIo64
340 #define TARGET_PRIxADDR PRIx64
341 #define TARGET_PRIXADDR PRIX64
342 #define TARGET_ADDR_FMT "0x%8.8" TARGET_PRIxADDR
344 #endif /* OPENOCD_HELPER_TYPES_H */