nightmaremail

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

gen_allocdefs.h (1488B)


      1 #ifndef GEN_ALLOC_DEFS_H
      2 #define GEN_ALLOC_DEFS_H
      3 
      4 #include "alloc.h"
      5 #include "error.h"
      6 #include "oflops.h"
      7 
      8 #define GEN_ALLOC_readyplus(ta,type,field,len,a,base,ta_rplus) \
      9 static int ta_rplus ## _internal (ta *x, unsigned int n, unsigned int pluslen) \
     10 { \
     11   unsigned int nlen; \
     12   errno = error_nomem; \
     13   if (x->field) { \
     14     unsigned int nnum; \
     15     type *nfield; \
     16     if (__builtin_add_overflow(n, pluslen, &n)) \
     17       return 0; \
     18     if (n <= x->a) \
     19       return 1; \
     20     if (__builtin_add_overflow(n, (n >> 3) + base, &nnum)) \
     21       return 0; \
     22     if (__builtin_mul_overflow(nnum, sizeof(type), &nlen)) \
     23       return 0; \
     24     nfield = realloc(x->field, nlen); \
     25     if (nfield == NULL) \
     26       return 0; \
     27     x->field = nfield; \
     28     x->a = nnum; \
     29     return 1; } \
     30   x->len = 0; \
     31   if (__builtin_mul_overflow(n, sizeof(type), &nlen)) \
     32     return 0; \
     33   x->field = (type *) alloc(nlen); \
     34   if (!x->field) \
     35     return 0; \
     36   x->a = n; \
     37   return 1; } \
     38 int ta_rplus(ta *x, unsigned int n) \
     39 { return ta_rplus ## _internal (x, n, x->len); }
     40 
     41 /* this needs a GEN_ALLOC_readyplus call before as it reuses the internal helper
     42  * function. */
     43 #define GEN_ALLOC_ready(ta,type,field,len,a,base,ta_ready) \
     44 int ta_ready(ta *x, unsigned int n) \
     45 { return ta_ready ## plus_internal (x, n, 0); }
     46 
     47 #define GEN_ALLOC_append(ta,type,field,len,a,base,ta_rplus,ta_append) \
     48 int ta_append(ta *x, type *i) \
     49 { if (!ta_rplus(x,1)) return 0; x->field[x->len++] = *i; return 1; }
     50 
     51 #endif