isl_basic_set_opt: avoid invalid access on error path
[platform/upstream/isl.git] / isl_list_templ.c
index 9504e6e..0af5b76 100644 (file)
@@ -1,13 +1,15 @@
 /*
  * Copyright 2008-2009 Katholieke Universiteit Leuven
  * Copyright 2011      INRIA Saclay
+ * Copyright 2012      Ecole Normale Superieure
  *
- * Use of this software is governed by the GNU LGPLv2.1 license
+ * Use of this software is governed by the MIT license
  *
  * Written by Sven Verdoolaege, K.U.Leuven, Departement
  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
+ * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
  */
 
 #define xCAT(A,B) A ## B
@@ -54,12 +56,77 @@ __isl_give LIST(EL) *FN(LIST(EL),copy)(__isl_keep LIST(EL) *list)
        return list;
 }
 
+__isl_give LIST(EL) *FN(LIST(EL),dup)(__isl_keep LIST(EL) *list)
+{
+       int i;
+       LIST(EL) *dup;
+
+       if (!list)
+               return NULL;
+
+       dup = FN(LIST(EL),alloc)(FN(LIST(EL),get_ctx)(list), list->n);
+       if (!dup)
+               return NULL;
+       for (i = 0; i < list->n; ++i)
+               dup = FN(LIST(EL),add)(dup, FN(EL,copy)(list->p[i]));
+       return dup;
+}
+
+__isl_give LIST(EL) *FN(LIST(EL),cow)(__isl_take LIST(EL) *list)
+{
+       if (!list)
+               return NULL;
+
+       if (list->ref == 1)
+               return list;
+       list->ref--;
+       return FN(LIST(EL),dup)(list);
+}
+
+/* Make sure "list" has room for at least "n" more pieces.
+ *
+ * If there is only one reference to list, we extend it in place.
+ * Otherwise, we create a new LIST(EL) and copy the elements.
+ */
+static __isl_give LIST(EL) *FN(LIST(EL),grow)(__isl_take LIST(EL) *list, int n)
+{
+       isl_ctx *ctx;
+       int i, new_size;
+       LIST(EL) *res;
+
+       if (!list)
+               return NULL;
+       if (list->n + n <= list->size)
+               return list;
+
+       ctx = FN(LIST(EL),get_ctx)(list);
+       new_size = ((list->n + n + 1) * 3) / 2;
+       if (list->ref == 1) {
+               res = isl_realloc(ctx, list, LIST(EL),
+                           sizeof(LIST(EL)) + (new_size - 1) * sizeof(EL *));
+               if (!res)
+                       return FN(LIST(EL),free)(list);
+               res->size = new_size;
+               return res;
+       }
+
+       res = FN(LIST(EL),alloc)(ctx, new_size);
+       if (!res)
+               return FN(LIST(EL),free)(list);
+
+       for (i = 0; i < list->n; ++i)
+               res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
+
+       FN(LIST(EL),free)(list);
+       return res;
+}
+
 __isl_give LIST(EL) *FN(LIST(EL),add)(__isl_take LIST(EL) *list,
        __isl_take struct EL *el)
 {
+       list = FN(LIST(EL),grow)(list, 1);
        if (!list || !el)
                goto error;
-       isl_assert(list->ctx, list->n < list->size, goto error);
        list->p[list->n] = el;
        list->n++;
        return list;
@@ -69,20 +136,91 @@ error:
        return NULL;
 }
 
-void FN(LIST(EL),free)(__isl_take LIST(EL) *list)
+/* Remove the "n" elements starting at "first" from "list".
+ */
+__isl_give LIST(EL) *FN(LIST(EL),drop)(__isl_take LIST(EL) *list,
+       unsigned first, unsigned n)
 {
        int i;
 
        if (!list)
-               return;
+               return NULL;
+       if (first + n > list->n || first + n < first)
+               isl_die(list->ctx, isl_error_invalid,
+                       "index out of bounds", return FN(LIST(EL),free)(list));
+       if (n == 0)
+               return list;
+       list = FN(LIST(EL),cow)(list);
+       if (!list)
+               return NULL;
+       for (i = 0; i < n; ++i)
+               FN(EL,free)(list->p[first + i]);
+       for (i = first; i + n < list->n; ++i)
+               list->p[i] = list->p[i + n];
+       list->n -= n;
+       return list;
+}
+
+/* Insert "el" at position "pos" in "list".
+ *
+ * If there is only one reference to "list" and if it already has space
+ * for one extra element, we insert it directly into "list".
+ * Otherwise, we create a new list consisting of "el" and copied
+ * elements from "list".
+ */
+__isl_give LIST(EL) *FN(LIST(EL),insert)(__isl_take LIST(EL) *list,
+       unsigned pos, __isl_take struct EL *el)
+{
+       int i;
+       isl_ctx *ctx;
+       LIST(EL) *res;
+
+       if (!list || !el)
+               goto error;
+       ctx = FN(LIST(EL),get_ctx)(list);
+       if (pos > list->n)
+               isl_die(ctx, isl_error_invalid,
+                       "index out of bounds", goto error);
+
+       if (list->ref == 1 && list->size > list->n) {
+               for (i = list->n - 1; i >= pos; --i)
+                       list->p[i + 1] = list->p[i];
+               list->n++;
+               list->p[pos] = el;
+               return list;
+       }
+
+       res = FN(LIST(EL),alloc)(ctx, list->n + 1);
+       for (i = 0; i < pos; ++i)
+               res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
+       res = FN(LIST(EL),add)(res, el);
+       for (i = pos; i < list->n; ++i)
+               res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
+       FN(LIST(EL),free)(list);
+
+       return res;
+error:
+       FN(EL,free)(el);
+       FN(LIST(EL),free)(list);
+       return NULL;
+}
+
+void *FN(LIST(EL),free)(__isl_take LIST(EL) *list)
+{
+       int i;
+
+       if (!list)
+               return NULL;
 
        if (--list->ref > 0)
-               return;
+               return NULL;
 
        isl_ctx_deref(list->ctx);
        for (i = 0; i < list->n; ++i)
                FN(EL,free)(list->p[i]);
        free(list);
+
+       return NULL;
 }
 
 int FN(FN(LIST(EL),n),BASE)(__isl_keep LIST(EL) *list)
@@ -100,6 +238,32 @@ __isl_give EL *FN(FN(LIST(EL),get),BASE)(__isl_keep LIST(EL) *list, int index)
        return FN(EL,copy)(list->p[index]);
 }
 
+/* Replace the element at position "index" in "list" by "el".
+ */
+__isl_give LIST(EL) *FN(FN(LIST(EL),set),BASE)(__isl_take LIST(EL) *list,
+       int index, __isl_take EL *el)
+{
+       if (!list || !el)
+               goto error;
+       if (index < 0 || index >= list->n)
+               isl_die(list->ctx, isl_error_invalid,
+                       "index out of bounds", goto error);
+       if (list->p[index] == el) {
+               FN(EL,free)(el);
+               return list;
+       }
+       list = FN(LIST(EL),cow)(list);
+       if (!list)
+               goto error;
+       FN(EL,free)(list->p[index]);
+       list->p[index] = el;
+       return list;
+error:
+       FN(EL,free)(el);
+       FN(LIST(EL),free)(list);
+       return NULL;
+}
+
 int FN(LIST(EL),foreach)(__isl_keep LIST(EL) *list,
        int (*fn)(__isl_take EL *el, void *user), void *user)
 {
@@ -119,6 +283,50 @@ int FN(LIST(EL),foreach)(__isl_keep LIST(EL) *list,
        return 0;
 }
 
+__isl_give LIST(EL) *FN(FN(LIST(EL),from),BASE)(__isl_take EL *el)
+{
+       isl_ctx *ctx;
+       LIST(EL) *list;
+
+       if (!el)
+               return NULL;
+       ctx = FN(EL,get_ctx)(el);
+       list = FN(LIST(EL),alloc)(ctx, 1);
+       if (!list)
+               goto error;
+       list = FN(LIST(EL),add)(list, el);
+       return list;
+error:
+       FN(EL,free)(el);
+       return NULL;
+}
+
+__isl_give LIST(EL) *FN(LIST(EL),concat)(__isl_take LIST(EL) *list1,
+       __isl_take LIST(EL) *list2)
+{
+       int i;
+       isl_ctx *ctx;
+       LIST(EL) *res;
+
+       if (!list1 || !list2)
+               goto error;
+
+       ctx = FN(LIST(EL),get_ctx)(list1);
+       res = FN(LIST(EL),alloc)(ctx, list1->n + list2->n);
+       for (i = 0; i < list1->n; ++i)
+               res = FN(LIST(EL),add)(res, FN(EL,copy)(list1->p[i]));
+       for (i = 0; i < list2->n; ++i)
+               res = FN(LIST(EL),add)(res, FN(EL,copy)(list2->p[i]));
+
+       FN(LIST(EL),free)(list1);
+       FN(LIST(EL),free)(list2);
+       return res;
+error:
+       FN(LIST(EL),free)(list1);
+       FN(LIST(EL),free)(list2);
+       return NULL;
+}
+
 __isl_give isl_printer *CAT(isl_printer_print_,LIST(BASE))(
        __isl_take isl_printer *p, __isl_keep LIST(EL) *list)
 {