1*13132SGlenn.Barry@oracle.com /*
2*13132SGlenn.Barry@oracle.com * This file is generated, please don't edit it.
3*13132SGlenn.Barry@oracle.com * script: ./../../../util/gen.pl
4*13132SGlenn.Barry@oracle.com * args: bimap errmap.h NAME=mecherrmap LEFT=OM_uint32 RIGHT=struct mecherror LEFTPRINT=print_OM_uint32 RIGHTPRINT=mecherror_print LEFTCMP=cmp_OM_uint32 RIGHTCMP=mecherror_cmp
5*13132SGlenn.Barry@oracle.com * The rest of this file is copied from a template, with
6*13132SGlenn.Barry@oracle.com * substitutions. See the template for copyright info.
7*13132SGlenn.Barry@oracle.com */
8*13132SGlenn.Barry@oracle.com /* start of t_bimap header template */
9*13132SGlenn.Barry@oracle.com /*
10*13132SGlenn.Barry@oracle.com * bidirectional mapping table, add-only
11*13132SGlenn.Barry@oracle.com *
12*13132SGlenn.Barry@oracle.com * Parameters:
13*13132SGlenn.Barry@oracle.com * NAME
14*13132SGlenn.Barry@oracle.com * LEFT, RIGHT - types
15*13132SGlenn.Barry@oracle.com * LEFTCMP, RIGHTCMP - comparison functions
16*13132SGlenn.Barry@oracle.com *
17*13132SGlenn.Barry@oracle.com * Methods:
18*13132SGlenn.Barry@oracle.com * int init() - nonzero is error code, if any possible
19*13132SGlenn.Barry@oracle.com * long size()
20*13132SGlenn.Barry@oracle.com * void foreach(int (*)(LEFT, RIGHT, void*), void*)
21*13132SGlenn.Barry@oracle.com * int add(LEFT, RIGHT) - 0 = success, -1 = allocation failure
22*13132SGlenn.Barry@oracle.com * const struct mecherror *findleft(OM_uint32) - null iff not found
23*13132SGlenn.Barry@oracle.com * const OM_uint32 *findright(struct mecherror)
24*13132SGlenn.Barry@oracle.com * void destroy() - destroys container, doesn't delete elements
25*13132SGlenn.Barry@oracle.com *
26*13132SGlenn.Barry@oracle.com * initial implementation: flat array of (left,right) pairs
27*13132SGlenn.Barry@oracle.com */
28*13132SGlenn.Barry@oracle.com
29*13132SGlenn.Barry@oracle.com struct mecherrmap__pair {
30*13132SGlenn.Barry@oracle.com OM_uint32 l;
31*13132SGlenn.Barry@oracle.com struct mecherror r;
32*13132SGlenn.Barry@oracle.com };
33*13132SGlenn.Barry@oracle.com /* end of t_bimap header template */
34*13132SGlenn.Barry@oracle.com /* start of t_array template */
35*13132SGlenn.Barry@oracle.com
36*13132SGlenn.Barry@oracle.com /*
37*13132SGlenn.Barry@oracle.com * array type, derived from template
38*13132SGlenn.Barry@oracle.com *
39*13132SGlenn.Barry@oracle.com * parameters:
40*13132SGlenn.Barry@oracle.com * NAME: mecherrmap__pairarray
41*13132SGlenn.Barry@oracle.com * TYPE: struct mecherrmap__pair
42*13132SGlenn.Barry@oracle.com *
43*13132SGlenn.Barry@oracle.com * methods:
44*13132SGlenn.Barry@oracle.com * int init() -> nonzero if fail initial allocation
45*13132SGlenn.Barry@oracle.com * unsigned long size() -> nonnegative number of values stored
46*13132SGlenn.Barry@oracle.com * int grow(newsize) -> negative if fail allocation, memset(,0,) new space
47*13132SGlenn.Barry@oracle.com * struct mecherrmap__pair *getaddr(idx) -> aborts if out of range
48*13132SGlenn.Barry@oracle.com * void set(idx, value) -> aborts if out of range
49*13132SGlenn.Barry@oracle.com * struct mecherrmap__pair get(idx) -> value, or aborts if out of range
50*13132SGlenn.Barry@oracle.com */
51*13132SGlenn.Barry@oracle.com
52*13132SGlenn.Barry@oracle.com #include <stdlib.h>
53*13132SGlenn.Barry@oracle.com #include <errno.h>
54*13132SGlenn.Barry@oracle.com #include <limits.h>
55*13132SGlenn.Barry@oracle.com #include <string.h>
56*13132SGlenn.Barry@oracle.com #ifdef HAVE_STDINT_H
57*13132SGlenn.Barry@oracle.com # include <stdint.h>
58*13132SGlenn.Barry@oracle.com #endif
59*13132SGlenn.Barry@oracle.com
60*13132SGlenn.Barry@oracle.com struct mecherrmap__pairarray__header {
61*13132SGlenn.Barry@oracle.com size_t allocated;
62*13132SGlenn.Barry@oracle.com struct mecherrmap__pair *elts;
63*13132SGlenn.Barry@oracle.com };
64*13132SGlenn.Barry@oracle.com typedef struct mecherrmap__pairarray__header mecherrmap__pairarray;
65*13132SGlenn.Barry@oracle.com
66*13132SGlenn.Barry@oracle.com static inline int
mecherrmap__pairarray_init(mecherrmap__pairarray * arr)67*13132SGlenn.Barry@oracle.com mecherrmap__pairarray_init(mecherrmap__pairarray *arr)
68*13132SGlenn.Barry@oracle.com {
69*13132SGlenn.Barry@oracle.com arr->elts = calloc(10, sizeof(struct mecherrmap__pair));
70*13132SGlenn.Barry@oracle.com if (arr->elts == NULL)
71*13132SGlenn.Barry@oracle.com return ENOMEM;
72*13132SGlenn.Barry@oracle.com arr->allocated = 10;
73*13132SGlenn.Barry@oracle.com return 0;
74*13132SGlenn.Barry@oracle.com }
75*13132SGlenn.Barry@oracle.com
76*13132SGlenn.Barry@oracle.com static inline long
mecherrmap__pairarray_size(mecherrmap__pairarray * arr)77*13132SGlenn.Barry@oracle.com mecherrmap__pairarray_size(mecherrmap__pairarray *arr)
78*13132SGlenn.Barry@oracle.com {
79*13132SGlenn.Barry@oracle.com return arr->allocated;
80*13132SGlenn.Barry@oracle.com }
81*13132SGlenn.Barry@oracle.com
82*13132SGlenn.Barry@oracle.com static inline long
mecherrmap__pairarray_max_size(mecherrmap__pairarray * arr)83*13132SGlenn.Barry@oracle.com mecherrmap__pairarray_max_size(mecherrmap__pairarray *arr)
84*13132SGlenn.Barry@oracle.com {
85*13132SGlenn.Barry@oracle.com size_t upper_bound;
86*13132SGlenn.Barry@oracle.com
87*13132SGlenn.Barry@oracle.com upper_bound = SIZE_MAX / sizeof(*arr->elts);
88*13132SGlenn.Barry@oracle.com if (upper_bound > LONG_MAX)
89*13132SGlenn.Barry@oracle.com upper_bound = LONG_MAX;
90*13132SGlenn.Barry@oracle.com return (long) upper_bound;
91*13132SGlenn.Barry@oracle.com }
92*13132SGlenn.Barry@oracle.com
93*13132SGlenn.Barry@oracle.com static inline int
mecherrmap__pairarray_grow(mecherrmap__pairarray * arr,unsigned long newcount)94*13132SGlenn.Barry@oracle.com mecherrmap__pairarray_grow(mecherrmap__pairarray *arr, unsigned long newcount)
95*13132SGlenn.Barry@oracle.com {
96*13132SGlenn.Barry@oracle.com size_t oldsize = sizeof(*arr->elts) * arr->allocated;
97*13132SGlenn.Barry@oracle.com size_t newsize;
98*13132SGlenn.Barry@oracle.com void *ptr;
99*13132SGlenn.Barry@oracle.com
100*13132SGlenn.Barry@oracle.com if (newcount > LONG_MAX)
101*13132SGlenn.Barry@oracle.com return -1;
102*13132SGlenn.Barry@oracle.com if (newcount < arr->allocated)
103*13132SGlenn.Barry@oracle.com return 0;
104*13132SGlenn.Barry@oracle.com if (newcount > mecherrmap__pairarray_max_size(arr))
105*13132SGlenn.Barry@oracle.com return -1;
106*13132SGlenn.Barry@oracle.com
107*13132SGlenn.Barry@oracle.com newsize = sizeof(*arr->elts) * newcount;
108*13132SGlenn.Barry@oracle.com ptr = realloc(arr->elts, newsize);
109*13132SGlenn.Barry@oracle.com if (ptr == NULL)
110*13132SGlenn.Barry@oracle.com return -1;
111*13132SGlenn.Barry@oracle.com memset((char *)ptr + oldsize, 0, newsize - oldsize);
112*13132SGlenn.Barry@oracle.com arr->elts = ptr;
113*13132SGlenn.Barry@oracle.com arr->allocated = newcount;
114*13132SGlenn.Barry@oracle.com return 0;
115*13132SGlenn.Barry@oracle.com }
116*13132SGlenn.Barry@oracle.com
117*13132SGlenn.Barry@oracle.com static inline struct mecherrmap__pair *
mecherrmap__pairarray_getaddr(mecherrmap__pairarray * arr,long idx)118*13132SGlenn.Barry@oracle.com mecherrmap__pairarray_getaddr (mecherrmap__pairarray *arr, long idx)
119*13132SGlenn.Barry@oracle.com {
120*13132SGlenn.Barry@oracle.com if (idx < 0 || idx >= arr->allocated)
121*13132SGlenn.Barry@oracle.com abort();
122*13132SGlenn.Barry@oracle.com return arr->elts + idx;
123*13132SGlenn.Barry@oracle.com }
124*13132SGlenn.Barry@oracle.com
125*13132SGlenn.Barry@oracle.com static inline void
mecherrmap__pairarray_set(mecherrmap__pairarray * arr,long idx,struct mecherrmap__pair value)126*13132SGlenn.Barry@oracle.com mecherrmap__pairarray_set (mecherrmap__pairarray *arr, long idx, struct mecherrmap__pair value)
127*13132SGlenn.Barry@oracle.com {
128*13132SGlenn.Barry@oracle.com struct mecherrmap__pair *newvalp;
129*13132SGlenn.Barry@oracle.com newvalp = mecherrmap__pairarray_getaddr(arr, idx);
130*13132SGlenn.Barry@oracle.com *newvalp = value;
131*13132SGlenn.Barry@oracle.com }
132*13132SGlenn.Barry@oracle.com
133*13132SGlenn.Barry@oracle.com static inline struct mecherrmap__pair
mecherrmap__pairarray_get(mecherrmap__pairarray * arr,long idx)134*13132SGlenn.Barry@oracle.com mecherrmap__pairarray_get (mecherrmap__pairarray *arr, long idx)
135*13132SGlenn.Barry@oracle.com {
136*13132SGlenn.Barry@oracle.com return *mecherrmap__pairarray_getaddr(arr, idx);
137*13132SGlenn.Barry@oracle.com }
138*13132SGlenn.Barry@oracle.com
139*13132SGlenn.Barry@oracle.com static inline void
mecherrmap__pairarray_destroy(mecherrmap__pairarray * arr)140*13132SGlenn.Barry@oracle.com mecherrmap__pairarray_destroy (mecherrmap__pairarray *arr)
141*13132SGlenn.Barry@oracle.com {
142*13132SGlenn.Barry@oracle.com free(arr->elts);
143*13132SGlenn.Barry@oracle.com arr->elts = 0;
144*13132SGlenn.Barry@oracle.com }
145*13132SGlenn.Barry@oracle.com /* end of t_array template */
146*13132SGlenn.Barry@oracle.com /* start of t_bimap body template */
147*13132SGlenn.Barry@oracle.com
148*13132SGlenn.Barry@oracle.com /* for use in cases where text substitutions may not work, like putting
149*13132SGlenn.Barry@oracle.com "const" before a type that turns out to be "char *" */
150*13132SGlenn.Barry@oracle.com typedef OM_uint32 mecherrmap__left_t;
151*13132SGlenn.Barry@oracle.com typedef struct mecherror mecherrmap__right_t;
152*13132SGlenn.Barry@oracle.com
153*13132SGlenn.Barry@oracle.com typedef struct {
154*13132SGlenn.Barry@oracle.com mecherrmap__pairarray a;
155*13132SGlenn.Barry@oracle.com long nextidx;
156*13132SGlenn.Barry@oracle.com } mecherrmap;
157*13132SGlenn.Barry@oracle.com
158*13132SGlenn.Barry@oracle.com static inline int
mecherrmap_init(mecherrmap * m)159*13132SGlenn.Barry@oracle.com mecherrmap_init (mecherrmap *m)
160*13132SGlenn.Barry@oracle.com {
161*13132SGlenn.Barry@oracle.com m->nextidx = 0;
162*13132SGlenn.Barry@oracle.com return mecherrmap__pairarray_init (&m->a);
163*13132SGlenn.Barry@oracle.com }
164*13132SGlenn.Barry@oracle.com
165*13132SGlenn.Barry@oracle.com static inline long
mecherrmap_size(mecherrmap * m)166*13132SGlenn.Barry@oracle.com mecherrmap_size (mecherrmap *m)
167*13132SGlenn.Barry@oracle.com {
168*13132SGlenn.Barry@oracle.com return mecherrmap__pairarray_size (&m->a);
169*13132SGlenn.Barry@oracle.com }
170*13132SGlenn.Barry@oracle.com
171*13132SGlenn.Barry@oracle.com static inline void
mecherrmap_foreach(mecherrmap * m,int (* fn)(OM_uint32,struct mecherror,void *),void * p)172*13132SGlenn.Barry@oracle.com mecherrmap_foreach (mecherrmap *m, int (*fn)(OM_uint32, struct mecherror, void *), void *p)
173*13132SGlenn.Barry@oracle.com {
174*13132SGlenn.Barry@oracle.com long i, sz;
175*13132SGlenn.Barry@oracle.com sz = m->nextidx;
176*13132SGlenn.Barry@oracle.com for (i = 0; i < sz; i++) {
177*13132SGlenn.Barry@oracle.com struct mecherrmap__pair *pair;
178*13132SGlenn.Barry@oracle.com pair = mecherrmap__pairarray_getaddr (&m->a, i);
179*13132SGlenn.Barry@oracle.com if ((*fn)(pair->l, pair->r, p) != 0)
180*13132SGlenn.Barry@oracle.com break;
181*13132SGlenn.Barry@oracle.com }
182*13132SGlenn.Barry@oracle.com }
183*13132SGlenn.Barry@oracle.com
184*13132SGlenn.Barry@oracle.com static inline int
mecherrmap_add(mecherrmap * m,OM_uint32 l,struct mecherror r)185*13132SGlenn.Barry@oracle.com mecherrmap_add (mecherrmap *m, OM_uint32 l, struct mecherror r)
186*13132SGlenn.Barry@oracle.com {
187*13132SGlenn.Barry@oracle.com long i, sz;
188*13132SGlenn.Barry@oracle.com struct mecherrmap__pair newpair;
189*13132SGlenn.Barry@oracle.com int err;
190*13132SGlenn.Barry@oracle.com
191*13132SGlenn.Barry@oracle.com sz = m->nextidx;
192*13132SGlenn.Barry@oracle.com /* Make sure we're not duplicating. */
193*13132SGlenn.Barry@oracle.com for (i = 0; i < sz; i++) {
194*13132SGlenn.Barry@oracle.com struct mecherrmap__pair *pair;
195*13132SGlenn.Barry@oracle.com pair = mecherrmap__pairarray_getaddr (&m->a, i);
196*13132SGlenn.Barry@oracle.com assert ((*cmp_OM_uint32)(l, pair->l) != 0);
197*13132SGlenn.Barry@oracle.com if ((*cmp_OM_uint32)(l, pair->l) == 0)
198*13132SGlenn.Barry@oracle.com abort();
199*13132SGlenn.Barry@oracle.com assert ((*mecherror_cmp)(r, pair->r) != 0);
200*13132SGlenn.Barry@oracle.com if ((*mecherror_cmp)(r, pair->r) == 0)
201*13132SGlenn.Barry@oracle.com abort();
202*13132SGlenn.Barry@oracle.com }
203*13132SGlenn.Barry@oracle.com newpair.l = l;
204*13132SGlenn.Barry@oracle.com newpair.r = r;
205*13132SGlenn.Barry@oracle.com if (sz >= LONG_MAX - 1)
206*13132SGlenn.Barry@oracle.com return ENOMEM;
207*13132SGlenn.Barry@oracle.com err = mecherrmap__pairarray_grow (&m->a, sz+1);
208*13132SGlenn.Barry@oracle.com if (err)
209*13132SGlenn.Barry@oracle.com return err;
210*13132SGlenn.Barry@oracle.com mecherrmap__pairarray_set (&m->a, sz, newpair);
211*13132SGlenn.Barry@oracle.com m->nextidx++;
212*13132SGlenn.Barry@oracle.com return 0;
213*13132SGlenn.Barry@oracle.com }
214*13132SGlenn.Barry@oracle.com
215*13132SGlenn.Barry@oracle.com static inline const mecherrmap__right_t *
mecherrmap_findleft(mecherrmap * m,OM_uint32 l)216*13132SGlenn.Barry@oracle.com mecherrmap_findleft (mecherrmap *m, OM_uint32 l)
217*13132SGlenn.Barry@oracle.com {
218*13132SGlenn.Barry@oracle.com long i, sz;
219*13132SGlenn.Barry@oracle.com sz = mecherrmap_size (m);
220*13132SGlenn.Barry@oracle.com for (i = 0; i < sz; i++) {
221*13132SGlenn.Barry@oracle.com struct mecherrmap__pair *pair;
222*13132SGlenn.Barry@oracle.com pair = mecherrmap__pairarray_getaddr (&m->a, i);
223*13132SGlenn.Barry@oracle.com if ((*cmp_OM_uint32)(l, pair->l) == 0)
224*13132SGlenn.Barry@oracle.com return &pair->r;
225*13132SGlenn.Barry@oracle.com }
226*13132SGlenn.Barry@oracle.com return 0;
227*13132SGlenn.Barry@oracle.com }
228*13132SGlenn.Barry@oracle.com
229*13132SGlenn.Barry@oracle.com static inline const mecherrmap__left_t *
mecherrmap_findright(mecherrmap * m,struct mecherror r)230*13132SGlenn.Barry@oracle.com mecherrmap_findright (mecherrmap *m, struct mecherror r)
231*13132SGlenn.Barry@oracle.com {
232*13132SGlenn.Barry@oracle.com long i, sz;
233*13132SGlenn.Barry@oracle.com sz = mecherrmap_size (m);
234*13132SGlenn.Barry@oracle.com for (i = 0; i < sz; i++) {
235*13132SGlenn.Barry@oracle.com struct mecherrmap__pair *pair;
236*13132SGlenn.Barry@oracle.com pair = mecherrmap__pairarray_getaddr (&m->a, i);
237*13132SGlenn.Barry@oracle.com if ((*mecherror_cmp)(r, pair->r) == 0)
238*13132SGlenn.Barry@oracle.com return &pair->l;
239*13132SGlenn.Barry@oracle.com }
240*13132SGlenn.Barry@oracle.com return 0;
241*13132SGlenn.Barry@oracle.com }
242*13132SGlenn.Barry@oracle.com
243*13132SGlenn.Barry@oracle.com struct mecherrmap__printstat {
244*13132SGlenn.Barry@oracle.com FILE *f;
245*13132SGlenn.Barry@oracle.com int comma;
246*13132SGlenn.Barry@oracle.com };
247*13132SGlenn.Barry@oracle.com static inline int
mecherrmap__printone(OM_uint32 l,struct mecherror r,void * p)248*13132SGlenn.Barry@oracle.com mecherrmap__printone (OM_uint32 l, struct mecherror r, void *p)
249*13132SGlenn.Barry@oracle.com {
250*13132SGlenn.Barry@oracle.com struct mecherrmap__printstat *ps = p;
251*13132SGlenn.Barry@oracle.com fprintf(ps->f, ps->comma ? ", (" : "(");
252*13132SGlenn.Barry@oracle.com ps->comma = 1;
253*13132SGlenn.Barry@oracle.com (*print_OM_uint32)(l, ps->f);
254*13132SGlenn.Barry@oracle.com fprintf(ps->f, ",");
255*13132SGlenn.Barry@oracle.com (*mecherror_print)(r, ps->f);
256*13132SGlenn.Barry@oracle.com fprintf(ps->f, ")");
257*13132SGlenn.Barry@oracle.com return 0;
258*13132SGlenn.Barry@oracle.com }
259*13132SGlenn.Barry@oracle.com
260*13132SGlenn.Barry@oracle.com static inline void
mecherrmap_printmap(mecherrmap * m,FILE * f)261*13132SGlenn.Barry@oracle.com mecherrmap_printmap (mecherrmap *m, FILE *f)
262*13132SGlenn.Barry@oracle.com {
263*13132SGlenn.Barry@oracle.com struct mecherrmap__printstat ps;
264*13132SGlenn.Barry@oracle.com ps.comma = 0;
265*13132SGlenn.Barry@oracle.com ps.f = f;
266*13132SGlenn.Barry@oracle.com fprintf(f, "(");
267*13132SGlenn.Barry@oracle.com mecherrmap_foreach (m, mecherrmap__printone, &ps);
268*13132SGlenn.Barry@oracle.com fprintf(f, ")");
269*13132SGlenn.Barry@oracle.com }
270*13132SGlenn.Barry@oracle.com
271*13132SGlenn.Barry@oracle.com static inline void
mecherrmap_destroy(mecherrmap * m)272*13132SGlenn.Barry@oracle.com mecherrmap_destroy (mecherrmap *m)
273*13132SGlenn.Barry@oracle.com {
274*13132SGlenn.Barry@oracle.com mecherrmap__pairarray_destroy (&m->a);
275*13132SGlenn.Barry@oracle.com }
276*13132SGlenn.Barry@oracle.com /* end of t_bimap body template */
277