diff options
Diffstat (limited to 'src/libmdsserver/linked-list.c')
-rw-r--r-- | src/libmdsserver/linked-list.c | 72 |
1 files changed, 40 insertions, 32 deletions
diff --git a/src/libmdsserver/linked-list.c b/src/libmdsserver/linked-list.c index 6c8e185..f00daf3 100644 --- a/src/libmdsserver/linked-list.c +++ b/src/libmdsserver/linked-list.c @@ -75,15 +75,17 @@ int linked_list_create(linked_list_t* restrict this, size_t capacity) this->values = NULL; this->next = NULL; this->previous = NULL; - if (xmalloc(this->reusable, capacity, ssize_t)) return -1; - if (xmalloc(this->values, capacity, size_t)) return -1; - if (xmalloc(this->next, capacity, ssize_t)) return -1; - if (xmalloc(this->previous, capacity, ssize_t)) return -1; + fail_if (xmalloc(this->reusable, capacity, ssize_t)); + fail_if (xmalloc(this->values, capacity, size_t)); + fail_if (xmalloc(this->next, capacity, ssize_t)); + fail_if (xmalloc(this->previous, capacity, ssize_t)); this->values[this->edge] = 0; this->next[this->edge] = this->edge; this->previous[this->edge] = this->edge; return 0; + fail: + return -1; } @@ -95,10 +97,10 @@ int linked_list_create(linked_list_t* restrict this, size_t capacity) */ void linked_list_destroy(linked_list_t* restrict this) { - free(this->reusable); this->reusable = NULL; - free(this->values); this->values = NULL; - free(this->next); this->next = NULL; - free(this->previous); this->previous = NULL; + free(this->reusable), this->reusable = NULL; + free(this->values), this->values = NULL; + free(this->next), this->next = NULL; + free(this->previous), this->previous = NULL; } @@ -116,16 +118,17 @@ int linked_list_clone(const linked_list_t* restrict this, linked_list_t* restric ssize_t* restrict new_next = NULL; ssize_t* restrict new_previous = NULL; ssize_t* restrict new_reusable; + int saved_errno; out->values = NULL; out->next = NULL; out->previous = NULL; out->reusable = NULL; - if ((new_values = malloc(n)) == NULL) goto fail; - if ((new_next = malloc(n)) == NULL) goto fail; - if ((new_previous = malloc(n)) == NULL) goto fail; - if ((new_reusable = malloc(n)) == NULL) goto fail; + fail_if ((new_values = malloc(n)) == NULL); + fail_if ((new_next = malloc(n)) == NULL); + fail_if ((new_previous = malloc(n)) == NULL); + fail_if ((new_reusable = malloc(n)) == NULL); out->values = new_values; out->next = new_next; @@ -145,10 +148,11 @@ int linked_list_clone(const linked_list_t* restrict this, linked_list_t* restric return 0; fail: + saved_errno = errno; free(new_values); free(new_next); free(new_previous); - return -1; + return errno = saved_errno, -1; } @@ -177,9 +181,9 @@ int linked_list_pack(linked_list_t* restrict this) size_t i = 0; ssize_t node; size_t* restrict vals; + int saved_errno; - if (xmalloc(vals, cap, size_t)) - return -1; + fail_if (xmalloc(vals, cap, size_t)); while (((size_t)head != this->end) && (this->next[head] == LINKED_LIST_UNUSED)) head++; if ((size_t)head != this->end) @@ -191,9 +195,9 @@ int linked_list_pack(linked_list_t* restrict this) if (cap != this->capacity) { - if (xmalloc(new_next, cap, ssize_t)) goto fail; - if (xmalloc(new_previous, cap, ssize_t)) goto fail; - if (xmalloc(new_reusable, cap, ssize_t)) goto fail; + fail_if (xmalloc(new_next, cap, ssize_t)); + fail_if (xmalloc(new_previous, cap, ssize_t)); + fail_if (xmalloc(new_reusable, cap, ssize_t)); free(this->next); free(this->previous); @@ -219,10 +223,11 @@ int linked_list_pack(linked_list_t* restrict this) return 0; fail: + saved_errno = errno; free(vals); free(new_next); free(new_previous); - return -1; + return errno = saved_errno, -1; } @@ -245,10 +250,7 @@ static ssize_t linked_list_get_next(linked_list_t* restrict this) ssize_t* old; if ((ssize_t)(this->end) < 0) - { - errno = ENOMEM; - return LINKED_LIST_UNUSED; - } + fail_if ((errno = ENOMEM)); this->capacity <<= 1; @@ -256,7 +258,7 @@ static ssize_t linked_list_get_next(linked_list_t* restrict this) if ((new_var = realloc(old_var = new_var, this->capacity * sizeof(type))) == NULL) \ { \ new_var = old_var; \ - return LINKED_LIST_UNUSED; \ + fail_if (1); \ } __realloc(this->values, old_values, size_t) @@ -267,6 +269,8 @@ static ssize_t linked_list_get_next(linked_list_t* restrict this) #undef __realloc } return (ssize_t)(this->end++); + fail: + return LINKED_LIST_UNUSED; } @@ -300,14 +304,15 @@ static ssize_t linked_list_unuse(linked_list_t* restrict this, ssize_t node) ssize_t linked_list_insert_after(linked_list_t* this, size_t value, ssize_t predecessor) { ssize_t node = linked_list_get_next(this); - if (node == LINKED_LIST_UNUSED) - return LINKED_LIST_UNUSED; + fail_if (node == LINKED_LIST_UNUSED); this->values[node] = value; this->next[node] = this->next[predecessor]; this->next[predecessor] = node; this->previous[node] = predecessor; this->previous[this->next[node]] = node; return node; + fail: + return LINKED_LIST_UNUSED; } @@ -339,14 +344,15 @@ ssize_t linked_list_remove_after(linked_list_t* restrict this, ssize_t predecess ssize_t linked_list_insert_before(linked_list_t* restrict this, size_t value, ssize_t successor) { ssize_t node = linked_list_get_next(this); - if (node == LINKED_LIST_UNUSED) - return LINKED_LIST_UNUSED; + fail_if (node == LINKED_LIST_UNUSED); this->values[node] = value; this->previous[node] = this->previous[successor]; this->previous[successor] = node; this->next[node] = successor; this->next[this->previous[node]] = node; return node; + fail: + return LINKED_LIST_UNUSED; } @@ -450,10 +456,10 @@ int linked_list_unmarshal(linked_list_t* restrict this, char* restrict data) n = this->capacity * sizeof(size_t); - if ((this->reusable = malloc(n)) == NULL) return -1; - if ((this->values = malloc(n)) == NULL) return -1; - if ((this->next = malloc(n)) == NULL) return -1; - if ((this->previous = malloc(n)) == NULL) return -1; + fail_if ((this->reusable = malloc(n)) == NULL); + fail_if ((this->values = malloc(n)) == NULL); + fail_if ((this->next = malloc(n)) == NULL); + fail_if ((this->previous = malloc(n)) == NULL); memcpy(this->reusable, data, this->reuse_head * sizeof(ssize_t)); buf_next(data, ssize_t, this->reuse_head); @@ -467,6 +473,8 @@ int linked_list_unmarshal(linked_list_t* restrict this, char* restrict data) memcpy(this->previous, data, this->end * sizeof(ssize_t)); return 0; + fail: + return -1; } |