agileRTOS (zrtos)  Version 0.8.0 (ghostbuster)
types.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 ykat UG (haftungsbeschraenkt) - All Rights Reserved
3  *
4  * Permission for non-commercial use is hereby granted,
5  * free of charge, without warranty of any kind.
6  */
7 #ifndef ZRTOS_TYPES_H
8 #define ZRTOS_TYPES_H
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 
14 #include <stdbool.h>
15 #include <stddef.h>
16 #include <stdint.h>
17 #include <limits.h>
18 #include <stdalign.h>
19 
20 #include <zrtos/cast.h>
21 
22 typedef uint16_t size_t;
23 typedef int16_t ssize_t;
24 /*
25 typedef enum{
26  false
27  ,true
28 }bool;
29 */
30 
31 int zrtos_types__ptr_cmp(void *a,void *b){
32  return ((ptrdiff_t)a)-((ptrdiff_t)b);
33 }
34 
35 void *zrtos_types__ptr_add(void *ptr,size_t byte_len){
36  return ((uint8_t*)ptr)+byte_len;
37 }
38 
39 void *zrtos_types__ptr_subtract(void *ptr,size_t byte_len){
40  return ((uint8_t*)ptr)-byte_len;
41 }
42 
43 size_t zrtos_types__ptr_get_byte_distance(void *bigger,void *smaller){
44  return ((uint8_t*)bigger)-((uint8_t*)smaller);
45 }
46 
47 #define ZRTOS_TYPES__BYTE_ALIGNMENT (__alignof__(max_align_t))
48 #define ZRTOS_TYPES__BYTE_ALIGNMENT_MASK (__alignof__(max_align_t)-1)
49 
51  uintptr_t ret = (uintptr_t)ptr;
52  ret = ret
55  ;
56  return (void*)ret;
57 }
58 
60  return len
63  ;
64 }
65 
66 #define zrtos_types__get_offset_of(type,member) \
67  __builtin_offsetof(type,member)
68 
69 #define zrtos_types__get_container_of(ptr, type, member) \
70  ({ \
71  typeof( ((type *)0)->member ) *mptr__ = (typeof( ((type *)0)->member ) *)(ptr); \
72  (type *)((char *)mptr__ - zrtos_types__get_offset_of(type,member)); \
73  })
74 
75 #define zrtos_types__get_container_of_ex(ptr, type, member) \
76  ((ptr) ? zrtos_types__get_container_of(ptr, type, member) : 0) \
77 
78 #define ZRTOS_TYPES__MIN(a,b) ((a)<(b)?(a):(b))
79 #define ZRTOS_TYPES__MAX(a,b) ((a)>(b)?(a):(b))
80 #define ZRTOS_TYPES__CMP(type,a_len,b_len)\
81  ((a_len) < (b_len) ? - 1 : ((a_len) > (b_len) ? 1 : 0))
82 
84  uint32_t first_address
85  ,uint32_t last_address
86  ,uint32_t offset
87  ,size_t *length
88 ){
89  if(offset >= first_address
90  && offset <= last_address){
91  *length = ZRTOS_TYPES__MIN(
92  *length
93  ,last_address - offset
94  );
95  return true;
96  }
97  return false;
98 }
99 
101  void *first_address
102  ,void *last_address
103  ,size_t offset
104  ,size_t *length
105 ){
107  last_address
108  ,first_address
109  )){
110  *length = ZRTOS_TYPES__MIN(
111  *length
113  last_address
115  last_address
116  ,offset
117  )
118  )
119  );
120  return true;
121  }
122  return false;
123 }
124 
126  uint64_t first_address
127  ,uint64_t last_address
128  ,uint64_t offset
129  ,size_t *length
130 ){
131  if(offset >= first_address
132  && offset <= last_address){
133  *length = ZRTOS_TYPES__MIN(
134  *length
135  ,last_address - offset
136  );
137  return true;
138  }
139  return false;
140 }
141 
142 
143 #ifdef __cplusplus
144 #define ZRTOS_TYPES__TYPEOF(a) auto
145 #else
146 #define ZRTOS_TYPES__TYPEOF(a) typeof(a)
147 #endif
148 
149 #define ZRTOS_TYPES__SWAP(a,b) \
150  do{ \
151  ZRTOS_TYPES__TYPEOF(a) a____ = (a); \
152  (a) = (b); \
153  (b) = a____; \
154  }while(0);
155 
156 #define ZRTOS_TYPES__SWAP_PTR_CONTENTS(a,b) \
157  do{ \
158  ZRTOS_TYPES__TYPEOF(*a) a____ = *(a); \
159  *(a) = *(b); \
160  *(b) = a____; \
161  }while(0);
162 
163 #define ZRTOS_TYPES__IS_ADD_OVERFLOW(a,b) \
164  (((a + b ) < a))
165 
166 #define ZRTOS_TYPES__GET_STATIC_ARRAY_LENGTH(arr) \
167  (sizeof(arr)/sizeof((arr)[0]))
168 
169 #define ZRTOS_TYPES__SIZE_MIN (0)
170 #define ZRTOS_TYPES__SIZE_MAX SIZE_MAX
171 
172 #define ZRTOS_TYPES__INT8_MIN INT8_MIN
173 #define ZRTOS_TYPES__INT8_MAX INT8_MAX
174 #define ZRTOS_TYPES__INT16_MIN INT16_MIN
175 #define ZRTOS_TYPES__INT16_MAX INT16_MAX
176 #define ZRTOS_TYPES__INT32_MIN INT32_MIN
177 #define ZRTOS_TYPES__INT32_MAX INT32_MAX
178 #define ZRTOS_TYPES__INT64_MIN INT64_MIN
179 #define ZRTOS_TYPES__INT64_MAX INT64_MAX
180 
181 #define ZRTOS_TYPES__UINT8_MIN (0)
182 #define ZRTOS_TYPES__UINT8_MAX UINT8_MAX
183 #define ZRTOS_TYPES__UINT16_MIN (0)
184 #define ZRTOS_TYPES__UINT16_MAX UINT16_MAX
185 #define ZRTOS_TYPES__UINT32_MIN (0)
186 #define ZRTOS_TYPES__UINT32_MAX UINT32_MAX
187 #define ZRTOS_TYPES__UINT64_MIN (0)
188 #define ZRTOS_TYPES__UINT64_MAX UINT64_MAX
189 
191  uint8_t h = (src & 0xF) + '0';
192  uint8_t l = (src >> 4) + '0';
193 
194  if(h > '9'){
195  h += 'A' - '9';
196  }
197  if(l > '9'){
198  l += 'A' - '9';
199  }
200 
201  dest[0] = h;
202  dest[1] = l;
203 
204  return 2;
205 }
206 
207 #define ZRTOS_TYPES__MAP(value,in_min,in_max,out_min,out_max)\
208  (((value)-(in_min))*((out_max)-(out_min))/((in_max)-(in_min))+(out_min))
209 
210 uint16_t zrtos_types__uint16_bswap(uint16_t x){
211  return ((__uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)));
212 }
213 
214 uint32_t zrtos_types__uint32_bswap(uint32_t x){
215  return ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8)
216  | (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24));
217 }
218 
219 uint64_t zrtos_types__uint64_bswap(uint64_t x){
220  return
221  ((((x) & 0xff00000000000000ull) >> 56)
222  | (((x) & 0x00ff000000000000ull) >> 40)
223  | (((x) & 0x0000ff0000000000ull) >> 24)
224  | (((x) & 0x000000ff00000000ull) >> 8)
225  | (((x) & 0x00000000ff000000ull) << 8)
226  | (((x) & 0x0000000000ff0000ull) << 24)
227  | (((x) & 0x000000000000ff00ull) << 40)
228  | (((x) & 0x00000000000000ffull) << 56))
229  ;
230 }
231 
232 #if ZRTOS_ARCH__BYTE_ORDER == ZRTOS_ARCH__BYTE_ORDER_LITTLE_ENDIAN
233 # define zrtos_types__htobe16(x) zrtos_types__uint16_bswap(x)
234 # define zrtos_types__htole16(x) (x)
235 # define zrtos_types__be16toh(x) zrtos_types__uint16_bswap(x)
236 # define zrtos_types__le16toh(x) (x)
237 
238 # define zrtos_types__htobe32(x) zrtos_types__uint32_bswap(x)
239 # define zrtos_types__htole32(x) (x)
240 # define zrtos_types__be32toh(x) zrtos_types__uint32_bswap(x)
241 # define zrtos_types__le32toh(x) (x)
242 
243 # define zrtos_types__htobe64(x) zrtos_types__uint64_bswap(x)
244 # define zrtos_types__htole64(x) (x)
245 # define zrtos_types__be64toh(x) zrtos_types__uint64_bswap(x)
246 # define zrtos_types__le64toh(x) (x)
247 #else
248 # define zrtos_types__htobe16(x) (x)
249 # define zrtos_types__htole16(x) zrtos_types__uint16_bswap(x)
250 # define zrtos_types__be16toh(x) (x)
251 # define zrtos_types__le16toh(x) zrtos_types__uint16_bswap(x)
252 
253 # define zrtos_types__htobe32(x) (x)
254 # define zrtos_types__htole32(x) zrtos_types__uint32_bswap(x)
255 # define zrtos_types__be32toh(x) (x)
256 # define zrtos_types__le32toh(x) zrtos_types__uint32_bswap(x)
257 
258 # define zrtos_types__htobe64(x) (x)
259 # define zrtos_types__htole64(x) zrtos_types__uint64_bswap(x)
260 # define zrtos_types__be64toh(x) (x)
261 # define zrtos_types__le64toh(x) zrtos_types__uint64_bswap(x)
262 #endif
263 
265  return c >= '0' && c <= '9';
266 }
267 
269  return c == ' ' || c == '\t';
270 }
271 
273  return c == '\r' || c == '\n';
274 }
275 
276 #ifdef __cplusplus
277 }
278 #endif
279 #endif
void * zrtos_types__ptr_subtract(void *ptr, size_t byte_len)
Definition: types.h:39
bool zrtos_types__uint32_is_valid_address_range(uint32_t first_address, uint32_t last_address, uint32_t offset, size_t *length)
Definition: types.h:83
#define ZRTOS_TYPES__BYTE_ALIGNMENT
Definition: types.h:47
void * zrtos_types__ptr_to_alignment(void *ptr)
Definition: types.h:50
uint32_t zrtos_types__uint32_bswap(uint32_t x)
Definition: types.h:214
size_t zrtos_types__uint8_to_hex(uint8_t *dest, uint8_t src)
Definition: types.h:190
uint16_t zrtos_types__uint16_bswap(uint16_t x)
Definition: types.h:210
bool zrtos_types__uint64_is_valid_address_range(uint64_t first_address, uint64_t last_address, uint64_t offset, size_t *length)
Definition: types.h:125
int16_t ssize_t
Definition: types.h:23
bool zrtos_types__is_digit(char c)
Definition: types.h:264
uint16_t size_t
Definition: types.h:22
uint64_t zrtos_types__uint64_bswap(uint64_t x)
Definition: types.h:219
#define ZRTOS_TYPES__MIN(a, b)
Definition: types.h:78
void * zrtos_types__ptr_add(void *ptr, size_t byte_len)
Definition: types.h:35
bool zrtos_types__is_newline(char c)
Definition: types.h:272
size_t zrtos_types__ptr_get_byte_distance(void *bigger, void *smaller)
Definition: types.h:43
int zrtos_types__ptr_cmp(void *a, void *b)
Definition: types.h:31
#define ZRTOS_TYPES__BYTE_ALIGNMENT_MASK
Definition: types.h:48
bool zrtos_types__ptr_is_valid_address_range(void *first_address, void *last_address, size_t offset, size_t *length)
Definition: types.h:100
size_t zrtos_types__ceil_size_to_alignment(size_t len)
Definition: types.h:59
static uint8_t
Definition: mcp2515.h:159
bool zrtos_types__is_whitespace(char c)
Definition: types.h:268