util.h (2126B)
1 #include <stdbool.h> 2 #include <stddef.h> 3 4 #ifndef noreturn 5 #if __STDC_VERSION__ >= 202311L 6 #define noreturn [[noreturn]] 7 #elif __STDC_VERSION__ >= 201112L 8 #define noreturn _Noreturn 9 /* C99 has no word for it: the compilers that build us have their own */ 10 #elif defined(__GNUC__) 11 #define noreturn __attribute__((noreturn)) 12 #else 13 #define noreturn 14 #endif 15 #endif 16 17 struct list { 18 struct list *prev, *next; 19 }; 20 21 struct array { 22 void *val; 23 size_t len, cap; 24 }; 25 26 struct treenode { 27 unsigned long long key; 28 void *child[2]; 29 int height; 30 bool new; /* set by treeinsert if this node was newly allocated */ 31 }; 32 33 extern char *argv0; 34 35 #ifndef countof 36 #define countof(a) (sizeof(a) / sizeof((a)[0])) 37 #endif 38 #define ALIGNDOWN(x, n) ((x) & -(n)) 39 #define ALIGNUP(x, n) ALIGNDOWN((x) + (n) - 1, n) 40 41 void warn(const char *, ...); 42 noreturn void fatal(const char *fmt, ...); 43 44 void *reallocarray(void *, size_t, size_t); 45 void *xreallocarray(void *, size_t, size_t); 46 void *xmalloc(size_t); 47 48 char *progname(char *, char *); 49 50 void listinsert(struct list *, struct list *); 51 void listremove(struct list *); 52 #define listelement(list, type, member) (type *)((char *)list - offsetof(type, member)) 53 54 void *arrayadd(struct array *, size_t); 55 void arrayaddptr(struct array *, void *); 56 void *arraysetptr(struct array *, size_t, void *); 57 void *arraygetptr(struct array *, size_t); 58 void arrayaddbuf(struct array *, const void *, size_t); 59 void *arraylast(struct array *, size_t); 60 #define arrayforeach(a, m) for (m = (a)->val; m != (void *)((char *)(a)->val + (a)->len); ++m) 61 62 /* map */ 63 64 struct map { 65 size_t len, cap; 66 struct mapkey *keys; 67 union mapval *vals; 68 }; 69 70 struct mapkey { 71 unsigned long hash; 72 const void *str; 73 size_t len; 74 }; 75 76 union mapval { 77 void *p; 78 int i; 79 }; 80 81 void mapkey(struct mapkey *, const void *, size_t); 82 void mapinit(struct map *, size_t); 83 void mapfree(struct map *, void (*)(void *)); 84 bool mapput(struct map *, struct mapkey *, size_t *); 85 void *mapget(struct map *, struct mapkey *, size_t *); 86 87 /* tree */ 88 89 void *treeinsert(void **, unsigned long long, size_t);