8 #define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({exit(-1); (void *)0;}):__result__;})
9 #define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({exit(-1); (void *)0;}):__result__;})
10 #define szmalloc(size) memset(smalloc(size), 0, size)
11 #define sstrdup(str) ({char *__strbuf__ = (str); strcpy(smalloc(strlen(__strbuf__) + 1), __strbuf__);})
12 #define omalloc(o) ((o) = szmalloc(sizeof(*(o))))
14 #define bufinit(buf) memset(&(buf), 0, sizeof(buf))
15 #define buffree(buf) do { if((buf).b != NULL) {free((buf).b);} } while(0)
16 #define bufdupc(buf) memcpy(smalloc((buf).d * sizeof(*((buf).b))), (buf).b, (buf).d * sizeof(*((buf).b)))
17 #define sizebuf(buf, wanted) (_sizebuf((struct buffer *)&(buf), (wanted), sizeof(*((buf).b))))
18 #define sizebuf2(buf, wanted) (_sizebuf2((void **)&(buf), &(buf ## size), (wanted), sizeof(*(buf))))
19 #define bufdel(buf, i) (memmove((buf).b + (i), (buf).b + (i) + 1, (--((buf).d) - (i)) * sizeof(*((buf).b))))
20 #define bufadd(buf, new) \
22 _sizebuf((struct buffer *)&(buf), (buf).d + 1, sizeof(*((buf).b))); \
23 (buf).b[(buf).d++] = (new); \
25 #define bufcat(buf, new, size) \
27 size_t __bufcat_size__; \
28 __bufcat_size__ = (size); \
29 _sizebuf((struct buffer *)&(buf), (buf).d + __bufcat_size__, sizeof((buf).b)); \
30 memcpy((buf).b + (buf).d, (new), (__bufcat_size__) * sizeof(*((buf).b))); \
31 (buf).d += __bufcat_size__; \
33 #define bufcatnum(buf, num, type) \
37 bufcat((buf), &__buf__, sizeof(type)); \
39 #define bufcatuid(buf, uid) \
43 bufcat((buf), &__buf__, sizeof(__buf__)); \
45 #define bufcatstr(buf, str) \
49 bufcat((buf), __buf__, strlen(__buf__) + 1); \
51 #define bufcatstr2(buf, str) \
55 bufcat((buf), __buf__, strlen(__buf__)); \
57 #define bufcats(buf, d) bufcat((buf), &(d), sizeof(d))
58 #define bufeat(buf, len) \
60 size_t __bufeat_size__; \
61 __bufeat_size__ = (len); \
62 memmove((buf).b, (buf).b + __bufeat_size__, (buf).d -= __bufeat_size__); \
85 void _sizebuf(struct buffer *buf, size_t wanted, size_t el);
86 void _sizebuf2(void **buf, size_t *sz, size_t wanted, size_t el);
87 char *vsprintf2(char *format, va_list al);
88 char *sprintf2(char *format, ...);