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