array.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #ifndef KONG_ARRAY_HEADER
  2. #define KONG_ARRAY_HEADER
  3. #define static_array(type, name, max_size) \
  4. typedef struct name { \
  5. type values[max_size]; \
  6. size_t size; \
  7. size_t max; \
  8. } name;
  9. /*#define static_array_with_init(type, name, max_size) \
  10. struct { \
  11. type values[max_size]; \
  12. size_t size; \
  13. size_t max; \
  14. } name; \
  15. name.size = 0; \
  16. name.max = max_size;*/
  17. #define static_array_init(array) \
  18. array.size = 0; \
  19. array.max = sizeof(array.values) / sizeof(array.values[0])
  20. #define static_array_push(array, value) \
  21. if (array.size >= array.max) { \
  22. debug_context context = {0}; \
  23. error(context, "Array overflow"); \
  24. } \
  25. array.values[array.size++] = value;
  26. #define static_array_push_p(array, value) \
  27. if (array->size >= array->max) { \
  28. debug_context context = {0}; \
  29. error(context, "Array overflow"); \
  30. } \
  31. array->values[array->size++] = value;
  32. #endif