fehQlibs 28
Qlibs
Loading...
Searching...
No Matches
genalloc.h
Go to the documentation of this file.
1#ifndef GENALLOC_H
2#define GENALLOC_H
3
4/*
5 * Revision 20250909, Erwin Hoffmann
6 * removed K/R style function definition
7 * added C23 compatibility
8*/
9
10#include <sys/types.h>
11
12/* GEN_ALLOC demystified:
13
14 GEN_ALLOC generates a list of self-defined types (structs) in an
15 allocated contiguous heap chunk while copying the content of the
16 entire field members or appending the existing field.
17 GEN_ALLOC types care of currently used and/or allocated bytes of field.
18
19Macros:
20 GEN_ALLOC_ready (ta,type,field,len,a,i,n,x,base,ta_ready)
21 GEN_ALLOC_readyplus(ta,type,field,len,a,i,n,x,base,ta_rplus)
22 GEN_ALLOC_append (ta,type,field,len,a,i,n,x,base,ta_rplus,ta_append)
23
24 0. ta: 'type alloc' - typedef'ed struct name (aka ipalloc et al.)
25 1. type: defined struct (used for size information)
26 2. field: declared public name of struct
27 2. len: used length of string
28 4. a: allocated size
29 5. i: current allocated size for member x
30 6. n: bytes to allocate; in 'ready' mode: +size of one entry;
31 in 'readyplus' mode: +size of +used size of one entry
32 7. x: local name (alias to field name)
33 8. base: size of single entry
34 9. ta_ready/ta_readyplus (operation)
35 10. ta_append (operation)
36
37*/
38
39/* file: gen_alloc.h */
40#define GEN_ALLOC_typedef(ta,type,field,len,a) \
41 typedef struct ta { type *field; unsigned int len; unsigned int a; } ta;
42
43/* file: gen_allocdefs.h (deprecated) */
44// used in: ipalloc, prioq, qmail-remote, qmail-inject, token822
45#define GEN_ALLOC_ready(ta,type,field,len,a,i,n,x,base,ta_ready) \
46int ta_ready(ta *x,unsigned int n) \
47{ unsigned int i; \
48 if (x->field) { \
49 i = x->a; \
50 if (n > i) { \
51 x->a = base + n + (n >> 3); \
52 if (alloc_re((void **)&x->field,i * sizeof(type),x->a * sizeof(type))) return 1; \
53 x->a = i; return 0; } \
54 return 1; } \
55 x->len = 0; \
56 return !!(x->field = (type *) alloc((x->a = n) * sizeof(type))); }
57
58#define GEN_ALLOC_readyplus(ta,type,field,len,a,i,n,x,base,ta_rplus) \
59int ta_rplus(ta *x,unsigned int n) \
60{ unsigned int i; \
61 if (x->field) { \
62 i = x->a; n += x->len; \
63 if (n > i) { \
64 x->a = base + n + (n >> 3); \
65 if (alloc_re((void **)&x->field,i * sizeof(type),x->a * sizeof(type))) return 1; \
66 x->a = i; return 0; } \
67 return 1; } \
68 x->len = 0; \
69 return !!(x->field = (type *) alloc((x->a = n) * sizeof(type))); }
70
71#define GEN_ALLOC_append(ta,type,field,len,a,i,n,x,base,ta_rplus,ta_append) \
72int ta_append(ta *x,type *i) \
73{ if (!ta_rplus(x,1)) return 0; x->field[x->len++] = *i; return 1; }
74
75#endif