aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmdsserver
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/libmdsserver/client-list.c21
-rw-r--r--src/libmdsserver/fd-table.c20
-rw-r--r--src/libmdsserver/hash-table.c31
-rw-r--r--src/libmdsserver/linked-list.c72
-rw-r--r--src/libmdsserver/macros.h16
-rw-r--r--src/libmdsserver/mds-message.c37
-rw-r--r--src/libmdsserver/util.c44
7 files changed, 135 insertions, 106 deletions
diff --git a/src/libmdsserver/client-list.c b/src/libmdsserver/client-list.c
index 66fc2ae..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;
}
@@ -101,11 +102,11 @@ int client_list_clone(const client_list_t* restrict this, client_list_t* restric
{
size_t n = this->capacity * sizeof(uint64_t);
uint64_t* restrict new_clients = NULL;
+ int saved_errno;
out->clients = NULL;
- if ((new_clients = malloc(n)) == NULL)
- goto fail;
+ fail_if ((new_clients = malloc(n)) == NULL);
out->clients = new_clients;
@@ -117,8 +118,9 @@ int client_list_clone(const client_list_t* restrict this, client_list_t* restric
return 0;
fail:
+ saved_errno = errno;
free(new_clients);
- return -1;
+ return errno = saved_errno, -1;
}
@@ -138,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;
}
@@ -229,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 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;
}
diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h
index 6c8f944..b72fd0a 100644
--- a/src/libmdsserver/macros.h
+++ b/src/libmdsserver/macros.h
@@ -399,11 +399,21 @@
/**
- * Go to the label `pfail` if a condition is met
+ * Go to the label `fail` if a condition is met
*
* @param ... The condition
*/
-#define fail_if(...) if (__VA_ARGS__) goto pfail
+#define fail_if(...) \
+ do \
+ if (__VA_ARGS__) \
+ { \
+ int _fail_if_saved_errno = errno; \
+ if ((errno != EMSGSIZE) && (errno != ECONNRESET) && (errno != EINTR)) \
+ fprintf(stderr, "failure at %s:%i\n", __FILE__, __LINE__); \
+ errno = _fail_if_saved_errno; \
+ goto fail; \
+ } \
+ while (0)
/**
@@ -412,7 +422,7 @@
* @param condition The condition
* @param instructions The instruction (semicolon-terminated)
*/
-#define exit_if(condition, instructions) if (condition) { instructions return 1; }
+#define exit_if(condition, instructions) do { if (condition) { instructions return 1; } } while (0)
/**
diff --git a/src/libmdsserver/mds-message.c b/src/libmdsserver/mds-message.c
index 37ebf85..5007f23 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;
}
@@ -100,10 +101,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;
}
@@ -116,11 +118,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;
}
@@ -231,10 +234,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;
}
@@ -250,8 +254,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. */
@@ -272,6 +275,8 @@ static int store_header(mds_message_t* restrict this, size_t length)
this->headers[this->header_count++] = header;
return 0;
+ fail:
+ return -1;
}
@@ -305,15 +310,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;
}
@@ -540,7 +543,7 @@ int mds_message_unmarshal(mds_message_t* restrict this, char* restrict data)
return 0;
- pfail:
+ fail:
return -1;
}
diff --git a/src/libmdsserver/util.c b/src/libmdsserver/util.c
index b1dff3c..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;
}
@@ -360,8 +359,7 @@ pid_t uninterruptable_waitpid(pid_t pid, int* restrict status, int options)
rc = waitpid(pid, status, options);
if (rc == (pid_t)-1)
{
- if (errno != EINTR)
- goto fail;
+ fail_if (errno != EINTR);
if (have_time && (monotone(&time_intr) >= 0))
if (time_start.tv_sec != time_intr.tv_sec)
intr_count = 0;