generalize isl_basic_set_list to generic lists
[platform/upstream/isl.git] / isl_list_templ.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2011      INRIA Saclay
4  *
5  * Use of this software is governed by the GNU LGPLv2.1 license
6  *
7  * Written by Sven Verdoolaege, K.U.Leuven, Departement
8  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
11  */
12
13 #define xCAT(A,B) A ## B
14 #define CAT(A,B) xCAT(A,B)
15 #undef EL
16 #define EL CAT(isl_,BASE)
17 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
18 #define FN(TYPE,NAME) xFN(TYPE,NAME)
19 #define xLIST(EL) EL ## _list
20 #define LIST(EL) xLIST(EL)
21
22 __isl_give LIST(EL) *FN(LIST(EL),alloc)(isl_ctx *ctx, int n)
23 {
24         LIST(EL) *list;
25
26         if (n < 0)
27                 isl_die(ctx, isl_error_invalid,
28                         "cannot create list of negative length",
29                         return NULL);
30         list = isl_alloc(ctx, LIST(EL),
31                          sizeof(LIST(EL)) + (n - 1) * sizeof(struct EL *));
32         if (!list)
33                 return NULL;
34
35         list->ctx = ctx;
36         isl_ctx_ref(ctx);
37         list->ref = 1;
38         list->size = n;
39         list->n = 0;
40         return list;
41 }
42
43 __isl_give LIST(EL) *FN(LIST(EL),add)(__isl_take LIST(EL) *list,
44         __isl_take struct EL *el)
45 {
46         if (!list || !el)
47                 goto error;
48         isl_assert(list->ctx, list->n < list->size, goto error);
49         list->p[list->n] = el;
50         list->n++;
51         return list;
52 error:
53         FN(EL,free)(el);
54         FN(LIST(EL),free)(list);
55         return NULL;
56 }
57
58 void FN(LIST(EL),free)(__isl_take LIST(EL) *list)
59 {
60         int i;
61
62         if (!list)
63                 return;
64
65         if (--list->ref > 0)
66                 return;
67
68         isl_ctx_deref(list->ctx);
69         for (i = 0; i < list->n; ++i)
70                 FN(EL,free)(list->p[i]);
71         free(list);
72 }