Advertisement
Guest User

Untitled

a guest
May 16th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.40 KB | None | 0 0
  1. ///////////
  2. //xarray.h
  3. ///////////
  4.  
  5. // Creates new xarray with given element size, and starting array length
  6. void *
  7. load_xarray(size_t start_size, size_t element_size);
  8.  
  9. // Sets size of xarray to new_size. Old array object may be invalidated/moved.
  10. // Returns the new array object.
  11. #define xarray_resize(xarray, new_size) \
  12.   xarray_resize_impl(xarray, (sizeof *(xarray)), new_size)
  13. void *
  14. xarray_resize_impl(void *xarray, size_t element_size, size_t new_size);
  15.  
  16. // Increases size of array by 1.
  17. #define xarray_expand(xarray) xarray_expand_impl(xarray, sizeof *(xarray))
  18. void *
  19. xarray_expand_impl(void *xarray, size_t element_size);
  20.  
  21. // Get current length of array.
  22. size_t
  23. xarray_size(void *xarray);
  24.  
  25. // Deallocate array.
  26. void
  27. free_xarray(void *xarray);
  28.  
  29. ///////////
  30. //xarray.c
  31. ///////////
  32.  
  33. #include <stddef.h>
  34. #include <stdalign.h>
  35. #include <stdlib.h>
  36.  
  37. #include "xarray.h"
  38.  
  39. #define MAX(a, b) ((a) >= (b) ? (a) : (b))
  40.  
  41. struct xarray {
  42.   size_t size, capacity;
  43.   alignas(max_align_t) unsigned char mem[];
  44. };
  45.  
  46. void *
  47. load_xarray(const size_t start_size, const size_t element_size)
  48. {
  49.   struct xarray *const xarray = malloc(
  50.     sizeof (struct xarray) + start_size*element_size
  51.   );
  52.   xarray->capacity = xarray->size = start_size;
  53.   return xarray->mem;
  54. }
  55.  
  56. static inline struct xarray *
  57. get_internals(void *xarray)
  58. {
  59.   return (void *)((unsigned char *)xarray - (offsetof(struct xarray, mem)));
  60. }
  61.  
  62. void *
  63. xarray_expand_impl(void *xarray, const size_t element_size)
  64. {
  65.   struct xarray *internals = get_internals(xarray);
  66.   if (++internals->size > internals->capacity) {
  67.     internals->capacity = MAX(2 * internals->capacity, 1);
  68.     internals = realloc(
  69.       internals, sizeof (struct xarray) + internals->capacity*element_size
  70.     );
  71.     xarray = internals->mem;
  72.   }
  73.   return xarray;
  74. }
  75.  
  76. void *
  77. xarray_resize_impl(void *xarray, const size_t element_size, size_t new_size)
  78. {
  79.   struct xarray *internals = get_internals(xarray);
  80.   internals->size = new_size;
  81.   if (new_size > internals->capacity) {
  82.     internals->capacity = MAX(new_size, 2 * internals->capacity);
  83.     internals = realloc(
  84.       internals, sizeof (struct xarray) + internals->capacity*element_size
  85.     );
  86.     xarray = internals->mem;
  87.   }
  88.   return xarray;
  89. }
  90.  
  91. size_t
  92. xarray_size(void *xarray)
  93. {
  94.   return get_internals(xarray)->size;
  95. }
  96.  
  97. void
  98. free_xarray(void *xarray)
  99. {
  100.   free(get_internals(xarray));
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement