| 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 | #if NCPUS_AT_CONFIGURE_TIME > 1 && NCPUS_AT_CONFIGURE_TIME < 1025 | ||
| 34 | enum { SMALL_ARRAY_DEFAULT_SIZE = NCPUS_AT_CONFIGURE_TIME }; | ||
| 35 | #else | ||
| 36 | enum { SMALL_ARRAY_DEFAULT_SIZE = 64 }; | ||
| 37 | #endif | ||
| 38 | |||
| 39 | #endif /* SMALL_ARRAY_TEMPLATE_H */ | ||
| 40 | |||
| 41 | /* Required before including: | ||
| 42 | * SMALL_ARRAY_TYPE : the element type (e.g. int, my_struct, void *) | ||
| 43 | * Optional: | ||
| 44 | * SMALL_ARRAY_TNAME : token-safe name (e.g. int, my_struct, void_ptr) | ||
| 45 | */ | ||
| 46 | |||
| 47 | #ifndef SMALL_ARRAY_TYPE | ||
| 48 | #error "SMALL_ARRAY_TYPE must be defined" | ||
| 49 | #endif | ||
| 50 | |||
| 51 | #ifndef SMALL_ARRAY_TNAME | ||
| 52 | #define SMALL_ARRAY_TNAME SMALL_ARRAY_TYPE | ||
| 53 | #endif | ||
| 54 | |||
| 55 | #define _SA_CAT2(a,b) a##b | ||
| 56 | #define _SA_CAT(a,b) _SA_CAT2(a,b) | ||
| 57 | #define _SA_T(suffix) _SA_CAT(small_array_, _SA_CAT(SMALL_ARRAY_TNAME, suffix)) | ||
| 58 | |||
| 59 | typedef struct _SA_T() { | ||
| 60 | size_t size; | ||
| 61 | SMALL_ARRAY_TYPE *values; | ||
| 62 | SMALL_ARRAY_TYPE buffer[SMALL_ARRAY_DEFAULT_SIZE]; | ||
| 63 | } _SA_T(_t); | ||
| 64 | |||
| 65 | 182 | static inline SMALL_ARRAY_TYPE* _SA_T(_init)(_SA_T(_t) *array, size_t n) { | |
| 66 | |||
| 67 | 182 | array->size = n; | |
| 68 | 182 | array->values = (n <= SMALL_ARRAY_DEFAULT_SIZE) | |
| 69 | ? array->buffer | ||
| 70 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 177 times.
|
182 | : malloc(sizeof(SMALL_ARRAY_TYPE) * n); |
| 71 | 182 | return array->values; | |
| 72 | } | ||
| 73 | |||
| 74 | 182 | static inline void _SA_T(_free)(_SA_T(_t) *array) { | |
| 75 | |||
| 76 |
2/2✓ Branch 0 taken 177 times.
✓ Branch 1 taken 5 times.
|
182 | if (array->size > SMALL_ARRAY_DEFAULT_SIZE) { |
| 77 | 177 | free(array->values); | |
| 78 | } | ||
| 79 | 182 | array->values = NULL; | |
| 80 | 182 | } | |
| 81 | |||
| 82 | #undef _SA_T | ||
| 83 | #undef _SA_CAT | ||
| 84 | #undef _SA_CAT2 | ||
| 85 | #undef SMALL_ARRAY_TNAME | ||
| 86 | #undef SMALL_ARRAY_TYPE | ||
| 87 |