123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521 |
- #include <malloc/dynarray.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <string.h>
- #ifndef DYNARRAY_STRUCT
- # error "DYNARRAY_STRUCT must be defined"
- #endif
- #ifndef DYNARRAY_ELEMENT
- # error "DYNARRAY_ELEMENT must be defined"
- #endif
- #ifndef DYNARRAY_PREFIX
- # error "DYNARRAY_PREFIX must be defined"
- #endif
- #ifdef DYNARRAY_INITIAL_SIZE
- # if DYNARRAY_INITIAL_SIZE < 0
- # error "DYNARRAY_INITIAL_SIZE must be non-negative"
- # endif
- # if DYNARRAY_INITIAL_SIZE > 0
- # define DYNARRAY_HAVE_SCRATCH 1
- # else
- # define DYNARRAY_HAVE_SCRATCH 0
- # endif
- #else
- # define DYNARRAY_INITIAL_SIZE \
- (sizeof (DYNARRAY_ELEMENT) > 64 ? 2 : 128 / sizeof (DYNARRAY_ELEMENT))
- # define DYNARRAY_HAVE_SCRATCH 1
- #endif
- struct DYNARRAY_STRUCT
- {
- union
- {
- struct dynarray_header dynarray_abstract;
- struct
- {
-
- size_t used;
- size_t allocated;
- DYNARRAY_ELEMENT *array;
- } dynarray_header;
- };
- #if DYNARRAY_HAVE_SCRATCH
-
- DYNARRAY_ELEMENT scratch[DYNARRAY_INITIAL_SIZE];
- #endif
- };
- #define DYNARRAY_CONCAT0(prefix, name) prefix##name
- #define DYNARRAY_CONCAT1(prefix, name) DYNARRAY_CONCAT0(prefix, name)
- #define DYNARRAY_NAME(name) DYNARRAY_CONCAT1(DYNARRAY_PREFIX, name)
- #if DYNARRAY_HAVE_SCRATCH
- # define DYNARRAY_SCRATCH(list) (list)->scratch
- #else
- # define DYNARRAY_SCRATCH(list) NULL
- #endif
- static inline void
- DYNARRAY_NAME (free__elements__) (DYNARRAY_ELEMENT *__dynarray_array,
- size_t __dynarray_used)
- {
- #ifdef DYNARRAY_ELEMENT_FREE
- for (size_t __dynarray_i = 0; __dynarray_i < __dynarray_used; ++__dynarray_i)
- DYNARRAY_ELEMENT_FREE (&__dynarray_array[__dynarray_i]);
- #endif
- }
- static inline void
- DYNARRAY_NAME (free__array__) (struct DYNARRAY_STRUCT *list)
- {
- #if DYNARRAY_HAVE_SCRATCH
- if (list->dynarray_header.array != list->scratch)
- free (list->dynarray_header.array);
- #else
- free (list->dynarray_header.array);
- #endif
- }
- __attribute__ ((nonnull (1)))
- static void
- DYNARRAY_NAME (init) (struct DYNARRAY_STRUCT *list)
- {
- list->dynarray_header.used = 0;
- list->dynarray_header.allocated = DYNARRAY_INITIAL_SIZE;
- list->dynarray_header.array = DYNARRAY_SCRATCH (list);
- }
- __attribute__ ((unused, nonnull (1)))
- static void
- DYNARRAY_NAME (free) (struct DYNARRAY_STRUCT *list)
- {
- DYNARRAY_NAME (free__elements__)
- (list->dynarray_header.array, list->dynarray_header.used);
- DYNARRAY_NAME (free__array__) (list);
- DYNARRAY_NAME (init) (list);
- }
- __attribute__ ((nonnull (1)))
- static inline bool
- DYNARRAY_NAME (has_failed) (const struct DYNARRAY_STRUCT *list)
- {
- return list->dynarray_header.allocated == __dynarray_error_marker ();
- }
- __attribute__ ((nonnull (1)))
- static void
- DYNARRAY_NAME (mark_failed) (struct DYNARRAY_STRUCT *list)
- {
- DYNARRAY_NAME (free__elements__)
- (list->dynarray_header.array, list->dynarray_header.used);
- DYNARRAY_NAME (free__array__) (list);
- list->dynarray_header.array = DYNARRAY_SCRATCH (list);
- list->dynarray_header.used = 0;
- list->dynarray_header.allocated = __dynarray_error_marker ();
- }
- __attribute__ ((nonnull (1)))
- static inline size_t
- DYNARRAY_NAME (size) (const struct DYNARRAY_STRUCT *list)
- {
- return list->dynarray_header.used;
- }
- __attribute__ ((nonnull (1)))
- static inline DYNARRAY_ELEMENT *
- DYNARRAY_NAME (at) (struct DYNARRAY_STRUCT *list, size_t index)
- {
- if (__glibc_unlikely (index >= DYNARRAY_NAME (size) (list)))
- __libc_dynarray_at_failure (DYNARRAY_NAME (size) (list), index);
- return list->dynarray_header.array + index;
- }
- __attribute__ ((nonnull (1)))
- static inline DYNARRAY_ELEMENT *
- DYNARRAY_NAME (begin) (struct DYNARRAY_STRUCT *list)
- {
- return list->dynarray_header.array;
- }
- __attribute__ ((nonnull (1)))
- static inline DYNARRAY_ELEMENT *
- DYNARRAY_NAME (end) (struct DYNARRAY_STRUCT *list)
- {
- return list->dynarray_header.array + list->dynarray_header.used;
- }
- static void
- DYNARRAY_NAME (add__) (struct DYNARRAY_STRUCT *list, DYNARRAY_ELEMENT item)
- {
- if (__glibc_unlikely
- (!__libc_dynarray_emplace_enlarge (&list->dynarray_abstract,
- DYNARRAY_SCRATCH (list),
- sizeof (DYNARRAY_ELEMENT))))
- {
- DYNARRAY_NAME (mark_failed) (list);
- return;
- }
-
- list->dynarray_header.array[list->dynarray_header.used++] = item;
- }
- __attribute__ ((unused, nonnull (1)))
- static inline void
- DYNARRAY_NAME (add) (struct DYNARRAY_STRUCT *list, DYNARRAY_ELEMENT item)
- {
-
- if (DYNARRAY_NAME (has_failed) (list))
- return;
-
- if (__glibc_unlikely (list->dynarray_header.used
- == list->dynarray_header.allocated))
- {
- DYNARRAY_NAME (add__) (list, item);
- return;
- }
-
- list->dynarray_header.array[list->dynarray_header.used++] = item;
- }
- static inline DYNARRAY_ELEMENT *
- DYNARRAY_NAME (emplace__tail__) (struct DYNARRAY_STRUCT *list)
- {
- DYNARRAY_ELEMENT *result
- = &list->dynarray_header.array[list->dynarray_header.used];
- ++list->dynarray_header.used;
- #if defined (DYNARRAY_ELEMENT_INIT)
- DYNARRAY_ELEMENT_INIT (result);
- #elif defined (DYNARRAY_ELEMENT_FREE)
- memset (result, 0, sizeof (*result));
- #endif
- return result;
- }
- static DYNARRAY_ELEMENT *
- DYNARRAY_NAME (emplace__) (struct DYNARRAY_STRUCT *list)
- {
- if (__glibc_unlikely
- (!__libc_dynarray_emplace_enlarge (&list->dynarray_abstract,
- DYNARRAY_SCRATCH (list),
- sizeof (DYNARRAY_ELEMENT))))
- {
- DYNARRAY_NAME (mark_failed) (list);
- return NULL;
- }
- return DYNARRAY_NAME (emplace__tail__) (list);
- }
- __attribute__ ((unused, warn_unused_result, nonnull (1)))
- static
- #if !(defined (DYNARRAY_ELEMENT_INIT) || defined (DYNARRAY_ELEMENT_FREE))
- inline
- #endif
- DYNARRAY_ELEMENT *
- DYNARRAY_NAME (emplace) (struct DYNARRAY_STRUCT *list)
- {
-
- if (DYNARRAY_NAME (has_failed) (list))
- return NULL;
-
- if (__glibc_unlikely (list->dynarray_header.used
- == list->dynarray_header.allocated))
- return (DYNARRAY_NAME (emplace__) (list));
- return DYNARRAY_NAME (emplace__tail__) (list);
- }
- __attribute__ ((unused, nonnull (1)))
- static bool
- DYNARRAY_NAME (resize) (struct DYNARRAY_STRUCT *list, size_t size)
- {
- if (size > list->dynarray_header.used)
- {
- bool ok;
- #if defined (DYNARRAY_ELEMENT_INIT)
-
- size_t old_size = list->dynarray_header.used;
- ok = __libc_dynarray_resize (&list->dynarray_abstract,
- size, DYNARRAY_SCRATCH (list),
- sizeof (DYNARRAY_ELEMENT));
- if (ok)
- for (size_t i = old_size; i < size; ++i)
- {
- DYNARRAY_ELEMENT_INIT (&list->dynarray_header.array[i]);
- }
- #elif defined (DYNARRAY_ELEMENT_FREE)
-
- ok = __libc_dynarray_resize_clear
- (&list->dynarray_abstract, size,
- DYNARRAY_SCRATCH (list), sizeof (DYNARRAY_ELEMENT));
- #else
- ok = __libc_dynarray_resize (&list->dynarray_abstract,
- size, DYNARRAY_SCRATCH (list),
- sizeof (DYNARRAY_ELEMENT));
- #endif
- if (__glibc_unlikely (!ok))
- DYNARRAY_NAME (mark_failed) (list);
- return ok;
- }
- else
- {
-
- DYNARRAY_NAME (free__elements__)
- (list->dynarray_header.array + size,
- list->dynarray_header.used - size);
- list->dynarray_header.used = size;
- return true;
- }
- }
- __attribute__ ((unused, nonnull (1)))
- static void
- DYNARRAY_NAME (remove_last) (struct DYNARRAY_STRUCT *list)
- {
-
- if (list->dynarray_header.used > 0)
- {
- size_t new_length = list->dynarray_header.used - 1;
- #ifdef DYNARRAY_ELEMENT_FREE
- DYNARRAY_ELEMENT_FREE (&list->dynarray_header.array[new_length]);
- #endif
- list->dynarray_header.used = new_length;
- }
- }
- __attribute__ ((unused, nonnull (1)))
- static void
- DYNARRAY_NAME (clear) (struct DYNARRAY_STRUCT *list)
- {
-
- DYNARRAY_NAME (free__elements__)
- (list->dynarray_header.array, list->dynarray_header.used);
- list->dynarray_header.used = 0;
- }
- #ifdef DYNARRAY_FINAL_TYPE
- __attribute__ ((unused, warn_unused_result, nonnull (1, 2)))
- static bool
- DYNARRAY_NAME (finalize) (struct DYNARRAY_STRUCT *list,
- DYNARRAY_FINAL_TYPE *result)
- {
- struct dynarray_finalize_result res;
- if (__libc_dynarray_finalize (&list->dynarray_abstract,
- DYNARRAY_SCRATCH (list),
- sizeof (DYNARRAY_ELEMENT), &res))
- {
-
- DYNARRAY_NAME (init) (list);
- *result = (DYNARRAY_FINAL_TYPE) { res.array, res.length };
- return true;
- }
- else
- {
-
- DYNARRAY_NAME (free) (list);
- errno = ENOMEM;
- return false;
- }
- }
- #else
- __attribute__ ((unused, warn_unused_result, nonnull (1)))
- static DYNARRAY_ELEMENT *
- DYNARRAY_NAME (finalize) (struct DYNARRAY_STRUCT *list, size_t *lengthp)
- {
- struct dynarray_finalize_result res;
- if (__libc_dynarray_finalize (&list->dynarray_abstract,
- DYNARRAY_SCRATCH (list),
- sizeof (DYNARRAY_ELEMENT), &res))
- {
-
- DYNARRAY_NAME (init) (list);
- if (lengthp != NULL)
- *lengthp = res.length;
- return res.array;
- }
- else
- {
-
- DYNARRAY_NAME (free) (list);
- errno = ENOMEM;
- return NULL;
- }
- }
- #endif
- #undef DYNARRAY_CONCAT0
- #undef DYNARRAY_CONCAT1
- #undef DYNARRAY_NAME
- #undef DYNARRAY_SCRATCH
- #undef DYNARRAY_HAVE_SCRATCH
- #undef DYNARRAY_STRUCT
- #undef DYNARRAY_ELEMENT
- #undef DYNARRAY_PREFIX
- #undef DYNARRAY_ELEMENT_FREE
- #undef DYNARRAY_ELEMENT_INIT
- #undef DYNARRAY_INITIAL_SIZE
- #undef DYNARRAY_FINAL_TYPE
|