| 1 |
|
| #ifndef GENERICSTACK_H |
| 2 |
|
| #define GENERICSTACK_H |
| 3 |
|
| |
| 4 |
|
| #include <stdlib.h> /* For malloc, free */ |
| 5 |
|
| #include <stddef.h> /* For size_t */ |
| 6 |
|
| #include <string.h> /* For memcpy */ |
| 7 |
|
| #include <errno.h> /* For errno */ |
| 8 |
|
| |
| 9 |
|
| /* =============== */ |
| 10 |
|
| /* C generic stack */ |
| 11 |
|
| /* =============== */ |
| 12 |
|
| |
| 13 |
|
| /* ---------------------------------------------------------------------- */ |
| 14 |
|
| /* Most of the generic stack implementations either assume that every */ |
| 15 |
|
| /* element is of the same size, or bypass type checking going through */ |
| 16 |
|
| /* a C layer. This version does not have these two constraints. */ |
| 17 |
|
| /* By default it is restricted to ANSI-C data type, nevertheless */ |
| 18 |
|
| /* adding others is as trivial as looking into e.g. long long below. */ |
| 19 |
|
| /* Please note that, IF the data typecast to the stack, no warning. */ |
| 20 |
|
| /* */ |
| 21 |
|
| /* Purists will notice this is an array-based implementation. This */ |
| 22 |
|
| /* choice was made because it is fits all my applications. */ |
| 23 |
|
| /* */ |
| 24 |
|
| /* Define GENERICSTACK_C99 to have C99 data type */ |
| 25 |
|
| /* Define GENERICSTACK_CUSTOM to XXX to have a custom type XXX */ |
| 26 |
|
| /* */ |
| 27 |
|
| /* Stack size increases automatically if needed until user disposes it. */ |
| 28 |
|
| /* ---------------------------------------------------------------------- */ |
| 29 |
|
| /* About the implementation we do use flexible arrays, because we do not */ |
| 30 |
|
| /* want to access out of what the compiler did on memory alignment, */ |
| 31 |
|
| /* internal padding etc... */ |
| 32 |
|
| /* Instead the rule is the following: as long as the generic stack usage */ |
| 33 |
|
| /* do not goes beyond the default size, internal default pool is used. */ |
| 34 |
|
| /* As soon as the usage goes beyond, the full data moved to the heap. */ |
| 35 |
|
| /* So, from access point of view, all data will always be aligned in a */ |
| 36 |
|
| /* single internal array, but there is a cost when: */ |
| 37 |
|
| /* - usage goes beyond default length (once with a malloc + memcpy) */ |
| 38 |
|
| |
| 39 |
|
| /* We count on any decent compiler to automatically inline our functions. */ |
| 40 |
|
| /* In doubt, set the GENERICSTACK_INLINE keyword, that will tell we WISH */ |
| 41 |
|
| /* inlining. */ |
| 42 |
|
| #ifdef __cplusplus |
| 43 |
|
| # undef GENERICSTACK_INLINE /* With C++ inline is well defined */ |
| 44 |
|
| # define GENERICSTACK_INLINE inline |
| 45 |
|
| #else |
| 46 |
|
| # ifndef GENERICSTACK_INLINE |
| 47 |
|
| # define GENERICSTACK_INLINE /* Define it to nothing */ |
| 48 |
|
| # endif |
| 49 |
|
| #endif |
| 50 |
|
| |
| 51 |
|
| /* ====================================================================== */ |
| 52 |
|
| /* gcc family has the __builtin_expect that optimizes branch prediction. */ |
| 53 |
|
| /* This is off by default, but you can set the macros */ |
| 54 |
|
| /* GENERICSTACK_LIKELY and GENERICSTACK_UNLIKELY to handle that. */ |
| 55 |
|
| /* ====================================================================== */ |
| 56 |
|
| #ifndef GENERICSTACK_LIKELY |
| 57 |
|
| # define GENERICSTACK_LIKELY(x) x |
| 58 |
|
| #endif |
| 59 |
|
| #ifndef GENERICSTACK_UNLIKELY |
| 60 |
|
| # define GENERICSTACK_UNLIKELY(x) x |
| 61 |
|
| #endif |
| 62 |
|
| |
| 63 |
|
| /* ====================================================================== */ |
| 64 |
|
| /* In theory we should check for int turnaround. In practice this is */ |
| 65 |
|
| /* instructions for nothing, this is a so improbable case. Nevertheless */ |
| 66 |
|
| /* if you insist, set the variable GENERICSTACK_PARANOID. */ |
| 67 |
|
| /* ====================================================================== */ |
| 68 |
|
| /* #define GENERICSTACK_PARANOID */ |
| 69 |
|
| |
| 70 |
|
| /* ====================================================================== */ |
| 71 |
|
| /* Stack default length. */ |
| 72 |
|
| /* ====================================================================== */ |
| 73 |
|
| #ifndef GENERICSTACK_DEFAULT_LENGTH |
| 74 |
|
| # define GENERICSTACK_DEFAULT_LENGTH 128 /* Subjective number */ |
| 75 |
|
| #endif |
| 76 |
|
| #if GENERICSTACK_DEFAULT_LENGTH < 0 |
| 77 |
|
| /* We rely on GENERICSTACK_DEFAULT_LENGTH >= 0 to managed extension */ |
| 78 |
|
| # error "GENERICSTACK_DEFAULT_LENGTH < 0 is not allowed" |
| 79 |
|
| #endif |
| 80 |
|
| |
| 81 |
|
| /* ====================================================================== */ |
| 82 |
|
| /* C99 support. */ |
| 83 |
|
| /* ====================================================================== */ |
| 84 |
|
| #ifdef GENERICSTACK_C99 |
| 85 |
|
| # undef GENERICSTACK_HAVE_LONG_LONG |
| 86 |
|
| # define GENERICSTACK_HAVE_LONG_LONG 1 |
| 87 |
|
| # undef GENERICSTACK_HAVE__BOOL |
| 88 |
|
| # define GENERICSTACK_HAVE__BOOL 1 |
| 89 |
|
| # undef GENERICSTACK_HAVE__COMPLEX |
| 90 |
|
| # define GENERICSTACK_HAVE__COMPLEX 1 |
| 91 |
|
| #else |
| 92 |
|
| # ifndef GENERICSTACK_HAVE_LONG_LONG |
| 93 |
|
| # define GENERICSTACK_HAVE_LONG_LONG 0 |
| 94 |
|
| # endif |
| 95 |
|
| # ifndef GENERICSTACK_HAVE__BOOL |
| 96 |
|
| # define GENERICSTACK_HAVE__BOOL 0 |
| 97 |
|
| # endif |
| 98 |
|
| # ifndef GENERICSTACK_HAVE__COMPLEX |
| 99 |
|
| # define GENERICSTACK_HAVE__COMPLEX 0 |
| 100 |
|
| # endif |
| 101 |
|
| #endif |
| 102 |
|
| |
| 103 |
|
| /* ====================================================================== */ |
| 104 |
|
| /* Custom type support. */ |
| 105 |
|
| /* ====================================================================== */ |
| 106 |
|
| #ifdef GENERICSTACK_CUSTOM |
| 107 |
|
| # undef GENERICSTACK_HAVE_CUSTOM |
| 108 |
|
| # define GENERICSTACK_HAVE_CUSTOM 1 |
| 109 |
|
| #else |
| 110 |
|
| # ifndef GENERICSTACK_HAVE_CUSTOM |
| 111 |
|
| # define GENERICSTACK_HAVE_CUSTOM 0 |
| 112 |
|
| # endif |
| 113 |
|
| #endif |
| 114 |
|
| |
| 115 |
|
| /* ====================================================================== */ |
| 116 |
|
| /* Items definition. */ |
| 117 |
|
| /* ====================================================================== */ |
| 118 |
|
| typedef enum genericStackItemType { |
| 119 |
|
| GENERICSTACKITEMTYPE_NA = 0, /* Not a hasard it is explicitely 0 */ |
| 120 |
|
| GENERICSTACKITEMTYPE_CHAR, |
| 121 |
|
| GENERICSTACKITEMTYPE_SHORT, |
| 122 |
|
| GENERICSTACKITEMTYPE_INT, |
| 123 |
|
| GENERICSTACKITEMTYPE_LONG, |
| 124 |
|
| GENERICSTACKITEMTYPE_FLOAT, |
| 125 |
|
| GENERICSTACKITEMTYPE_DOUBLE, |
| 126 |
|
| GENERICSTACKITEMTYPE_PTR, |
| 127 |
|
| GENERICSTACKITEMTYPE_ARRAY, |
| 128 |
|
| #if GENERICSTACK_HAVE_LONG_LONG |
| 129 |
|
| GENERICSTACKITEMTYPE_LONG_LONG, |
| 130 |
|
| #endif |
| 131 |
|
| #if GENERICSTACK_HAVE__BOOL |
| 132 |
|
| GENERICSTACKITEMTYPE__BOOL, |
| 133 |
|
| #endif |
| 134 |
|
| #if GENERICSTACK_HAVE__COMPLEX |
| 135 |
|
| GENERICSTACKITEMTYPE_FLOAT__COMPLEX, |
| 136 |
|
| GENERICSTACKITEMTYPE_DOUBLE__COMPLEX, |
| 137 |
|
| GENERICSTACKITEMTYPE_LONG_DOUBLE__COMPLEX, |
| 138 |
|
| #endif |
| 139 |
|
| #if GENERICSTACK_HAVE_CUSTOM |
| 140 |
|
| GENERICSTACKITEMTYPE_CUSTOM, |
| 141 |
|
| #endif |
| 142 |
|
| GENERICSTACKITEMTYPE_LONG_DOUBLE, |
| 143 |
|
| _GENERICSTACKITEMTYPE_MAX |
| 144 |
|
| } genericStackItemType_t; |
| 145 |
|
| |
| 146 |
|
| typedef struct genericStackItemTypeArray { |
| 147 |
|
| void *p; |
| 148 |
|
| size_t lengthl; |
| 149 |
|
| } genericStackItemTypeArray_t; |
| 150 |
|
| |
| 151 |
|
| typedef struct genericStackItem { |
| 152 |
|
| genericStackItemType_t type; |
| 153 |
|
| union { |
| 154 |
|
| char c; |
| 155 |
|
| short s; |
| 156 |
|
| int i; |
| 157 |
|
| long l; |
| 158 |
|
| float f; |
| 159 |
|
| double d; |
| 160 |
|
| void *p; |
| 161 |
|
| genericStackItemTypeArray_t a; |
| 162 |
|
| #if GENERICSTACK_HAVE_LONG_LONG > 0 |
| 163 |
|
| long long ll; |
| 164 |
|
| #endif |
| 165 |
|
| #if GENERICSTACK_HAVE__BOOL > 0 |
| 166 |
|
| _Bool b; |
| 167 |
|
| #endif |
| 168 |
|
| #if GENERICSTACK_HAVE__COMPLEX > 0 |
| 169 |
|
| float _Complex fc; |
| 170 |
|
| double _Complex dc; |
| 171 |
|
| long double _Complex ldc; |
| 172 |
|
| #endif |
| 173 |
|
| #if GENERICSTACK_HAVE_CUSTOM > 0 |
| 174 |
|
| GENERICSTACK_CUSTOM custom; |
| 175 |
|
| #endif |
| 176 |
|
| long double ld; |
| 177 |
|
| } u; |
| 178 |
|
| } genericStackItem_t; |
| 179 |
|
| |
| 180 |
|
| /* ====================================================================== */ |
| 181 |
|
| /* Stack definition. */ |
| 182 |
|
| /* ====================================================================== */ |
| 183 |
|
| typedef struct genericStack { |
| 184 |
|
| genericStackItem_t *items; /* Point to defaultItems or heapItems */ |
| 185 |
|
| #if GENERICSTACK_DEFAULT_LENGTH > 0 |
| 186 |
|
| genericStackItem_t defaultItems[GENERICSTACK_DEFAULT_LENGTH]; |
| 187 |
|
| #endif |
| 188 |
|
| genericStackItem_t *heapItems; |
| 189 |
|
| int initialLengthi; /* Initial length */ |
| 190 |
|
| int heapi; /* Heap length */ |
| 191 |
|
| int lengthi; /* Available length */ |
| 192 |
|
| int usedi; /* Used length */ |
| 193 |
|
| int errori; /* True if the stack had an error */ |
| 194 |
|
| } genericStack_t; |
| 195 |
|
| |
| 196 |
|
| /* ====================================================================== */ |
| 197 |
|
| /* "Inline"d functions */ |
| 198 |
|
| /* */ |
| 199 |
|
| /* Not that we never assert: we assume that a SIGSEGV will be enough -; */ |
| 200 |
|
| /* ====================================================================== */ |
| 201 |
|
| |
| 202 |
|
| #ifdef GENERICSTACK_ZERO_INT_IS_NOT_ZERO_BYTES |
| 203 |
|
| /* we want to memset items[starti...endi] - no protection on indices because we call it only when we know we need it */ |
| 204 |
|
| static GENERICSTACK_INLINE void _GENERICSTACK_NA_MEMSET(genericStack_t *stackp, int starti, int endi) { |
| 205 |
|
| int indicei; |
| 206 |
|
| |
| 207 |
|
| for (indicei = starti; indicei <= endi; indicei++) { |
| 208 |
|
| stackp->items[indicei].type = GENERICSTACKITEMTYPE_NA; |
| 209 |
|
| } |
| 210 |
|
| } |
| 211 |
|
| |
| 212 |
|
| # if GENERICSTACK_DEFAULT_LENGTH > 0 |
| 213 |
|
| static GENERICSTACK_INLINE short _GENERICSTACK_CREATE_HEAP(genericStack_t *stackp, int heapi) { |
| 214 |
|
| short rcb; |
| 215 |
|
| |
| 216 |
|
| stackp->heapItems = (genericStackItem_t *) malloc(heapi * sizeof(genericStackItem_t)); |
| 217 |
|
| if (GENERICSTACK_UNLIKELY(stackp->heapItems == NULL)) { |
| 218 |
|
| stackp->errori = 1; |
| 219 |
|
| rcb = 0; |
| 220 |
|
| } else { |
| 221 |
|
| stackp->items = stackp->heapItems; |
| 222 |
|
| if (stackp->usedi > 0) { |
| 223 |
|
| memcpy(stackp->items, stackp->defaultItems, stackp->usedi * sizeof(genericStackItem_t)); |
| 224 |
|
| } |
| 225 |
|
| stackp->lengthi = stackp->heapi = heapi; |
| 226 |
|
| rcb = 1; |
| 227 |
|
| } |
| 228 |
|
| |
| 229 |
|
| return rcb; |
| 230 |
|
| } |
| 231 |
|
| # else /* GENERICSTACK_DEFAULT_LENGTH > 0 */ |
| 232 |
|
| static GENERICSTACK_INLINE short _GENERICSTACK_CREATE_HEAP(genericStack_t *stackp, int heapi) { |
| 233 |
|
| short rcb; |
| 234 |
|
| |
| 235 |
|
| stackp->heapItems = (genericStackItem_t *) malloc(heapi * sizeof(genericStackItem_t)); |
| 236 |
|
| if (GENERICSTACK_UNLIKELY(stackp->heapItems == NULL)) { |
| 237 |
|
| stackp->errori = 1; |
| 238 |
|
| rcb = 0; |
| 239 |
|
| } else { |
| 240 |
|
| stackp->items = stackp->heapItems; |
| 241 |
|
| stackp->lengthi = stackp->heapi = heapi; |
| 242 |
|
| rcb = 1; |
| 243 |
|
| } |
| 244 |
|
| |
| 245 |
|
| return rcb; |
| 246 |
|
| } |
| 247 |
|
| # endif /* GENERICSTACK_DEFAULT_LENGTH > 0 */ |
| 248 |
|
| #else /* GENERICSTACK_ZERO_INT_IS_NOT_ZERO_BYTES */ |
| 249 |
0 |
| static GENERICSTACK_INLINE void _GENERICSTACK_NA_MEMSET(genericStack_t *stackp, int starti, int endi) { |
| 250 |
0 |
| memset(&(stackp->items[starti]), '\0', (endi - starti + 1) * sizeof(genericStackItem_t)); |
| 251 |
0 |
| } |
| 252 |
|
| # if GENERICSTACK_DEFAULT_LENGTH > 0 |
| 253 |
0 |
| static GENERICSTACK_INLINE short _GENERICSTACK_CREATE_HEAP(genericStack_t *stackp, int heapi) { |
| 254 |
|
| short rcb; |
| 255 |
|
| |
| 256 |
|
| /* No need to calloc: GENERICSTACK_NA_MEMSET() does the job when needed, and in practice this almost never happens */ |
| 257 |
0 |
| stackp->heapItems = (genericStackItem_t *) malloc(heapi * sizeof(genericStackItem_t)); |
| 258 |
0 |
▶ | if (GENERICSTACK_UNLIKELY(stackp->heapItems == NULL)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GENERICSTACK_UNLIKELY(stackp->heapItems == NULL)) { |
0 |
0 |
0 |
|
| 259 |
0 |
| stackp->errori = 1; |
| 260 |
0 |
| rcb = 0; |
| 261 |
|
| } else { |
| 262 |
0 |
| stackp->items = stackp->heapItems; |
| 263 |
0 |
▶ | if (stackp->usedi > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (stackp->usedi > 0) { |
0 |
0 |
0 |
|
| 264 |
0 |
| memcpy(stackp->items, stackp->defaultItems, stackp->usedi * sizeof(genericStackItem_t)); |
| 265 |
|
| } |
| 266 |
|
| /* No need to memset */ |
| 267 |
0 |
| stackp->lengthi = stackp->heapi = heapi; |
| 268 |
0 |
| rcb = 1; |
| 269 |
|
| } |
| 270 |
|
| |
| 271 |
0 |
| return rcb; |
| 272 |
|
| } |
| 273 |
|
| # else /* GENERICSTACK_DEFAULT_LENGTH > 0 */ |
| 274 |
|
| static GENERICSTACK_INLINE short _GENERICSTACK_CREATE_HEAP(genericStack_t *stackp, int heapi) { |
| 275 |
|
| short rcb; |
| 276 |
|
| |
| 277 |
|
| stackp->heapItems = (genericStackItem_t *) malloc(heapi * sizeof(genericStackItem_t)); |
| 278 |
|
| if (GENERICSTACK_UNLIKELY(stackp->heapItems == NULL)) { |
| 279 |
|
| stackp->errori = 1; |
| 280 |
|
| rcb = 0; |
| 281 |
|
| } else { |
| 282 |
|
| stackp->items = stackp->heapItems; |
| 283 |
|
| stackp->lengthi = stackp->heapi = heapi; |
| 284 |
|
| rcb = 1; |
| 285 |
|
| } |
| 286 |
|
| |
| 287 |
|
| return rcb; |
| 288 |
|
| } |
| 289 |
|
| # endif /* GENERICSTACK_DEFAULT_LENGTH > 0 */ |
| 290 |
|
| #endif /* GENERICSTACK_ZERO_INT_IS_NOT_ZERO_BYTES */ |
| 291 |
|
| |
| 292 |
0 |
| static GENERICSTACK_INLINE short _GENERICSTACK_EXTEND_HEAP(genericStack_t *stackp, int heapi) { |
| 293 |
|
| short rcb; |
| 294 |
|
| |
| 295 |
|
| /* We are already on the heap, so previous value is in items */ |
| 296 |
0 |
| stackp->heapItems = (genericStackItem_t *) realloc(stackp->items, heapi * sizeof(genericStackItem_t)); |
| 297 |
0 |
▶ | if (GENERICSTACK_UNLIKELY(stackp->heapItems == NULL)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GENERICSTACK_UNLIKELY(stackp->heapItems == NULL)) { |
0 |
0 |
0 |
|
| 298 |
0 |
| stackp->errori = 1; |
| 299 |
0 |
| stackp->heapItems = stackp->items; |
| 300 |
0 |
| rcb = 0; |
| 301 |
|
| } else { |
| 302 |
|
| /* Initialize new items to type GENERICSTACK_NA */ |
| 303 |
|
| /* Previous length was stackp->heapi: [0..stackp->heapi-1] */ |
| 304 |
|
| /* New length is heapi: [0..stackp->heapi.. heapi] */ |
| 305 |
0 |
| stackp->items = stackp->heapItems; |
| 306 |
0 |
| stackp->lengthi = stackp->heapi = heapi; |
| 307 |
0 |
| rcb = 1; |
| 308 |
|
| } |
| 309 |
|
| |
| 310 |
0 |
| return rcb; |
| 311 |
|
| } |
| 312 |
|
| |
| 313 |
0 |
| static GENERICSTACK_INLINE short _GENERICSTACK_EXTEND(genericStack_t *stackp, int wantedi, int targetIndicei) { |
| 314 |
|
| short rcb; |
| 315 |
|
| |
| 316 |
|
| /* Do we need to go the heap ? */ |
| 317 |
0 |
▶ | if (wantedi <= GENERICSTACK_DEFAULT_LENGTH) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (wantedi <= GENERICSTACK_DEFAULT_LENGTH) { |
0 |
0 |
0 |
|
| 318 |
0 |
| rcb = 1; |
| 319 |
|
| } else { |
| 320 |
|
| /* Is heap already large enough ? */ |
| 321 |
0 |
▶ | if (wantedi <= stackp->heapi) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (wantedi <= stackp->heapi) { |
0 |
0 |
0 |
|
| 322 |
0 |
| rcb = 1; |
| 323 |
|
| } else { |
| 324 |
|
| /* Get the multiple of 2 that is >= wantedi */ |
| 325 |
|
| int heapi; |
| 326 |
|
| #ifdef GENERICSTACK_PARANOID |
| 327 |
|
| int previousHeapi; |
| 328 |
|
| #endif |
| 329 |
|
| |
| 330 |
|
| /* https://www.geeksforgeeks.org/smallest-power-of-2-greater-than-or-equal-to-n/ */ |
| 331 |
0 |
▶ | if (!(wantedi & (wantedi - 1))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(wantedi & (wantedi - 1))) { |
0 |
0 |
0 |
|
| 332 |
0 |
| heapi = wantedi; |
| 333 |
|
| } else { |
| 334 |
|
| #ifdef GENERICSTACK_PARANOID |
| 335 |
|
| previousHeapi = |
| 336 |
|
| #endif |
| 337 |
0 |
| heapi = 1; |
| 338 |
0 |
▶ | while (heapi < wantedi) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (heapi < wantedi) { |
0 |
0 |
0 |
|
| 339 |
|
| /* We count on compiler to optimize (<<= 1, + twice etc.) */ |
| 340 |
0 |
| heapi *= 2; |
| 341 |
|
| #ifdef GENERICSTACK_PARANOID |
| 342 |
|
| if (heapi < previousHeapi) { |
| 343 |
|
| /* Turnaround */ |
| 344 |
|
| heapi = 0; |
| 345 |
|
| break; |
| 346 |
|
| } |
| 347 |
|
| previousHeapi = heapi; |
| 348 |
|
| #endif |
| 349 |
|
| } |
| 350 |
|
| } |
| 351 |
|
| |
| 352 |
0 |
▶ | if (GENERICSTACK_UNLIKELY(heapi == 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GENERICSTACK_UNLIKELY(heapi == 0)) { |
0 |
0 |
0 |
|
| 353 |
0 |
| stackp->errori = 1; |
| 354 |
0 |
| errno = EINVAL; |
| 355 |
0 |
| rcb = 0; |
| 356 |
|
| } else { |
| 357 |
0 |
▶ | rcb = (stackp->heapItems == NULL) ? _GENERICSTACK_CREATE_HEAP(stackp, heapi) : _GENERICSTACK_EXTEND_HEAP(stackp, heapi); |
Branch: 1 branch
| Branch | True | False | Total |
| rcb = (stackp->heapItems == NULL) ? _GENERICSTACK_CREATE_HEAP(stackp, heapi) : _GENERICSTACK_EXTEND_HEAP(stackp, heapi); |
0 |
0 |
0 |
|
| 358 |
|
| } |
| 359 |
|
| } |
| 360 |
|
| } |
| 361 |
|
| |
| 362 |
|
| /* Are we targetting something that is bigger than the next unused indice ? */ |
| 363 |
0 |
▶ | if (targetIndicei > stackp->usedi) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (targetIndicei > stackp->usedi) { |
0 |
0 |
0 |
|
| 364 |
|
| /* Current usage is: [0..stackp>usedi-1], user targets [0..stackp->usedi-1..targetIndicei where targetIndicei is bigger than stackp->usedi */ |
| 365 |
0 |
| _GENERICSTACK_NA_MEMSET(stackp, stackp->usedi, targetIndicei - 1); |
| 366 |
|
| } |
| 367 |
|
| |
| 368 |
0 |
| return rcb; |
| 369 |
|
| } |
| 370 |
|
| |
| 371 |
|
| #if GENERICSTACK_DEFAULT_LENGTH > 0 |
| 372 |
0 |
| static GENERICSTACK_INLINE void _GENERICSTACK_INIT(genericStack_t *stackp) { |
| 373 |
0 |
| stackp->items = stackp->defaultItems; |
| 374 |
0 |
| stackp->heapItems = NULL; |
| 375 |
0 |
| stackp->lengthi = stackp->initialLengthi = GENERICSTACK_DEFAULT_LENGTH; |
| 376 |
0 |
| stackp->errori = stackp->usedi = stackp->heapi = 0; |
| 377 |
0 |
| } |
| 378 |
|
| #else /* GENERICSTACK_DEFAULT_LENGTH > 0 */ |
| 379 |
|
| static GENERICSTACK_INLINE void _GENERICSTACK_INIT(genericStack_t *stackp) { |
| 380 |
|
| stackp->heapItems = stackp->items = NULL; |
| 381 |
|
| stackp->errori = stackp->usedi = stackp->heapi = stackp->lengthi = stackp->initialLengthi = 0; |
| 382 |
|
| } |
| 383 |
|
| #endif /* GENERICSTACK_DEFAULT_LENGTH > 0 */ |
| 384 |
|
| |
| 385 |
0 |
| static GENERICSTACK_INLINE short _GENERICSTACK_INIT_SIZED(genericStack_t *stackp, int lengthi) { |
| 386 |
|
| short rcb; |
| 387 |
|
| |
| 388 |
|
| /* Note that _GENERICSTACK_INIT() never alters stackp->errori */ |
| 389 |
0 |
| _GENERICSTACK_INIT(stackp); |
| 390 |
0 |
| rcb = _GENERICSTACK_EXTEND(stackp, lengthi, 0); |
| 391 |
0 |
▶ | if (GENERICSTACK_LIKELY(rcb)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GENERICSTACK_LIKELY(rcb)) { |
0 |
0 |
0 |
|
| 392 |
0 |
| stackp->initialLengthi = lengthi; |
| 393 |
|
| } |
| 394 |
|
| |
| 395 |
0 |
| return rcb; |
| 396 |
|
| } |
| 397 |
|
| |
| 398 |
0 |
| static GENERICSTACK_INLINE void _GENERICSTACK_RESET(genericStack_t *stackp) { |
| 399 |
0 |
▶ | if (stackp != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (stackp != NULL) { |
0 |
0 |
0 |
|
| 400 |
0 |
▶ | if (stackp->heapItems != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (stackp->heapItems != NULL) { |
0 |
0 |
0 |
|
| 401 |
0 |
| free(stackp->heapItems); |
| 402 |
0 |
| stackp->heapItems = NULL; |
| 403 |
|
| } |
| 404 |
0 |
| stackp->usedi = stackp->heapi = stackp->lengthi = 0; |
| 405 |
|
| } |
| 406 |
0 |
| } |
| 407 |
|
| |
| 408 |
0 |
| static GENERICSTACK_INLINE void _GENERICSTACK_FREE(genericStack_t *stackp) { |
| 409 |
0 |
▶ | if (stackp != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (stackp != NULL) { |
0 |
0 |
0 |
|
| 410 |
0 |
▶ | if (stackp->heapItems != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (stackp->heapItems != NULL) { |
0 |
0 |
0 |
|
| 411 |
0 |
| free(stackp->heapItems); |
| 412 |
|
| } |
| 413 |
0 |
| free(stackp); |
| 414 |
|
| } |
| 415 |
0 |
| } |
| 416 |
|
| |
| 417 |
0 |
| static GENERICSTACK_INLINE genericStack_t *_GENERICSTACK_NEW() { |
| 418 |
0 |
| genericStack_t *stackp = (genericStack_t *) malloc(sizeof(genericStack_t)); |
| 419 |
|
| |
| 420 |
0 |
▶ | if (GENERICSTACK_LIKELY(stackp != NULL)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GENERICSTACK_LIKELY(stackp != NULL)) { |
0 |
0 |
0 |
|
| 421 |
0 |
| _GENERICSTACK_INIT(stackp); |
| 422 |
|
| } |
| 423 |
|
| |
| 424 |
0 |
| return stackp; |
| 425 |
|
| } |
| 426 |
|
| |
| 427 |
0 |
| static GENERICSTACK_INLINE genericStack_t *_GENERICSTACK_NEW_SIZED(int lengthi) { |
| 428 |
0 |
| genericStack_t *stackp = (genericStack_t *) malloc(sizeof(genericStack_t)); |
| 429 |
|
| |
| 430 |
0 |
▶ | if (GENERICSTACK_LIKELY(stackp != NULL)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GENERICSTACK_LIKELY(stackp != NULL)) { |
0 |
0 |
0 |
|
| 431 |
|
| /* Note that _GENERICSTACK_INIT() never alters stackp->errori */ |
| 432 |
0 |
| _GENERICSTACK_INIT(stackp); |
| 433 |
0 |
▶ | if (GENERICSTACK_UNLIKELY(! _GENERICSTACK_EXTEND(stackp, lengthi, 0))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GENERICSTACK_UNLIKELY(! _GENERICSTACK_EXTEND(stackp, lengthi, 0))) { |
0 |
0 |
0 |
|
| 434 |
0 |
| _GENERICSTACK_FREE(stackp); |
| 435 |
0 |
| stackp = NULL; |
| 436 |
|
| } |
| 437 |
|
| } |
| 438 |
|
| |
| 439 |
0 |
| return stackp; |
| 440 |
|
| } |
| 441 |
|
| |
| 442 |
0 |
| static GENERICSTACK_INLINE void _GENERICSTACK_RELAX(genericStack_t *stackp) { |
| 443 |
0 |
▶ | if (stackp != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (stackp != NULL) { |
0 |
0 |
0 |
|
| 444 |
0 |
| stackp->usedi = 0; |
| 445 |
|
| } |
| 446 |
0 |
| } |
| 447 |
|
| |
| 448 |
|
| #ifdef GENERICSTACK_PARANOID |
| 449 |
|
| # define __GENERICSTACK_SET_BY_TYPE(varType, itemType, dst) \ |
| 450 |
|
| static GENERICSTACK_INLINE short _GENERICSTACK_SET_##itemType(genericStack_t *stackp, varType var, int indexi) { \ |
| 451 |
|
| short rcb; \ |
| 452 |
|
| int lengthi = indexi + 1; \ |
| 453 |
|
| \ |
| 454 |
|
| /* Turnaround */ \ |
| 455 |
|
| if (GENERICSTACK_UNLIKELY(lengthi < indexi)) { \ |
| 456 |
|
| stackp->errori = 1; \ |
| 457 |
|
| rcb = 0; \ |
| 458 |
|
| } else { \ |
| 459 |
|
| if (GENERICSTACK_UNLIKELY(! _GENERICSTACK_EXTEND(stackp, lengthi, indexi))) { \ |
| 460 |
|
| rcb = 0; \ |
| 461 |
|
| } else { \ |
| 462 |
|
| if (stackp->usedi < lengthi) { \ |
| 463 |
|
| stackp->usedi = lengthi; \ |
| 464 |
|
| } \ |
| 465 |
|
| stackp->items[indexi].type = GENERICSTACKITEMTYPE_##itemType; \ |
| 466 |
|
| stackp->items[indexi].u.dst = var; \ |
| 467 |
|
| rcb = 1; \ |
| 468 |
|
| } \ |
| 469 |
|
| } \ |
| 470 |
|
| \ |
| 471 |
|
| return rcb; \ |
| 472 |
|
| } |
| 473 |
|
| #else /* GENERICSTACK_PARANOID */ |
| 474 |
|
| # define __GENERICSTACK_SET_BY_TYPE(varType, itemType, dst) \ |
| 475 |
|
| static GENERICSTACK_INLINE short _GENERICSTACK_SET_##itemType(genericStack_t *stackp, varType var, int indexi) { \ |
| 476 |
|
| short rcb; \ |
| 477 |
|
| int lengthi = indexi + 1; \ |
| 478 |
|
| \ |
| 479 |
|
| if (GENERICSTACK_UNLIKELY(! _GENERICSTACK_EXTEND(stackp, lengthi, indexi))) { \ |
| 480 |
|
| rcb = 0; \ |
| 481 |
|
| } else { \ |
| 482 |
|
| if (stackp->usedi < lengthi) { \ |
| 483 |
|
| stackp->usedi = lengthi; \ |
| 484 |
|
| } \ |
| 485 |
|
| stackp->items[indexi].type = GENERICSTACKITEMTYPE_##itemType; \ |
| 486 |
|
| stackp->items[indexi].u.dst = var; \ |
| 487 |
|
| rcb = 1; \ |
| 488 |
|
| } \ |
| 489 |
|
| \ |
| 490 |
|
| return rcb; \ |
| 491 |
|
| } |
| 492 |
|
| #endif /* GENERICSTACK_PARANOID */ |
| 493 |
|
| |
| 494 |
|
| #ifdef GENERICSTACK_PARANOID |
| 495 |
|
| # define __GENERICSTACK_SET_BY_TYPEP(varType, itemType, dst) \ |
| 496 |
|
| static GENERICSTACK_INLINE short _GENERICSTACK_SET_##itemType##P(genericStack_t *stackp, varType *var, int indexi) { \ |
| 497 |
|
| short rcb; \ |
| 498 |
|
| int lengthi = indexi + 1; \ |
| 499 |
|
| \ |
| 500 |
|
| /* Turnaround */ \ |
| 501 |
|
| if (GENERICSTACK_UNLIKELY(lengthi < indexi)) { \ |
| 502 |
|
| stackp->errori = 1; \ |
| 503 |
|
| rcb = 0; \ |
| 504 |
|
| } else { \ |
| 505 |
|
| if (GENERICSTACK_UNLIKELY(! _GENERICSTACK_EXTEND(stackp, lengthi, indexi))) { \ |
| 506 |
|
| rcb = 0; \ |
| 507 |
|
| } else { \ |
| 508 |
|
| if (stackp->usedi < lengthi) { \ |
| 509 |
|
| stackp->usedi = lengthi; \ |
| 510 |
|
| } \ |
| 511 |
|
| stackp->items[indexi].type = GENERICSTACKITEMTYPE_##itemType; \ |
| 512 |
|
| stackp->items[indexi].u.dst = *var; \ |
| 513 |
|
| rcb = 1; \ |
| 514 |
|
| } \ |
| 515 |
|
| } \ |
| 516 |
|
| \ |
| 517 |
|
| return rcb; \ |
| 518 |
|
| } |
| 519 |
|
| #else /* GENERICSTACK_PARANOID */ |
| 520 |
|
| # define __GENERICSTACK_SET_BY_TYPEP(varType, itemType, dst) \ |
| 521 |
|
| static GENERICSTACK_INLINE short _GENERICSTACK_SET_##itemType##P(genericStack_t *stackp, varType *var, int indexi) { \ |
| 522 |
|
| short rcb; \ |
| 523 |
|
| int lengthi = indexi + 1; \ |
| 524 |
|
| \ |
| 525 |
|
| if (GENERICSTACK_UNLIKELY(! _GENERICSTACK_EXTEND(stackp, lengthi, indexi))) { \ |
| 526 |
|
| rcb = 0; \ |
| 527 |
|
| } else { \ |
| 528 |
|
| if (stackp->usedi < lengthi) { \ |
| 529 |
|
| stackp->usedi = lengthi; \ |
| 530 |
|
| } \ |
| 531 |
|
| stackp->items[indexi].type = GENERICSTACKITEMTYPE_##itemType; \ |
| 532 |
|
| stackp->items[indexi].u.dst = *var; \ |
| 533 |
|
| rcb = 1; \ |
| 534 |
|
| } \ |
| 535 |
|
| \ |
| 536 |
|
| return rcb; \ |
| 537 |
|
| } |
| 538 |
|
| #endif /* GENERICSTACK_PARANOID */ |
| 539 |
|
| |
| 540 |
0 |
▶ | __GENERICSTACK_SET_BY_TYPE(char, CHAR, c) |
Branch: 2 branches
| Branch | True | False | Total |
| __GENERICSTACK_SET_BY_TYPE(char, CHAR, c) |
0 |
0 |
0 |
| __GENERICSTACK_SET_BY_TYPE(char, CHAR, c) |
0 |
0 |
0 |
|
| 541 |
0 |
▶ | __GENERICSTACK_SET_BY_TYPE(short, SHORT, s) |
Branch: 2 branches
| Branch | True | False | Total |
| __GENERICSTACK_SET_BY_TYPE(short, SHORT, s) |
0 |
0 |
0 |
| __GENERICSTACK_SET_BY_TYPE(short, SHORT, s) |
0 |
0 |
0 |
|
| 542 |
0 |
▶ | __GENERICSTACK_SET_BY_TYPE(int, INT, i) |
Branch: 2 branches
| Branch | True | False | Total |
| __GENERICSTACK_SET_BY_TYPE(int, INT, i) |
0 |
0 |
0 |
| __GENERICSTACK_SET_BY_TYPE(int, INT, i) |
0 |
0 |
0 |
|
| 543 |
0 |
▶ | __GENERICSTACK_SET_BY_TYPE(long, LONG, l) |
Branch: 2 branches
| Branch | True | False | Total |
| __GENERICSTACK_SET_BY_TYPE(long, LONG, l) |
0 |
0 |
0 |
| __GENERICSTACK_SET_BY_TYPE(long, LONG, l) |
0 |
0 |
0 |
|
| 544 |
0 |
▶ | __GENERICSTACK_SET_BY_TYPE(long double, LONG_DOUBLE, ld) |
Branch: 2 branches
| Branch | True | False | Total |
| __GENERICSTACK_SET_BY_TYPE(long double, LONG_DOUBLE, ld) |
0 |
0 |
0 |
| __GENERICSTACK_SET_BY_TYPE(long double, LONG_DOUBLE, ld) |
0 |
0 |
0 |
|
| 545 |
0 |
▶ | __GENERICSTACK_SET_BY_TYPE(float, FLOAT, f) |
Branch: 2 branches
| Branch | True | False | Total |
| __GENERICSTACK_SET_BY_TYPE(float, FLOAT, f) |
0 |
0 |
0 |
| __GENERICSTACK_SET_BY_TYPE(float, FLOAT, f) |
0 |
0 |
0 |
|
| 546 |
0 |
▶ | __GENERICSTACK_SET_BY_TYPE(double, DOUBLE, d) |
Branch: 2 branches
| Branch | True | False | Total |
| __GENERICSTACK_SET_BY_TYPE(double, DOUBLE, d) |
0 |
0 |
0 |
| __GENERICSTACK_SET_BY_TYPE(double, DOUBLE, d) |
0 |
0 |
0 |
|
| 547 |
0 |
▶ | __GENERICSTACK_SET_BY_TYPE(void *, PTR, p) |
Branch: 2 branches
| Branch | True | False | Total |
| __GENERICSTACK_SET_BY_TYPE(void *, PTR, p) |
0 |
0 |
0 |
| __GENERICSTACK_SET_BY_TYPE(void *, PTR, p) |
0 |
0 |
0 |
|
| 548 |
0 |
▶ | __GENERICSTACK_SET_BY_TYPE(genericStackItemTypeArray_t, ARRAY, a) |
Branch: 2 branches
| Branch | True | False | Total |
| __GENERICSTACK_SET_BY_TYPE(genericStackItemTypeArray_t, ARRAY, a) |
0 |
0 |
0 |
| __GENERICSTACK_SET_BY_TYPE(genericStackItemTypeArray_t, ARRAY, a) |
0 |
0 |
0 |
|
| 549 |
0 |
▶ | __GENERICSTACK_SET_BY_TYPEP(genericStackItemTypeArray_t, ARRAY, a) |
Branch: 2 branches
| Branch | True | False | Total |
| __GENERICSTACK_SET_BY_TYPEP(genericStackItemTypeArray_t, ARRAY, a) |
0 |
0 |
0 |
| __GENERICSTACK_SET_BY_TYPEP(genericStackItemTypeArray_t, ARRAY, a) |
0 |
0 |
0 |
|
| 550 |
|
| #if GENERICSTACK_HAVE_LONG_LONG > 0 |
| 551 |
|
| __GENERICSTACK_SET_BY_TYPE(long long, LONG_LONG, ll) |
| 552 |
|
| #endif |
| 553 |
|
| #if GENERICSTACK_HAVE__BOOL > 0 |
| 554 |
|
| __GENERICSTACK_SET_BY_TYPE(_Bool, _BOOL, b) |
| 555 |
|
| #endif |
| 556 |
|
| #if GENERICSTACK_HAVE__COMPLEX > 0 |
| 557 |
|
| __GENERICSTACK_SET_BY_TYPE(float _Complex, FLOAT__COMPLEX, fc) |
| 558 |
|
| __GENERICSTACK_SET_BY_TYPE(double _Complex, DOUBLE__COMPLEX, dc) |
| 559 |
|
| __GENERICSTACK_SET_BY_TYPE(long double _Complex, LONG_DOUBLE__COMPLEX, ldc) |
| 560 |
|
| #endif |
| 561 |
|
| #if GENERICSTACK_HAVE_CUSTOM > 0 |
| 562 |
|
| __GENERICSTACK_SET_BY_TYPE(GENERICSTACK_CUSTOM, CUSTOM, custom) |
| 563 |
|
| __GENERICSTACK_SET_BY_TYPEP(GENERICSTACK_CUSTOM, CUSTOM, custom) |
| 564 |
|
| #endif |
| 565 |
|
| /* Special case for N/A: there is no associated data */ |
| 566 |
0 |
| static GENERICSTACK_INLINE short _GENERICSTACK_SET_NA(genericStack_t *stackp, int indexi) { |
| 567 |
|
| short rcb; |
| 568 |
0 |
| int lengthi = indexi + 1; |
| 569 |
|
| |
| 570 |
|
| #ifdef GENERICSTACK_PARANOID |
| 571 |
|
| /* Turnaround */ |
| 572 |
|
| if (GENERICSTACK_UNLIKELY(lengthi < indexi)) { |
| 573 |
|
| stackp->errori = 1; |
| 574 |
|
| rcb = 0; |
| 575 |
|
| } else { |
| 576 |
|
| #endif |
| 577 |
0 |
▶ | if (GENERICSTACK_UNLIKELY(! _GENERICSTACK_EXTEND(stackp, lengthi, indexi))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GENERICSTACK_UNLIKELY(! _GENERICSTACK_EXTEND(stackp, lengthi, indexi))) { |
0 |
0 |
0 |
|
| 578 |
0 |
| rcb = 0; |
| 579 |
|
| } else { |
| 580 |
0 |
▶ | if (stackp->usedi < lengthi) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (stackp->usedi < lengthi) { |
0 |
0 |
0 |
|
| 581 |
0 |
| stackp->usedi = lengthi; |
| 582 |
|
| } |
| 583 |
0 |
| stackp->items[indexi].type = GENERICSTACKITEMTYPE_NA; |
| 584 |
0 |
| rcb = 1; |
| 585 |
|
| } |
| 586 |
|
| #ifdef GENERICSTACK_PARANOID |
| 587 |
|
| } |
| 588 |
|
| #endif |
| 589 |
|
| |
| 590 |
0 |
| return rcb; |
| 591 |
|
| } |
| 592 |
|
| |
| 593 |
|
| /* ====================================================================== */ |
| 594 |
|
| /* Interface. */ |
| 595 |
|
| /* ====================================================================== */ |
| 596 |
|
| #define GENERICSTACK_ERROR(stackp) ((stackp == NULL) || (stackp)->errori != 0) |
| 597 |
|
| #define GENERICSTACK_ERROR_RESET(stackp) (stackp)->errori = 0 |
| 598 |
|
| #define GENERICSTACK_INITIAL_LENGTH(stackp) (stackp)->initialLengthi |
| 599 |
|
| #define GENERICSTACK_HEAP_LENGTH(stackp) (stackp)->heapi |
| 600 |
|
| #define GENERICSTACK_LENGTH(stackp) (stackp)->lengthi |
| 601 |
|
| #define GENERICSTACK_USED(stackp) (stackp)->usedi |
| 602 |
|
| #define GENERICSTACK_INIT(stackp) _GENERICSTACK_INIT(stackp) |
| 603 |
|
| #define GENERICSTACK_NEW(stackp) (stackp) = _GENERICSTACK_NEW() |
| 604 |
|
| #define GENERICSTACK_NEW_SIZED(stackp, lengthi) (stackp) = _GENERICSTACK_NEW_SIZED(lengthi) |
| 605 |
|
| #define GENERICSTACK_INIT_SIZED(stackp, lengthi) _GENERICSTACK_INIT_SIZED(stackp, lengthi) |
| 606 |
|
| |
| 607 |
|
| /* ====================================================================== */ |
| 608 |
|
| /* GET interface */ |
| 609 |
|
| /* ====================================================================== */ |
| 610 |
|
| #define GENERICSTACK_GET_CHAR(stackp, indexi) ((stackp)->items[indexi].u.c) |
| 611 |
|
| #define GENERICSTACK_GET_SHORT(stackp, indexi) ((stackp)->items[indexi].u.s) |
| 612 |
|
| #define GENERICSTACK_GET_INT(stackp, indexi) ((stackp)->items[indexi].u.i) |
| 613 |
|
| #define GENERICSTACK_GET_LONG(stackp, indexi) ((stackp)->items[indexi].u.l) |
| 614 |
|
| #define GENERICSTACK_GET_LONG_DOUBLE(stackp, indexi) ((stackp)->items[indexi].u.ld) |
| 615 |
|
| #define GENERICSTACK_GET_FLOAT(stackp, indexi) ((stackp)->items[indexi].u.f) |
| 616 |
|
| #define GENERICSTACK_GET_DOUBLE(stackp, indexi) ((stackp)->items[indexi].u.d) |
| 617 |
|
| #define GENERICSTACK_GET_PTR(stackp, indexi) ((stackp)->items[indexi].u.p) |
| 618 |
|
| #define GENERICSTACK_GET_ARRAY(stackp, indexi) ((stackp)->items[indexi].u.a) |
| 619 |
|
| #define GENERICSTACK_GET_ARRAYP(stackp, indexi) (&((stackp)->items[indexi].u.a)) |
| 620 |
|
| #if GENERICSTACK_HAVE_LONG_LONG > 0 |
| 621 |
|
| # define GENERICSTACK_GET_LONG_LONG(stackp, indexi) ((stackp)->items[indexi].u.ll) |
| 622 |
|
| #endif |
| 623 |
|
| #if GENERICSTACK_HAVE__BOOL > 0 |
| 624 |
|
| # define GENERICSTACK_GET__BOOL(stackp, indexi) ((stackp)->items[indexi].u.b) |
| 625 |
|
| #endif |
| 626 |
|
| #if GENERICSTACK_HAVE__COMPLEX > 0 |
| 627 |
|
| # define GENERICSTACK_GET_FLOAT__COMPLEX(stackp, indexi) ((stackp)->items[indexi].u.fc) |
| 628 |
|
| # define GENERICSTACK_GET_DOUBLE__COMPLEX(stackp, indexi) ((stackp)->items[indexi].u.dc) |
| 629 |
|
| # define GENERICSTACK_GET_LONG_DOUBLE__COMPLEX(stackp, indexi) ((stackp)->items[indexi].u.ldc) |
| 630 |
|
| #endif |
| 631 |
|
| #if GENERICSTACK_HAVE_CUSTOM > 0 |
| 632 |
|
| # define GENERICSTACK_GET_CUSTOM(stackp, indexi) ((stackp)->items[indexi].u.custom) |
| 633 |
|
| # define GENERICSTACK_GET_CUSTOMP(stackp, indexi) (&((stackp)->items[indexi].u.custom)) |
| 634 |
|
| #endif |
| 635 |
|
| /* Per def N/A value is undefined - we just have to make sure indexi is processed */ |
| 636 |
|
| #define GENERICSTACK_GET_NA(stackp, indexi) (indexi) |
| 637 |
|
| |
| 638 |
|
| /* ====================================================================== */ |
| 639 |
|
| /* SET interface */ |
| 640 |
|
| /* ====================================================================== */ |
| 641 |
|
| #define GENERICSTACK_SET_CHAR(stackp, var, index) _GENERICSTACK_SET_CHAR(stackp, var, index) |
| 642 |
|
| #define GENERICSTACK_SET_SHORT(stackp, var, index) _GENERICSTACK_SET_SHORT(stackp, var, index) |
| 643 |
|
| #define GENERICSTACK_SET_INT(stackp, var, index) _GENERICSTACK_SET_INT(stackp, var, index) |
| 644 |
|
| #define GENERICSTACK_SET_LONG(stackp, var, index) _GENERICSTACK_SET_LONG(stackp, var, index) |
| 645 |
|
| #define GENERICSTACK_SET_LONG_DOUBLE(stackp, var, index) _GENERICSTACK_SET_LONG_DOUBLE(stackp, var, index) |
| 646 |
|
| #define GENERICSTACK_SET_FLOAT(stackp, var, index) _GENERICSTACK_SET_FLOAT(stackp, var, index) |
| 647 |
|
| #define GENERICSTACK_SET_DOUBLE(stackp, var, index) _GENERICSTACK_SET_DOUBLE(stackp, var, index) |
| 648 |
|
| #define GENERICSTACK_SET_PTR(stackp, var, index) _GENERICSTACK_SET_PTR(stackp, var, index) |
| 649 |
|
| #define GENERICSTACK_SET_ARRAY(stackp, var, index) _GENERICSTACK_SET_ARRAY(stackp, var, index) |
| 650 |
|
| #define GENERICSTACK_SET_ARRAYP(stackp, var, index) _GENERICSTACK_SET_ARRAYP(stackp, var, index) |
| 651 |
|
| #if GENERICSTACK_HAVE_LONG_LONG > 0 |
| 652 |
|
| # define GENERICSTACK_SET_LONG_LONG(stackp, var, index) _GENERICSTACK_SET_LONG_LONG(stackp, var, index) |
| 653 |
|
| #endif |
| 654 |
|
| #if GENERICSTACK_HAVE__BOOL > 0 |
| 655 |
|
| # define GENERICSTACK_SET__BOOL(stackp, var, index) _GENERICSTACK_SET_BOOL(stackp, var, index) |
| 656 |
|
| #endif |
| 657 |
|
| #if GENERICSTACK_HAVE__COMPLEX > 0 |
| 658 |
|
| # define GENERICSTACK_SET_FLOAT__COMPLEX(stackp, var, index) _GENERICSTACK_SET_FLOAT__COMPLEX(stackp, var, index) |
| 659 |
|
| # define GENERICSTACK_SET_DOUBLE__COMPLEX(stackp, var, index) _GENERICSTACK_SET_DOUBLE__COMPLEX(stackp, var, index) |
| 660 |
|
| # define GENERICSTACK_SET_LONG_DOUBLE__COMPLEX(stackp, var, index) _GENERICSTACK_SET_LONG_DOUBLE__COMPLEX(stackp, var, index) |
| 661 |
|
| #endif |
| 662 |
|
| #if GENERICSTACK_HAVE_CUSTOM > 0 |
| 663 |
|
| # define GENERICSTACK_SET_CUSTOM(stackp, var, index) _GENERICSTACK_SET_CUSTOM(stackp, var, index) |
| 664 |
|
| # define GENERICSTACK_SET_CUSTOMP(stackp, var, index) _GENERICSTACK_SET_CUSTOMP(stackp, var, index) |
| 665 |
|
| #endif |
| 666 |
|
| #define GENERICSTACK_SET_NA(stackp, index) _GENERICSTACK_SET_NA(stackp, index) |
| 667 |
|
| |
| 668 |
|
| /* ====================================================================== */ |
| 669 |
|
| /* PUSH interface: built on top of SET */ |
| 670 |
|
| /* ====================================================================== */ |
| 671 |
|
| #define GENERICSTACK_PUSH_CHAR(stackp, var) GENERICSTACK_SET_CHAR(stackp, var, (stackp)->usedi) |
| 672 |
|
| #define GENERICSTACK_PUSH_SHORT(stackp, var) GENERICSTACK_SET_SHORT(stackp, var, (stackp)->usedi) |
| 673 |
|
| #define GENERICSTACK_PUSH_INT(stackp, var) GENERICSTACK_SET_INT(stackp, var, (stackp)->usedi) |
| 674 |
|
| #define GENERICSTACK_PUSH_LONG(stackp, var) GENERICSTACK_SET_LONG(stackp, var, (stackp)->usedi) |
| 675 |
|
| #define GENERICSTACK_PUSH_LONG_DOUBLE(stackp, var) GENERICSTACK_SET_LONG_DOUBLE(stackp, var, (stackp)->usedi) |
| 676 |
|
| #define GENERICSTACK_PUSH_FLOAT(stackp, var) GENERICSTACK_SET_FLOAT(stackp, var, (stackp)->usedi) |
| 677 |
|
| #define GENERICSTACK_PUSH_DOUBLE(stackp, var) GENERICSTACK_SET_DOUBLE(stackp, var, (stackp)->usedi) |
| 678 |
|
| #define GENERICSTACK_PUSH_PTR(stackp, var) GENERICSTACK_SET_PTR(stackp, var, (stackp)->usedi) |
| 679 |
|
| #define GENERICSTACK_PUSH_ARRAY(stackp, var) GENERICSTACK_SET_ARRAY(stackp, var, (stackp)->usedi) |
| 680 |
|
| #define GENERICSTACK_PUSH_ARRAYP(stackp, var) GENERICSTACK_SET_ARRAYP(stackp, var, (stackp)->usedi) |
| 681 |
|
| #if GENERICSTACK_HAVE_LONG_LONG > 0 |
| 682 |
|
| # define GENERICSTACK_PUSH_LONG_LONG(stackp, var) GENERICSTACK_SET_LONG_LONG(stackp, var, (stackp)->usedi) |
| 683 |
|
| #endif |
| 684 |
|
| #if GENERICSTACK_HAVE__BOOL > 0 |
| 685 |
|
| # define GENERICSTACK_PUSH__BOOL(stackp, var) GENERICSTACK_SET__BOOL(stackp, var, (stackp)->usedi) |
| 686 |
|
| #endif |
| 687 |
|
| #if GENERICSTACK_HAVE__COMPLEX > 0 |
| 688 |
|
| # define GENERICSTACK_PUSH_FLOAT__COMPLEX(stackp, var) GENERICSTACK_SET_FLOAT__COMPLEX(stackp, var, (stackp)->usedi) |
| 689 |
|
| # define GENERICSTACK_PUSH_DOUBLE__COMPLEX(stackp, var) GENERICSTACK_SET_DOUBLE__COMPLEX(stackp, var, (stackp)->usedi) |
| 690 |
|
| # define GENERICSTACK_PUSH_LONG_DOUBLE__COMPLEX(stackp, var) GENERICSTACK_SET_LONG_DOUBLE__COMPLEX(stackp, var, (stackp)->usedi) |
| 691 |
|
| #endif |
| 692 |
|
| #if GENERICSTACK_HAVE_CUSTOM > 0 |
| 693 |
|
| # define GENERICSTACK_PUSH_CUSTOM(stackp, var) GENERICSTACK_SET_CUSTOM(stackp, var, (stackp)->usedi) |
| 694 |
|
| # define GENERICSTACK_PUSH_CUSTOMP(stackp, var) GENERICSTACK_SET_CUSTOMP(stackp, var, (stackp)->usedi) |
| 695 |
|
| #endif |
| 696 |
|
| #define GENERICSTACK_PUSH_NA(stackp) GENERICSTACK_SET_NA(stackp, (stackp)->usedi) |
| 697 |
|
| |
| 698 |
|
| /* ====================================================================== */ |
| 699 |
|
| /* POP interface: built on top GET */ |
| 700 |
|
| /* ====================================================================== */ |
| 701 |
|
| #define GENERICSTACK_POP_CHAR(stackp) GENERICSTACK_GET_CHAR(stackp, --(stackp)->usedi) |
| 702 |
|
| #define GENERICSTACK_POP_SHORT(stackp) GENERICSTACK_GET_SHORT(stackp, --(stackp)->usedi) |
| 703 |
|
| #define GENERICSTACK_POP_INT(stackp) GENERICSTACK_GET_INT(stackp, --(stackp)->usedi) |
| 704 |
|
| #define GENERICSTACK_POP_LONG(stackp) GENERICSTACK_GET_LONG(stackp, --(stackp)->usedi) |
| 705 |
|
| #define GENERICSTACK_POP_LONG_DOUBLE(stackp) GENERICSTACK_GET_LONG_DOUBLE(stackp, --(stackp)->usedi) |
| 706 |
|
| #define GENERICSTACK_POP_FLOAT(stackp) GENERICSTACK_GET_FLOAT(stackp, --(stackp)->usedi) |
| 707 |
|
| #define GENERICSTACK_POP_DOUBLE(stackp) GENERICSTACK_GET_DOUBLE(stackp, --(stackp)->usedi) |
| 708 |
|
| #define GENERICSTACK_POP_PTR(stackp) GENERICSTACK_GET_PTR(stackp, --(stackp)->usedi) |
| 709 |
|
| #define GENERICSTACK_POP_ARRAY(stackp) GENERICSTACK_GET_ARRAY(stackp,--(stackp)->usedi) |
| 710 |
|
| #if GENERICSTACK_HAVE_LONG_LONG > 0 |
| 711 |
|
| # define GENERICSTACK_POP_LONG_LONG(stackp) GENERICSTACK_GET_LONG_LONG(stackp, --(stackp)->usedi) |
| 712 |
|
| #endif |
| 713 |
|
| #if GENERICSTACK_HAVE__BOOL > 0 |
| 714 |
|
| # define GENERICSTACK_POP__BOOL(stackp) GENERICSTACK_GET__BOOL(stackp, --(stackp)->usedi) |
| 715 |
|
| #endif |
| 716 |
|
| #if GENERICSTACK_HAVE__COMPLEX > 0 |
| 717 |
|
| # define GENERICSTACK_POP_FLOAT__COMPLEX(stackp) GENERICSTACK_GET_FLOAT__COMPLEX(stackp, --(stackp)->usedi) |
| 718 |
|
| # define GENERICSTACK_POP_DOUBLE__COMPLEX(stackp) GENERICSTACK_GET_DOUBLE__COMPLEX(stackp, --(stackp)->usedi) |
| 719 |
|
| # define GENERICSTACK_POP_LONG_DOUBLE__COMPLEX(stackp) GENERICSTACK_GET_LONG_DOUBLE__COMPLEX(stackp, --(stackp)->usedi) |
| 720 |
|
| #endif |
| 721 |
|
| #if GENERICSTACK_HAVE_CUSTOM > 0 |
| 722 |
|
| # define GENERICSTACK_POP_CUSTOM(stackp) GENERICSTACK_GET_CUSTOM(stackp, --(stackp)->usedi) |
| 723 |
|
| #endif |
| 724 |
|
| #define GENERICSTACK_POP_NA(stackp) GENERICSTACK_GET_NA(stackp, --(stackp)->usedi) |
| 725 |
|
| |
| 726 |
|
| /* ====================================================================== */ |
| 727 |
|
| /* Memory release */ |
| 728 |
|
| /* We intentionnaly loop on size and not used. */ |
| 729 |
|
| /* ====================================================================== */ |
| 730 |
|
| #define GENERICSTACK_RESET(stackp) _GENERICSTACK_RESET(stackp) |
| 731 |
|
| #define GENERICSTACK_FREE(stackp) _GENERICSTACK_FREE(stackp) |
| 732 |
|
| #define GENERICSTACK_RELAX(stackp) _GENERICSTACK_RELAX(stackp) |
| 733 |
|
| |
| 734 |
|
| /* ====================================================================== */ |
| 735 |
|
| /* In some rare occasions user might want to get the basic type */ |
| 736 |
|
| /* from an item type. */ |
| 737 |
|
| /* ====================================================================== */ |
| 738 |
|
| #define GENERICSTACKITEMTYPE(stackp, index) (stackp)->items[index].type |
| 739 |
|
| |
| 740 |
|
| #define GENERICSTACKITEMTYPE2TYPE_CHAR char |
| 741 |
|
| #define GENERICSTACKITEMTYPE2TYPE_SHORT short |
| 742 |
|
| #define GENERICSTACKITEMTYPE2TYPE_INT int |
| 743 |
|
| #define GENERICSTACKITEMTYPE2TYPE_LONG long |
| 744 |
|
| #define GENERICSTACKITEMTYPE2TYPE_LONG_DOUBLE long double |
| 745 |
|
| #define GENERICSTACKITEMTYPE2TYPE_FLOAT float |
| 746 |
|
| #define GENERICSTACKITEMTYPE2TYPE_DOUBLE double |
| 747 |
|
| #define GENERICSTACKITEMTYPE2TYPE_PTR void * |
| 748 |
|
| #define GENERICSTACKITEMTYPE2TYPE_ARRAY genericStackItemTypeArray_t |
| 749 |
|
| #define GENERICSTACKITEMTYPE2TYPE_ARRAYP genericStackItemTypeArray_t * |
| 750 |
|
| #define GENERICSTACK_ARRAY_PTR(a) (a).p |
| 751 |
|
| #define GENERICSTACK_ARRAYP_PTR(a) (a)->p |
| 752 |
|
| #define GENERICSTACK_ARRAY_LENGTH(a) (a).lengthl |
| 753 |
|
| #define GENERICSTACK_ARRAYP_LENGTH(a) (a)->lengthl |
| 754 |
|
| #if GENERICSTACK_HAVE_LONG_LONG |
| 755 |
|
| #define GENERICSTACKITEMTYPE2TYPE_LONG_LONG long long |
| 756 |
|
| #endif |
| 757 |
|
| #if GENERICSTACK_HAVE__BOOL |
| 758 |
|
| #define GENERICSTACKITEMTYPE2TYPE__BOOL _Bool |
| 759 |
|
| #endif |
| 760 |
|
| #if GENERICSTACK_HAVE__COMPLEX |
| 761 |
|
| #define GENERICSTACKITEMTYPE2TYPE_FLOAT__COMPLEX float _Complex |
| 762 |
|
| #define GENERICSTACKITEMTYPE2TYPE_DOUBLE__COMPLEX double _Complex |
| 763 |
|
| #define GENERICSTACKITEMTYPE2TYPE_LONG_DOUBLE__COMPLEX long double _Complex |
| 764 |
|
| #endif |
| 765 |
|
| #if GENERICSTACK_HAVE_CUSTOM |
| 766 |
|
| #define GENERICSTACKITEMTYPE2TYPE_CUSTOM GENERICSTACK_CUSTOM |
| 767 |
|
| #define GENERICSTACKITEMTYPE2TYPE_CUSTOMP GENERICSTACK_CUSTOM * |
| 768 |
|
| #endif |
| 769 |
|
| |
| 770 |
|
| /* ====================================================================== */ |
| 771 |
|
| /* IS interface */ |
| 772 |
|
| /* ====================================================================== */ |
| 773 |
|
| #define GENERICSTACK_EXISTS(stackp, i) (((stackp) != NULL) && ((stackp)->usedi > i)) |
| 774 |
|
| #define GENERICSTACK_IS_NA(stackp, i) (GENERICSTACK_EXISTS(stackp, i) && (GENERICSTACKITEMTYPE((stackp), (i)) == GENERICSTACKITEMTYPE_NA)) |
| 775 |
|
| #define GENERICSTACK_IS_CHAR(stackp, i) (GENERICSTACK_EXISTS(stackp, i) && (GENERICSTACKITEMTYPE((stackp), (i)) == GENERICSTACKITEMTYPE_CHAR)) |
| 776 |
|
| #define GENERICSTACK_IS_SHORT(stackp, i) (GENERICSTACK_EXISTS(stackp, i) && (GENERICSTACKITEMTYPE((stackp), (i)) == GENERICSTACKITEMTYPE_SHORT)) |
| 777 |
|
| #define GENERICSTACK_IS_INT(stackp, i) (GENERICSTACK_EXISTS(stackp, i) && (GENERICSTACKITEMTYPE((stackp), (i)) == GENERICSTACKITEMTYPE_INT)) |
| 778 |
|
| #define GENERICSTACK_IS_LONG(stackp, i) (GENERICSTACK_EXISTS(stackp, i) && (GENERICSTACKITEMTYPE((stackp), (i)) == GENERICSTACKITEMTYPE_LONG)) |
| 779 |
|
| #define GENERICSTACK_IS_LONG_DOUBLE(stackp, i) (GENERICSTACK_EXISTS(stackp, i) && (GENERICSTACKITEMTYPE((stackp), (i)) == GENERICSTACKITEMTYPE_LONG_DOUBLE)) |
| 780 |
|
| #define GENERICSTACK_IS_FLOAT(stackp, i) (GENERICSTACK_EXISTS(stackp, i) && (GENERICSTACKITEMTYPE((stackp), (i)) == GENERICSTACKITEMTYPE_FLOAT)) |
| 781 |
|
| #define GENERICSTACK_IS_DOUBLE(stackp, i) (GENERICSTACK_EXISTS(stackp, i) && (GENERICSTACKITEMTYPE((stackp), (i)) == GENERICSTACKITEMTYPE_DOUBLE)) |
| 782 |
|
| #define GENERICSTACK_IS_PTR(stackp, i) (GENERICSTACK_EXISTS(stackp, i) && (GENERICSTACKITEMTYPE((stackp), (i)) == GENERICSTACKITEMTYPE_PTR)) |
| 783 |
|
| #define GENERICSTACK_IS_ARRAY(stackp, i) (GENERICSTACK_EXISTS(stackp, i) && (GENERICSTACKITEMTYPE((stackp), (i)) == GENERICSTACKITEMTYPE_ARRAY)) |
| 784 |
|
| #if GENERICSTACK_HAVE_LONG_LONG |
| 785 |
|
| #define GENERICSTACK_IS_LONG_LONG(stackp, i) (GENERICSTACK_EXISTS(stackp, i) && (GENERICSTACKITEMTYPE((stackp), (i)) == GENERICSTACKITEMTYPE_LONG_LONG)) |
| 786 |
|
| #endif |
| 787 |
|
| #if GENERICSTACK_HAVE__BOOL |
| 788 |
|
| #define GENERICSTACK_IS__BOOL(stackp, i) (GENERICSTACK_EXISTS(stackp, i) && (GENERICSTACKITEMTYPE((stackp), (i)) == GENERICSTACKITEMTYPE__BOOL)) |
| 789 |
|
| #endif |
| 790 |
|
| #if GENERICSTACK_HAVE__COMPLEX |
| 791 |
|
| #define GENERICSTACK_IS_FLOAT__COMPLEX(stackp, i) (GENERICSTACK_EXISTS(stackp, i) && (GENERICSTACKITEMTYPE((stackp), (i)) == GENERICSTACKITEMTYPE_FLOAT__COMPLEX)) |
| 792 |
|
| #define GENERICSTACK_IS_DOUBLE__COMPLEX(stackp, i) (GENERICSTACK_EXISTS(stackp, i) && (GENERICSTACKITEMTYPE((stackp), (i)) == GENERICSTACKITEMTYPE_DOUBLE__COMPLEX)) |
| 793 |
|
| #define GENERICSTACK_IS_LONG_DOUBLE__COMPLEX(stackp, i) (GENERICSTACK_EXISTS(stackp, i) && (GENERICSTACKITEMTYPE((stackp), (i)) == GENERICSTACKITEMTYPE_LONG_DOUBLE__COMPLEX)) |
| 794 |
|
| #endif |
| 795 |
|
| #if GENERICSTACK_HAVE_CUSTOM |
| 796 |
|
| #define GENERICSTACK_IS_CUSTOM(stackp, i) (GENERICSTACK_EXISTS(stackp, i) && (GENERICSTACKITEMTYPE((stackp), (i)) == GENERICSTACKITEMTYPE_CUSTOM)) |
| 797 |
|
| #endif |
| 798 |
|
| |
| 799 |
|
| /* ====================================================================== */ |
| 800 |
|
| /* Dump macro for development purpose. Fixed to stderr. */ |
| 801 |
|
| /* ====================================================================== */ |
| 802 |
|
| #if GENERICSTACK_HAVE_CUSTOM |
| 803 |
|
| #define _GENERICSTACK_DUMP_CASE_CUSTOM(stackp,indice) case GENERICSTACKITEMTYPE_CUSTOM: fprintf(stderr, "Element[%3ld/%3ld] type : CUSTOM\n", (unsigned long) indice, (unsigned long) GENERICSTACK_USED(stackp)); break; |
| 804 |
|
| #else |
| 805 |
|
| #define _GENERICSTACK_DUMP_CASE_CUSTOM(stackp,indice) |
| 806 |
|
| #endif |
| 807 |
|
| #define GENERICSTACK_DUMP(stackp) do { \ |
| 808 |
|
| int _i_for_dump; \ |
| 809 |
|
| fprintf(stderr, "GENERIC STACK DUMP\n"); \ |
| 810 |
|
| fprintf(stderr, "------------------\n"); \ |
| 811 |
|
| fprintf(stderr, "Items : %p\n", stackp->items); \ |
| 812 |
|
| fprintf(stderr, "Heap items : %p\n", stackp->heapItems); \ |
| 813 |
|
| fprintf(stderr, "Initial Length : %d\n", GENERICSTACK_INITIAL_LENGTH(stackp)); \ |
| 814 |
|
| fprintf(stderr, "Heap Length : %d\n", GENERICSTACK_HEAP_LENGTH(stackp)); \ |
| 815 |
|
| fprintf(stderr, "Length : %d\n", GENERICSTACK_LENGTH(stackp)); \ |
| 816 |
|
| fprintf(stderr, "Used: : %d\n", GENERICSTACK_USED(stackp)); \ |
| 817 |
|
| fprintf(stderr, "Error? : %s\n", GENERICSTACK_ERROR(stackp) ? "yes" : "no"); \ |
| 818 |
|
| for (_i_for_dump = 0; _i_for_dump < GENERICSTACK_USED(stackp); _i_for_dump++) { \ |
| 819 |
|
| switch(GENERICSTACKITEMTYPE(stackp, _i_for_dump)) { \ |
| 820 |
|
| case GENERICSTACKITEMTYPE_NA: \ |
| 821 |
|
| fprintf(stderr, "Element[%3d/%3d] type : NA\n", _i_for_dump, GENERICSTACK_USED(stackp)); \ |
| 822 |
|
| break; \ |
| 823 |
|
| case GENERICSTACKITEMTYPE_CHAR: \ |
| 824 |
|
| fprintf(stderr, "Element[%3d/%3d] type : CHAR\n", _i_for_dump, GENERICSTACK_USED(stackp)); \ |
| 825 |
|
| break; \ |
| 826 |
|
| case GENERICSTACKITEMTYPE_SHORT: \ |
| 827 |
|
| fprintf(stderr, "Element[%3d/%3d] type : SHORT\n", _i_for_dump, GENERICSTACK_USED(stackp)); \ |
| 828 |
|
| break; \ |
| 829 |
|
| case GENERICSTACKITEMTYPE_INT: \ |
| 830 |
|
| fprintf(stderr, "Element[%3d/%3d] type : INT\n", _i_for_dump, GENERICSTACK_USED(stackp)); \ |
| 831 |
|
| break; \ |
| 832 |
|
| case GENERICSTACKITEMTYPE_LONG: \ |
| 833 |
|
| fprintf(stderr, "Element[%3d/%3d] type : LONG\n", _i_for_dump, GENERICSTACK_USED(stackp)); \ |
| 834 |
|
| break; \ |
| 835 |
|
| case GENERICSTACKITEMTYPE_FLOAT: \ |
| 836 |
|
| fprintf(stderr, "Element[%3d/%3d] type : FLOAT\n", _i_for_dump, GENERICSTACK_USED(stackp)); \ |
| 837 |
|
| break; \ |
| 838 |
|
| case GENERICSTACKITEMTYPE_DOUBLE: \ |
| 839 |
|
| fprintf(stderr, "Element[%3d/%3d] type : DOUBLE\n", _i_for_dump, GENERICSTACK_USED(stackp)); \ |
| 840 |
|
| break; \ |
| 841 |
|
| case GENERICSTACKITEMTYPE_PTR: \ |
| 842 |
|
| fprintf(stderr, "Element[%3d/%3d] type : PTR\n", _i_for_dump, GENERICSTACK_USED(stackp)); \ |
| 843 |
|
| break; \ |
| 844 |
|
| case GENERICSTACKITEMTYPE_ARRAY: \ |
| 845 |
|
| fprintf(stderr, "Element[%3d/%3d] type : ARRAY\n", _i_for_dump, GENERICSTACK_USED(stackp)); \ |
| 846 |
|
| break; \ |
| 847 |
|
| case GENERICSTACKITEMTYPE_LONG_DOUBLE: \ |
| 848 |
|
| fprintf(stderr, "Element[%3d/%3d] type : LONG DOUBLE\n", _i_for_dump, GENERICSTACK_USED(stackp)); \ |
| 849 |
|
| break; \ |
| 850 |
|
| default: \ |
| 851 |
|
| fprintf(stderr, "Element[%3d/%3d] type : %d\n", _i_for_dump, GENERICSTACK_USED(stackp), GENERICSTACKITEMTYPE(stackp, _i_for_dump)); \ |
| 852 |
|
| break; \ |
| 853 |
|
| _GENERICSTACK_DUMP_CASE_CUSTOM(stackp,_i_for_dump) \ |
| 854 |
|
| } \ |
| 855 |
|
| } \ |
| 856 |
|
| } while (0) |
| 857 |
|
| |
| 858 |
|
| /* ====================================================================== */ |
| 859 |
|
| /* Switches two entries */ |
| 860 |
|
| /* We support a "negative index", which mean start by far from the end. */ |
| 861 |
|
| /* ====================================================================== */ |
| 862 |
|
| #define GENERICSTACK_SWITCH(stackp, i1, i2) do { \ |
| 863 |
|
| int _genericStackSwitch_index1 = (int) (i1); \ |
| 864 |
|
| int _genericStackSwitch_index2 = (int) (i2); \ |
| 865 |
|
| \ |
| 866 |
|
| if (_genericStackSwitch_index1 < 0) { \ |
| 867 |
|
| _genericStackSwitch_index1 = (stackp)->usedi + _genericStackSwitch_index1; \ |
| 868 |
|
| } \ |
| 869 |
|
| if (_genericStackSwitch_index2 < 0) { \ |
| 870 |
|
| _genericStackSwitch_index2 = (stackp)->usedi + _genericStackSwitch_index2; \ |
| 871 |
|
| } \ |
| 872 |
|
| \ |
| 873 |
|
| if (GENERICSTACK_UNLIKELY((_genericStackSwitch_index1 < 0) || ((_genericStackSwitch_index1) >= (stackp)->usedi) || \ |
| 874 |
|
| (_genericStackSwitch_index2 < 0) || ((_genericStackSwitch_index2) >= (stackp)->usedi))) { \ |
| 875 |
|
| (stackp)->errori = 1; \ |
| 876 |
|
| errno = EINVAL; \ |
| 877 |
|
| } else if (_genericStackSwitch_index1 != _genericStackSwitch_index2) { \ |
| 878 |
|
| genericStackItem_t _item = (stackp)->items[_genericStackSwitch_index1]; \ |
| 879 |
|
| void *_addr1 = (void *) &((stackp)->items[_genericStackSwitch_index1]); \ |
| 880 |
|
| void *_addr2 = (void *) &((stackp)->items[_genericStackSwitch_index2]); \ |
| 881 |
|
| \ |
| 882 |
|
| memcpy(_addr1, _addr2, sizeof(genericStackItem_t)); \ |
| 883 |
|
| memcpy(_addr2, &_item, sizeof(genericStackItem_t)); \ |
| 884 |
|
| } \ |
| 885 |
|
| } while (0) |
| 886 |
|
| |
| 887 |
|
| #define GENERICSTACK_SORT(stackName, callback) qsort((void *) (stackName)->items, (size_t) (stackName)->usedi, sizeof(genericStackItem_t), callback) |
| 888 |
|
| |
| 889 |
|
| #endif /* GENERICSTACK_H */ |