From 36243244e8f4759a22fabd64d22553d373bcf165 Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Thu, 17 May 2018 14:49:03 +0200 Subject: [PATCH] isl_multi_*_set_*: extract out isl_multi_*_restore This function will be reused in the next commit. Signed-off-by: Sven Verdoolaege --- isl_multi_templ.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/isl_multi_templ.c b/isl_multi_templ.c index b3b9e280..c1eab387 100644 --- a/isl_multi_templ.c +++ b/isl_multi_templ.c @@ -217,6 +217,30 @@ __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi, return FN(EL,copy)(multi->p[pos]); } +/* Set the element at position "pos" of "multi" to "el", + * where the position may be empty if "multi" has only a single reference. + */ +static __isl_give MULTI(BASE) *FN(MULTI(BASE),restore)( + __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el) +{ + multi = FN(MULTI(BASE),cow)(multi); + if (!multi || !el) + goto error; + + if (pos < 0 || pos >= multi->n) + isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid, + "index out of bounds", goto error); + + FN(EL,free)(multi->p[pos]); + multi->p[pos] = el; + + return multi; +error: + FN(MULTI(BASE),free)(multi); + FN(EL,free)(el); + return NULL; +} + __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)( __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el) { @@ -224,10 +248,6 @@ __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)( isl_space *el_space = NULL; isl_bool match; - multi = FN(MULTI(BASE),cow)(multi); - if (!multi || !el) - goto error; - multi_space = FN(MULTI(BASE),get_space)(multi); match = FN(EL,matching_params)(el, multi_space); if (match < 0) @@ -242,12 +262,7 @@ __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)( if (FN(EL,check_match_domain_space)(el, multi_space) < 0) goto error; - if (pos < 0 || pos >= multi->n) - isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid, - "index out of bounds", goto error); - - FN(EL,free)(multi->p[pos]); - multi->p[pos] = el; + multi = FN(MULTI(BASE),restore)(multi, pos, el); isl_space_free(multi_space); isl_space_free(el_space); -- 2.11.4.GIT