Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////
- //xarray.h
- ///////////
- // Creates new xarray with given element size, and starting array length
- void *
- load_xarray(size_t start_size, size_t element_size);
- // Sets size of xarray to new_size. Old array object may be invalidated/moved.
- // Returns the new array object.
- #define xarray_resize(xarray, new_size) \
- xarray_resize_impl(xarray, (sizeof *(xarray)), new_size)
- void *
- xarray_resize_impl(void *xarray, size_t element_size, size_t new_size);
- // Increases size of array by 1.
- #define xarray_expand(xarray) xarray_expand_impl(xarray, sizeof *(xarray))
- void *
- xarray_expand_impl(void *xarray, size_t element_size);
- // Get current length of array.
- size_t
- xarray_size(void *xarray);
- // Deallocate array.
- void
- free_xarray(void *xarray);
- ///////////
- //xarray.c
- ///////////
- #include <stddef.h>
- #include <stdalign.h>
- #include <stdlib.h>
- #include "xarray.h"
- #define MAX(a, b) ((a) >= (b) ? (a) : (b))
- struct xarray {
- size_t size, capacity;
- alignas(max_align_t) unsigned char mem[];
- };
- void *
- load_xarray(const size_t start_size, const size_t element_size)
- {
- struct xarray *const xarray = malloc(
- sizeof (struct xarray) + start_size*element_size
- );
- xarray->capacity = xarray->size = start_size;
- return xarray->mem;
- }
- static inline struct xarray *
- get_internals(void *xarray)
- {
- return (void *)((unsigned char *)xarray - (offsetof(struct xarray, mem)));
- }
- void *
- xarray_expand_impl(void *xarray, const size_t element_size)
- {
- struct xarray *internals = get_internals(xarray);
- if (++internals->size > internals->capacity) {
- internals->capacity = MAX(2 * internals->capacity, 1);
- internals = realloc(
- internals, sizeof (struct xarray) + internals->capacity*element_size
- );
- xarray = internals->mem;
- }
- return xarray;
- }
- void *
- xarray_resize_impl(void *xarray, const size_t element_size, size_t new_size)
- {
- struct xarray *internals = get_internals(xarray);
- internals->size = new_size;
- if (new_size > internals->capacity) {
- internals->capacity = MAX(new_size, 2 * internals->capacity);
- internals = realloc(
- internals, sizeof (struct xarray) + internals->capacity*element_size
- );
- xarray = internals->mem;
- }
- return xarray;
- }
- size_t
- xarray_size(void *xarray)
- {
- return get_internals(xarray)->size;
- }
- void
- free_xarray(void *xarray)
- {
- free(get_internals(xarray));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement