scheduling: optionally create schedules with outermost parallelism
[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_ctx *FN(LIST(EL),get_ctx)(__isl_keep LIST(EL) *list)
23 {
24         return list ? list->ctx : NULL;
25 }
26
27 __isl_give LIST(EL) *FN(LIST(EL),alloc)(isl_ctx *ctx, int n)
28 {
29         LIST(EL) *list;
30
31         if (n < 0)
32                 isl_die(ctx, isl_error_invalid,
33                         "cannot create list of negative length",
34                         return NULL);
35         list = isl_alloc(ctx, LIST(EL),
36                          sizeof(LIST(EL)) + (n - 1) * sizeof(struct EL *));
37         if (!list)
38                 return NULL;
39
40         list->ctx = ctx;
41         isl_ctx_ref(ctx);
42         list->ref = 1;
43         list->size = n;
44         list->n = 0;
45         return list;
46 }
47
48 __isl_give LIST(EL) *FN(LIST(EL),copy)(__isl_keep LIST(EL) *list)
49 {
50         if (!list)
51                 return NULL;
52
53         list->ref++;
54         return list;
55 }
56
57 __isl_give LIST(EL) *FN(LIST(EL),add)(__isl_take LIST(EL) *list,
58         __isl_take struct EL *el)
59 {
60         if (!list || !el)
61                 goto error;
62         isl_assert(list->ctx, list->n < list->size, goto error);
63         list->p[list->n] = el;
64         list->n++;
65         return list;
66 error:
67         FN(EL,free)(el);
68         FN(LIST(EL),free)(list);
69         return NULL;
70 }
71
72 void FN(LIST(EL),free)(__isl_take LIST(EL) *list)
73 {
74         int i;
75
76         if (!list)
77                 return;
78
79         if (--list->ref > 0)
80                 return;
81
82         isl_ctx_deref(list->ctx);
83         for (i = 0; i < list->n; ++i)
84                 FN(EL,free)(list->p[i]);
85         free(list);
86 }
87
88 int FN(FN(LIST(EL),n),BASE)(__isl_keep LIST(EL) *list)
89 {
90         return list ? list->n : 0;
91 }
92
93 __isl_give EL *FN(FN(LIST(EL),get),BASE)(__isl_keep LIST(EL) *list, int index)
94 {
95         if (!list)
96                 return NULL;
97         if (index < 0 || index >= list->n)
98                 isl_die(list->ctx, isl_error_invalid,
99                         "index out of bounds", return NULL);
100         return FN(EL,copy)(list->p[index]);
101 }
102
103 int FN(LIST(EL),foreach)(__isl_keep LIST(EL) *list,
104         int (*fn)(__isl_take EL *el, void *user), void *user)
105 {
106         int i;
107
108         if (!list)
109                 return -1;
110
111         for (i = 0; i < list->n; ++i) {
112                 EL *el = FN(EL,copy(list->p[i]));
113                 if (!el)
114                         return -1;
115                 if (fn(el, user) < 0)
116                         return -1;
117         }
118
119         return 0;
120 }
121
122 __isl_give isl_printer *CAT(isl_printer_print_,LIST(BASE))(
123         __isl_take isl_printer *p, __isl_keep LIST(EL) *list)
124 {
125         int i;
126
127         if (!p || !list)
128                 goto error;
129         p = isl_printer_print_str(p, "(");
130         for (i = 0; i < list->n; ++i) {
131                 if (i)
132                         p = isl_printer_print_str(p, ",");
133                 p = CAT(isl_printer_print_,BASE)(p, list->p[i]);
134         }
135         p = isl_printer_print_str(p, ")");
136         return p;
137 error:
138         isl_printer_free(p);
139         return NULL;
140 }
141
142 void FN(LIST(EL),dump)(__isl_keep LIST(EL) *list)
143 {
144         isl_printer *printer;
145
146         if (!list)
147                 return;
148
149         printer = isl_printer_to_file(FN(LIST(EL),get_ctx)(list), stderr);
150         printer = CAT(isl_printer_print_,LIST(BASE))(printer, list);
151         printer = isl_printer_end_line(printer);
152
153         isl_printer_free(printer);
154 }