diff options
Diffstat (limited to 'src/libmdsserver')
-rw-r--r-- | src/libmdsserver/client-list.c | 16 | ||||
-rw-r--r-- | src/libmdsserver/fd-table.c | 10 | ||||
-rw-r--r-- | src/libmdsserver/linked-list.c | 36 | ||||
-rw-r--r-- | src/libmdsserver/macros.h | 16 | ||||
-rw-r--r-- | src/libmdsserver/mds-message.c | 5 |
5 files changed, 29 insertions, 54 deletions
diff --git a/src/libmdsserver/client-list.c b/src/libmdsserver/client-list.c index 33b6844..f06b9b5 100644 --- a/src/libmdsserver/client-list.c +++ b/src/libmdsserver/client-list.c @@ -100,27 +100,15 @@ void client_list_destroy(client_list_t* restrict this) */ int client_list_clone(const client_list_t* restrict this, client_list_t* restrict out) { - size_t n = this->capacity * sizeof(uint64_t); - uint64_t* restrict new_clients = NULL; - int saved_errno; - - out->clients = NULL; - - fail_if (xbmalloc(new_clients, n)); - - out->clients = new_clients; + fail_if (xmemdup(out->clients, this->clients, this->capacity, uint64_t)); out->capacity = this->capacity; out->size = this->size; - memcpy(out->clients, this->clients, n); - return 0; fail: - saved_errno = errno; - free(new_clients); - return errno = saved_errno, -1; + return -1; } diff --git a/src/libmdsserver/fd-table.c b/src/libmdsserver/fd-table.c index 4623825..b5d8f8c 100644 --- a/src/libmdsserver/fd-table.c +++ b/src/libmdsserver/fd-table.c @@ -278,6 +278,7 @@ void fd_table_marshal(const fd_table_t* restrict this, char* restrict data) int fd_table_unmarshal(fd_table_t* restrict this, char* restrict data, remap_func* remapper) { size_t bitcap; + size_t i; /* buf_get(data, int, 0, FD_TABLE_T_VERSION) */ buf_next(data, int, 1); @@ -300,12 +301,9 @@ int fd_table_unmarshal(fd_table_t* restrict this, char* restrict data, remap_fun memcpy(this->used, data, bitcap * sizeof(uint64_t)); if (remapper != NULL) - { - size_t i; - for (i = 0; i < this->capacity; i++) - if (this->used[i / 64] & ((uint64_t)1 << (i % 64))) - this->values[i] = remapper(this->values[i]); - } + for (i = 0; i < this->capacity; i++) + if (this->used[i / 64] & ((uint64_t)1 << (i % 64))) + this->values[i] = remapper(this->values[i]); return 0; fail: diff --git a/src/libmdsserver/linked-list.c b/src/libmdsserver/linked-list.c index 1dea87a..602bc03 100644 --- a/src/libmdsserver/linked-list.c +++ b/src/libmdsserver/linked-list.c @@ -113,46 +113,20 @@ void linked_list_destroy(linked_list_t* restrict this) */ int linked_list_clone(const linked_list_t* restrict this, linked_list_t* restrict out) { - size_t n = this->capacity * sizeof(ssize_t); - size_t* restrict new_values = NULL; - 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; - - fail_if (xbmalloc(new_values, n)); - fail_if (xbmalloc(new_next, n)); - fail_if (xbmalloc(new_previous, n)); - fail_if (xbmalloc(new_reusable, n)); - - out->values = new_values; - out->next = new_next; - out->previous = new_previous; - out->reusable = new_reusable; + fail_if (xmemdup(out->values, this->values, this->capacity, size_t)); + fail_if (xmemdup(out->next, this->next, this->capacity, ssize_t)); + fail_if (xmemdup(out->previous, this->previous, this->capacity, ssize_t)); + fail_if (xmemdup(out->reusable, this->reusable, this->capacity, ssize_t)); out->capacity = this->capacity; out->end = this->end; out->reuse_head = this->reuse_head; out->edge = this->edge; - memcpy(out->values, this->values, n); - memcpy(out->next, this->next, n); - memcpy(out->previous, this->previous, n); - memcpy(out->reusable, this->reusable, n); - return 0; fail: - saved_errno = errno; - free(new_values); - free(new_next); - free(new_previous); - return errno = saved_errno, -1; + return -1; } diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h index 672c0a3..991ea8f 100644 --- a/src/libmdsserver/macros.h +++ b/src/libmdsserver/macros.h @@ -437,6 +437,22 @@ /** + * `malloc` and `memcpy` wrapper that creates a duplicate of a pointer and + * returns whether the allocation was not successful + * + * @param var:void* The variable to which to assign the duplicate + * @param original:const void* The buffer to duplicate + * @param elements:size_t The number of elements to duplicate + * @param type The data type of the elements to duplicate + * @return :int Evaluates to true if an only if the allocation failed + */ +#define xmemdup(var, original, elements, type) \ + (((var = malloc((elements) * sizeof(type))) == NULL) ? 1 : \ + (memcpy(var, original, (elements) * sizeof(type)), 0)) + + + +/** * Call `perror` if `errno` is non-zero and set `errno` to zero * * @param str:const char* The argument passed to `perror` diff --git a/src/libmdsserver/mds-message.c b/src/libmdsserver/mds-message.c index 5007f23..1676023 100644 --- a/src/libmdsserver/mds-message.c +++ b/src/libmdsserver/mds-message.c @@ -254,7 +254,7 @@ static int store_header(mds_message_t* restrict this, size_t length) char* header; /* Allocate the header. */ - fail_if (xmalloc(header, length, char)); + fail_if (xmalloc(header, length, char)); /* FIXME this do not feel right (but it works perfectly...) */ /* Copy the header data into the allocated header, */ memcpy(header, this->buffer, length * sizeof(char)); /* and NUL-terminate it. */ @@ -530,8 +530,7 @@ int mds_message_unmarshal(mds_message_t* restrict this, char* restrict data) for (i = 0; i < this->header_count; i++) { n = strlen(data) + 1; - fail_if (xmalloc(this->headers[i], n, char)); - memcpy(this->headers[i], data, n * sizeof(char)); + fail_if (xmemdup(this->headers[i], data, n, char)); buf_next(data, char, n); this->header_count++; } |