| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************/ | ||
| 2 | /* Copyright 2009-2026 Barcelona Supercomputing Center */ | ||
| 3 | /* */ | ||
| 4 | /* This file is part of the DLB library. */ | ||
| 5 | /* */ | ||
| 6 | /* DLB is free software: you can redistribute it and/or modify */ | ||
| 7 | /* it under the terms of the GNU Lesser General Public License as published by */ | ||
| 8 | /* the Free Software Foundation, either version 3 of the License, or */ | ||
| 9 | /* (at your option) any later version. */ | ||
| 10 | /* */ | ||
| 11 | /* DLB is distributed in the hope that it will be useful, */ | ||
| 12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | ||
| 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | ||
| 14 | /* GNU Lesser General Public License for more details. */ | ||
| 15 | /* */ | ||
| 16 | /* You should have received a copy of the GNU Lesser General Public License */ | ||
| 17 | /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */ | ||
| 18 | /*********************************************************************************/ | ||
| 19 | |||
| 20 | /* Credits: https://nullprogram.com/blog/2016/10/07/ */ | ||
| 21 | /* Credits: https://www.davidpriver.com/ctemplates.html */ | ||
| 22 | |||
| 23 | #ifndef SMALL_ARRAY_TEMPLATE_H | ||
| 24 | #define SMALL_ARRAY_TEMPLATE_H | ||
| 25 | |||
| 26 | #ifdef HAVE_CONFIG_H | ||
| 27 | #include <config.h> | ||
| 28 | #endif | ||
| 29 | |||
| 30 | #include <stddef.h> | ||
| 31 | #include <stdlib.h> | ||
| 32 | |||
| 33 | #ifndef SMALL_ARRAY_DEFAULT_BYTES | ||
| 34 | #define SMALL_ARRAY_DEFAULT_BYTES 1024 | ||
| 35 | #endif | ||
| 36 | |||
| 37 | #endif /* SMALL_ARRAY_TEMPLATE_H */ | ||
| 38 | |||
| 39 | /* Required before including: | ||
| 40 | * SMALL_ARRAY_TYPE : the element type (e.g. int, my_struct, void *) | ||
| 41 | * Optional: | ||
| 42 | * SMALL_ARRAY_TNAME : token-safe name (e.g. int, my_struct, void_ptr) | ||
| 43 | */ | ||
| 44 | |||
| 45 | #ifndef SMALL_ARRAY_TYPE | ||
| 46 | #error "SMALL_ARRAY_TYPE must be defined" | ||
| 47 | #endif | ||
| 48 | |||
| 49 | #ifndef SMALL_ARRAY_TNAME | ||
| 50 | #define SMALL_ARRAY_TNAME SMALL_ARRAY_TYPE | ||
| 51 | #endif | ||
| 52 | |||
| 53 | #define _SA_CAT2(a,b) a##b | ||
| 54 | #define _SA_CAT(a,b) _SA_CAT2(a,b) | ||
| 55 | #define _SA_T(suffix) _SA_CAT(small_array_, _SA_CAT(SMALL_ARRAY_TNAME, suffix)) | ||
| 56 | |||
| 57 | /* Capacity in elements, at least 1 so that buffer[] is never zero-length. | ||
| 58 | * Defined per instantiation because it depends on the element type. */ | ||
| 59 | #define _SA_CAP \ | ||
| 60 | (SMALL_ARRAY_DEFAULT_BYTES >= sizeof(SMALL_ARRAY_TYPE) \ | ||
| 61 | ? SMALL_ARRAY_DEFAULT_BYTES / sizeof(SMALL_ARRAY_TYPE) : 1) | ||
| 62 | |||
| 63 | |||
| 64 | typedef struct _SA_T() { | ||
| 65 | size_t size; | ||
| 66 | SMALL_ARRAY_TYPE *values; | ||
| 67 | SMALL_ARRAY_TYPE buffer[_SA_CAP]; | ||
| 68 | } _SA_T(_t); | ||
| 69 | |||
| 70 | |||
| 71 | /* Returns a pointer to n elements, or NULL if allocation failed (or n is | ||
| 72 | * too large). On failure the array is left empty, so _free is still safe. | ||
| 73 | * Elements are uninitialized. NOTE: values may point into buffer, so the | ||
| 74 | * struct must not be copied or returned by value after _init. */ | ||
| 75 | |||
| 76 | 194 | static inline SMALL_ARRAY_TYPE* _SA_T(_init)(_SA_T(_t) *array, size_t n) { | |
| 77 | |||
| 78 |
2/2✓ Branch 0 taken 185 times.
✓ Branch 1 taken 2 times.
|
194 | if (n <= _SA_CAP) { |
| 79 | 190 | array->values = array->buffer; | |
| 80 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
4 | } else if (n > SIZE_MAX / sizeof(SMALL_ARRAY_TYPE)) { |
| 81 | ✗ | array->values = NULL; // sizeof * n would overflow | |
| 82 | } else { | ||
| 83 | 4 | array->values = malloc(sizeof(SMALL_ARRAY_TYPE) * n); | |
| 84 | } | ||
| 85 | |||
| 86 |
1/2✓ Branch 0 taken 187 times.
✗ Branch 1 not taken.
|
194 | array->size = array->values ? n : 0; |
| 87 | |||
| 88 | 194 | return array->values; | |
| 89 | } | ||
| 90 | |||
| 91 | 194 | static inline void _SA_T(_free)(_SA_T(_t) *array) { | |
| 92 | |||
| 93 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 185 times.
|
194 | if (array->values != array->buffer) { |
| 94 | 4 | free(array->values); | |
| 95 | } | ||
| 96 | 194 | array->values = NULL; | |
| 97 | 194 | array->size = 0; | |
| 98 | 194 | } | |
| 99 | |||
| 100 | #undef _SA_CAP | ||
| 101 | #undef _SA_T | ||
| 102 | #undef _SA_CAT | ||
| 103 | #undef _SA_CAT2 | ||
| 104 | #undef SMALL_ARRAY_TNAME | ||
| 105 | #undef SMALL_ARRAY_TYPE | ||
| 106 |