utils.h (1321B)
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 5 static inline int p_alloc(DynamicArray *s, uint32_t amount) { 6 uint32_t const diff=s->init+s->elSize*amount-s->alloc, nas=s->alloc+max(diff,15)*s->elSize; 7 if (s->alloc < s->init + s->elSize * amount) { 8 char* tmp = realloc(s->content, nas); 9 if (!tmp) return 0; 10 s->alloc = nas, s->content = tmp; 11 } 12 return 1; 13 } 14 static inline char *view(DynamicArray * s, uint32_t i) { return s->content + i*s->elSize; } 15 static inline char *end(DynamicArray *s, uint32_t i) { return s->content +s->init-(i+1)*s->elSize; } 16 static inline uint32_t getU32(DynamicArray* s, uint32_t i, int b) { return *((uint32_t*) (b ?view(s,i) :end(s,i))); } 17 static char *expand(DynamicArray *s) { if (!p_alloc(s, 1)) return NULL; s->init += s->elSize; return end(s, 0); } 18 static inline void pop(DynamicArray* s) { s->init -= s->elSize; } 19 static inline void empty(DynamicArray* s) { s->init = 0; } 20 static inline int size(DynamicArray const * s) { return s->init / s->elSize; } 21 static inline void assign(DynamicArray* s, DynamicArray const *o) { 22 if (p_alloc(s, size(o))) memcpy(s->content, o->content, (s->init=o->init)); 23 }