diff options
Diffstat (limited to 'src/libmdsserver')
-rw-r--r-- | src/libmdsserver/client-list.c | 14 | ||||
-rw-r--r-- | src/libmdsserver/fd-table.c | 20 | ||||
-rw-r--r-- | src/libmdsserver/hash-table.c | 31 | ||||
-rw-r--r-- | src/libmdsserver/linked-list.c | 42 | ||||
-rw-r--r-- | src/libmdsserver/mds-message.c | 35 | ||||
-rw-r--r-- | src/libmdsserver/util.c | 41 |
6 files changed, 99 insertions, 84 deletions
diff --git a/src/libmdsserver/client-list.c b/src/libmdsserver/client-list.c index ff3ffe2..24403cb 100644 --- a/src/libmdsserver/client-list.c +++ b/src/libmdsserver/client-list.c @@ -70,10 +70,11 @@ int client_list_create(client_list_t* restrict this, size_t capacity) this->capacity = capacity = to_power_of_two(capacity); this->size = 0; this->clients = NULL; - if (xmalloc(this->clients, capacity, uint64_t)) - return -1; + fail_if (xmalloc(this->clients, capacity, uint64_t)); return 0; + fail: + return -1; } @@ -139,12 +140,14 @@ int client_list_add(client_list_t* restrict this, uint64_t client) { this->capacity >>= 1; this->clients = old; - return -1; + fail_if (1); } } this->clients[this->size++] = client; return 0; + fail: + return -1; } @@ -230,13 +233,14 @@ int client_list_unmarshal(client_list_t* restrict this, char* restrict data) n = this->capacity * sizeof(uint64_t); - if ((this->clients = malloc(n)) == NULL) - return -1; + fail_if ((this->clients = malloc(n)) == NULL); n = this->size * sizeof(uint64_t); memcpy(this->clients, data, n); return 0; + fail: + return -1; } diff --git a/src/libmdsserver/fd-table.c b/src/libmdsserver/fd-table.c index 960b3e4..4623825 100644 --- a/src/libmdsserver/fd-table.c +++ b/src/libmdsserver/fd-table.c @@ -48,10 +48,12 @@ int fd_table_create_tuned(fd_table_t* restrict this, size_t initial_capacity) the time overhead of `fd_table_contains_value`. */ bitcap = (this->capacity + 63) / 64; - if (xcalloc(this->used, bitcap, size_t)) return -1; - if (xcalloc(this->values, this->capacity, size_t)) return -1; + fail_if (xcalloc(this->used, bitcap, size_t)); + fail_if (xcalloc(this->values, this->capacity, size_t)); return 0; + fail: + return -1; } @@ -164,7 +166,7 @@ size_t fd_table_put(fd_table_t* restrict this, int key, size_t value) if (xrealloc(this->values, this->capacity << 1, size_t)) { this->values = old_values; - return 0; + fail_if (1); } memset(this->values + this->capacity, 0, this->capacity * sizeof(size_t)); @@ -180,7 +182,7 @@ size_t fd_table_put(fd_table_t* restrict this, int key, size_t value) { this->used = old_used; this->capacity >>= 1; - return 0; + fail_if (1); } memset(this->used + old_bitcap, 0, (new_bitcap - old_bitcap) * sizeof(uint64_t)); @@ -192,6 +194,8 @@ size_t fd_table_put(fd_table_t* restrict this, int key, size_t value) this->values[key] = value; this->size++; return 0; + fail: + return 0; } @@ -285,12 +289,10 @@ int fd_table_unmarshal(fd_table_t* restrict this, char* restrict data, remap_fun this->used = NULL; this->value_comparator = NULL; - if (xmalloc(this->values, this->capacity, size_t)) - return -1; + fail_if (xmalloc(this->values, this->capacity, size_t)); bitcap = (this->capacity + 63) / 64; - if (xmalloc(this->used, bitcap, size_t)) - return -1; + fail_if (xmalloc(this->used, bitcap, size_t)); memcpy(this->values, data, this->capacity * sizeof(size_t)); buf_next(data, size_t, this->capacity); @@ -306,5 +308,7 @@ int fd_table_unmarshal(fd_table_t* restrict this, char* restrict data, remap_fun } return 0; + fail: + return -1; } diff --git a/src/libmdsserver/hash-table.c b/src/libmdsserver/hash-table.c index c0de355..c9b2f4d 100644 --- a/src/libmdsserver/hash-table.c +++ b/src/libmdsserver/hash-table.c @@ -65,7 +65,7 @@ static inline size_t __attribute__((pure)) truncate_hash(const hash_table_t* res * Grow the table * * @param this The hash table - * @return Non zero on error, `errno` will be set accordingly + * @return Non-zero on error, `errno` will be set accordingly */ static int rehash(hash_table_t* restrict this) { @@ -76,8 +76,7 @@ static int rehash(hash_table_t* restrict this) hash_entry_t* destination; hash_entry_t* next; - if (xcalloc(this->buckets, old_capacity * 2 + 1, hash_entry_t*)) - return -1; + fail_if (xcalloc(this->buckets, old_capacity * 2 + 1, hash_entry_t*)); this->capacity = old_capacity * 2 + 1; this->threshold = (size_t)((float)(this->capacity) * this->load_factor); @@ -108,6 +107,8 @@ static int rehash(hash_table_t* restrict this) free(old_buckets); return 0; + fail: + return -1; } @@ -124,8 +125,7 @@ int hash_table_create_fine_tuned(hash_table_t* restrict this, size_t initial_cap this->buckets = NULL; this->capacity = initial_capacity ? initial_capacity : 1; - if (xcalloc(this->buckets, this->capacity, hash_entry_t*)) - return -1; + fail_if (xcalloc(this->buckets, this->capacity, hash_entry_t*)); this->load_factor = load_factor; this->threshold = (size_t)((float)(this->capacity) * load_factor); this->size = 0; @@ -134,6 +134,8 @@ int hash_table_create_fine_tuned(hash_table_t* restrict this, size_t initial_cap this->hasher = NULL; return 0; + fail: + return -1; } @@ -299,14 +301,12 @@ size_t hash_table_put(hash_table_t* restrict this, size_t key, size_t value) if (++(this->size) > this->threshold) { errno = 0; - if (rehash(this)) - return 0; + fail_if (rehash(this)); index = truncate_hash(this, key_hash); } errno = 0; - if (xmalloc(bucket, 1, hash_entry_t)) - return 0; + fail_if (xmalloc(bucket, 1, hash_entry_t)); bucket->value = value; bucket->key = key; bucket->hash = key_hash; @@ -314,6 +314,8 @@ size_t hash_table_put(hash_table_t* restrict this, size_t key, size_t value) this->buckets[index] = bucket; return 0; + fail: + return 0; } @@ -472,8 +474,7 @@ int hash_table_unmarshal(hash_table_t* restrict this, char* restrict data, remap buf_get_next(data, size_t, this->threshold); buf_get_next(data, size_t, this->size); - if (xcalloc(this->buckets, this->capacity, hash_entry_t*)) - return -1; + fail_if (xcalloc(this->buckets, this->capacity, hash_entry_t*)); for (i = 0; i < n; i++) { @@ -481,16 +482,14 @@ int hash_table_unmarshal(hash_table_t* restrict this, char* restrict data, remap hash_entry_t* restrict bucket; buf_get_next(data, size_t, m); - if (xmalloc(this->buckets[i] = bucket, 1, hash_entry_t)) - return -1; + fail_if (xmalloc(this->buckets[i] = bucket, 1, hash_entry_t)); while (m--) { if (m == 0) bucket->next = NULL; else - if (xmalloc(bucket->next, 1, hash_entry_t)) - return -1; + fail_if (xmalloc(bucket->next, 1, hash_entry_t)); buf_get_next(data, size_t, bucket->key); buf_get_next(data, size_t, bucket->value); if (remapper != NULL) @@ -500,5 +499,7 @@ int hash_table_unmarshal(hash_table_t* restrict this, char* restrict data, remap } return 0; + fail: + return -1; } diff --git a/src/libmdsserver/linked-list.c b/src/libmdsserver/linked-list.c index 7756d90..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; } @@ -181,8 +183,7 @@ int linked_list_pack(linked_list_t* restrict this) 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) @@ -249,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; @@ -260,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) @@ -271,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; } @@ -304,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; } @@ -343,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; } @@ -454,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); @@ -471,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; } diff --git a/src/libmdsserver/mds-message.c b/src/libmdsserver/mds-message.c index 4075f57..68334de 100644 --- a/src/libmdsserver/mds-message.c +++ b/src/libmdsserver/mds-message.c @@ -48,9 +48,10 @@ int mds_message_initialise(mds_message_t* restrict this) this->buffer_size = 128; this->buffer_ptr = 0; this->stage = 0; - if (xmalloc(this->buffer, this->buffer_size, char)) - return -1; + fail_if (xmalloc(this->buffer, this->buffer_size, char)); return 0; + fail: + return -1; } @@ -102,10 +103,11 @@ void mds_message_destroy(mds_message_t* restrict this) int mds_message_extend_headers(mds_message_t* restrict this, size_t extent) { char** new_headers = this->headers; - if (xrealloc(new_headers, this->header_count + extent, char*)) - return -1; + fail_if (xrealloc(new_headers, this->header_count + extent, char*)); this->headers = new_headers; return 0; + fail: + return -1; } @@ -118,11 +120,12 @@ int mds_message_extend_headers(mds_message_t* restrict this, size_t extent) static int mds_message_extend_buffer(mds_message_t* restrict this) { char* new_buf = this->buffer; - if (xrealloc(new_buf, this->buffer_size << 1, char)) - return -1; + fail_if (xrealloc(new_buf, this->buffer_size << 1, char)); this->buffer = new_buf; this->buffer_size <<= 1; return 0; + fail: + return -1; } @@ -236,10 +239,11 @@ static int initialise_payload(mds_message_t* restrict this) /* Allocate the payload buffer. */ if (this->payload_size > 0) - if (xmalloc(this->payload, this->payload_size, char)) - return -1; + fail_if (xmalloc(this->payload, this->payload_size, char)); return 0; + fail: + return -1; } @@ -255,8 +259,7 @@ static int store_header(mds_message_t* restrict this, size_t length) char* header; /* Allocate the header. */ - if (xmalloc(header, length, char)) - return -1; + fail_if (xmalloc(header, length, char)); /* Copy the header data into the allocated header, */ memcpy(header, this->buffer, length * sizeof(char)); /* and NUL-terminate it. */ @@ -277,6 +280,8 @@ static int store_header(mds_message_t* restrict this, size_t length) this->headers[this->header_count++] = header; return 0; + fail: + return -1; } @@ -310,15 +315,13 @@ static int continue_read(mds_message_t* restrict this, int fd) errno = 0; got = recv(fd, this->buffer + this->buffer_ptr, n, 0); this->buffer_ptr += (size_t)(got < 0 ? 0 : got); - if (errno) - return -1; + fail_if (errno); if (got == 0) - { - errno = ECONNRESET; - return -1; - } + fail_if ((errno = ECONNRESET)); return 0; + fail: + return -1; } diff --git a/src/libmdsserver/util.c b/src/libmdsserver/util.c index 2e72229..123463c 100644 --- a/src/libmdsserver/util.c +++ b/src/libmdsserver/util.c @@ -103,11 +103,12 @@ int prepare_reexec(void) { ssize_t len; len = readlink(SELF_EXE, self_exe, (sizeof(self_exe) / sizeof(char)) - 1); - if (len < 0) - return -1; + fail_if (len < 0); /* ‘readlink() does not append a null byte to buf.’ */ self_exe[len] = '\0'; return 0; + fail: + return -1; } @@ -135,8 +136,7 @@ void reexec_server(int argc, char** argv, int reexeced) { *reexec_args_++ = *argv; *reexec_args_ = strdup("--re-exec"); - if (*reexec_args_ == NULL) - return; + fail_if (*reexec_args_ == NULL); for (i = 1; i < argc; i++) reexec_args_[i] = argv[i]; } @@ -146,6 +146,8 @@ void reexec_server(int argc, char** argv, int reexeced) reexec_args_[i] = argv[i]; reexec_args_[argc] = NULL; execv(self_exe[0] ? self_exe : argv[0], reexec_args); + fail: + return; } @@ -253,12 +255,13 @@ int full_write(int fd, const char* buffer, size_t length) { errno = 0; wrote = write(fd, buffer, length); - if (errno && (errno != EINTR)) - return -1; + fail_if (errno && (errno != EINTR)); length -= (size_t)max(wrote, 0); buffer += (size_t)max(wrote, 0); } return 0; + fail: + return -1; } @@ -271,38 +274,29 @@ int full_write(int fd, const char* buffer, size_t length) */ char* full_read(int fd, size_t* length) { + char* old_buf = NULL; size_t buffer_size = 8 << 10; size_t buffer_ptr = 0; char* buffer; ssize_t got; + int saved_errno; + if (length != NULL) *length = 0; /* Allocate buffer for data. */ - if (xmalloc(buffer, buffer_size, char)) - return NULL; + fail_if (xmalloc(buffer, buffer_size, char)); /* Read the file. */ for (;;) { /* Grow buffer if it is too small. */ if (buffer_size == buffer_ptr) - { - char* old_buf = buffer; - if (xrealloc(buffer, buffer_size <<= 1, char)) - { - free(old_buf); - return NULL; - } - } + fail_if (xxrealloc(old_buf, buffer, buffer_size <<= 1, char)); /* Read from the file into the buffer. */ got = read(fd, buffer + buffer_ptr, buffer_size - buffer_ptr); - if ((got < 0) && (errno != EINTR)) - { - free(buffer); - return NULL; - } + fail_if ((got < 0) && (errno != EINTR)); if (got == 0) break; buffer_ptr += (size_t)got; @@ -311,6 +305,11 @@ char* full_read(int fd, size_t* length) if (length != NULL) *length = buffer_ptr; return buffer; + fail: + saved_errno = errno; + free(old_buf); + free(buffer); + return errno = saved_errno, NULL; } |