add *list_copy
[isl.git] / isl_list_templ.c
blobc3a8b4f1868139694aba117c69ec9c381a3c63b2
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2011 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
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
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)
22 __isl_give LIST(EL) *FN(LIST(EL),alloc)(isl_ctx *ctx, int n)
24 LIST(EL) *list;
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;
35 list->ctx = ctx;
36 isl_ctx_ref(ctx);
37 list->ref = 1;
38 list->size = n;
39 list->n = 0;
40 return list;
43 __isl_give LIST(EL) *FN(LIST(EL),copy)(__isl_keep LIST(EL) *list)
45 if (!list)
46 return NULL;
48 list->ref++;
49 return list;
52 __isl_give LIST(EL) *FN(LIST(EL),add)(__isl_take LIST(EL) *list,
53 __isl_take struct EL *el)
55 if (!list || !el)
56 goto error;
57 isl_assert(list->ctx, list->n < list->size, goto error);
58 list->p[list->n] = el;
59 list->n++;
60 return list;
61 error:
62 FN(EL,free)(el);
63 FN(LIST(EL),free)(list);
64 return NULL;
67 void FN(LIST(EL),free)(__isl_take LIST(EL) *list)
69 int i;
71 if (!list)
72 return;
74 if (--list->ref > 0)
75 return;
77 isl_ctx_deref(list->ctx);
78 for (i = 0; i < list->n; ++i)
79 FN(EL,free)(list->p[i]);
80 free(list);