Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
1.3 KiB

  1. /// Dynamic memory-chunk, with (1) datatype size, (2/3) initialized / allocated chunk, (4) content
  2. typedef struct { uint8_t const elSize; uint32_t init, alloc; char* content; } DynamicArray;
  3. #define UTF8_ARRAY {4, 0, 0, NULL}
  4. static inline int p_alloc(DynamicArray *s, uint32_t amount) {
  5. uint32_t const diff=s->init+s->elSize*amount-s->alloc, nas=s->alloc+max(diff,15)*s->elSize;
  6. if (s->alloc < s->init + s->elSize * amount) {
  7. char* tmp = realloc(s->content, nas);
  8. if (!tmp) return 0;
  9. s->alloc = nas, s->content = tmp;
  10. }
  11. return 1;
  12. }
  13. static inline char *view(DynamicArray * s, uint32_t i) { return s->content + i*s->elSize; }
  14. static inline char *end(DynamicArray *s, uint32_t i) { return s->content +s->init-(i+1)*s->elSize; }
  15. static inline uint32_t getU32(DynamicArray* s, uint32_t i, int b) { return *((uint32_t*) (b ?view(s,i) :end(s,i))); }
  16. static char *expand(DynamicArray *s) { if (!p_alloc(s, 1)) return NULL; s->init += s->elSize; return end(s, 0); }
  17. static inline void pop(DynamicArray* s) { s->init -= s->elSize; }
  18. static inline void empty(DynamicArray* s) { s->init = 0; }
  19. static inline int size(DynamicArray const * s) { return s->init / s->elSize; }
  20. static inline void assign(DynamicArray* s, DynamicArray const *o) {
  21. if (p_alloc(s, size(o))) memcpy(s->content, o->content, (s->init=o->init));
  22. }