aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-12-10 02:58:25 +0100
committerMattias Andrée <maandree@operamail.com>2014-12-10 02:58:25 +0100
commit344273a7e0a6899451836e6072fecebea4a6ac24 (patch)
treeea24fa9e12ae6dad40eeb6470c92545e14f0f8cf /src
parentno more direct allocations, always use macros, unless using alloca (diff)
downloadmds-344273a7e0a6899451836e6072fecebea4a6ac24.tar.gz
mds-344273a7e0a6899451836e6072fecebea4a6ac24.tar.bz2
mds-344273a7e0a6899451836e6072fecebea4a6ac24.tar.xz
add xmemdup macro
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src')
-rw-r--r--src/libmdsserver/client-list.c16
-rw-r--r--src/libmdsserver/fd-table.c10
-rw-r--r--src/libmdsserver/linked-list.c36
-rw-r--r--src/libmdsserver/macros.h16
-rw-r--r--src/libmdsserver/mds-message.c5
-rw-r--r--src/mds-clipboard.c6
-rw-r--r--src/mds-kbdc/include-stack.c3
-rw-r--r--src/mds-kbdc/raw-data.c3
-rw-r--r--src/mds-kbdc/string.c3
-rw-r--r--src/mds-kkbd.c3
-rw-r--r--src/mds-registry/registry.c3
-rw-r--r--src/mds-registry/slave.c21
-rw-r--r--src/mds-server/client.c6
-rw-r--r--src/mds-server/interception-condition.c7
-rw-r--r--src/mds-server/multicast.c3
-rw-r--r--src/mds-server/receiving.c3
16 files changed, 53 insertions, 91 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++;
}
diff --git a/src/mds-clipboard.c b/src/mds-clipboard.c
index 50ee6a3..a2c3a0f 100644
--- a/src/mds-clipboard.c
+++ b/src/mds-clipboard.c
@@ -299,8 +299,7 @@ int unmarshal_server(char* state_buf)
buf_get_next(state_buf, long, clip->dethklok.tv_nsec);
buf_get_next(state_buf, uint64_t, clip->client);
buf_get_next(state_buf, int, clip->autopurge);
- fail_if (xmalloc(clip->content, clip->length, char));
- memcpy(clip->content, state_buf, clip->length * sizeof(char));
+ fail_if (xmemdup(clip->content, state_buf, clip->length, char));
state_buf += clip->length;
}
}
@@ -721,8 +720,7 @@ int clipboard_add(int level, const char* time_to_live, const char* recv_client_i
new_clip.autopurge = autopurge;
new_clip.length = received.payload_size;
- fail_if (xmalloc(new_clip.content, new_clip.length, char));
- memcpy(new_clip.content, received.payload, new_clip.length * sizeof(char));
+ fail_if (xmemdup(new_clip.content, received.payload, new_clip.length, char));
if (clipboard_used[level] == clipboard_size[level])
free_clipboard_entry(clipboard[level] + clipboard_used[level] - 1);
diff --git a/src/mds-kbdc/include-stack.c b/src/mds-kbdc/include-stack.c
index b34b57b..d9225a3 100644
--- a/src/mds-kbdc/include-stack.c
+++ b/src/mds-kbdc/include-stack.c
@@ -199,8 +199,7 @@ mds_kbdc_include_stack_t* mds_kbdc_include_stack_save(void)
if (latest_save->ptr == 0)
return latest_save;
- fail_if (xmalloc(latest_save->stack, latest_save->ptr, const mds_kbdc_tree_include_t*));
- memcpy(latest_save->stack, includes, latest_save->ptr * sizeof(const mds_kbdc_tree_include_t*));
+ fail_if (xmemdup(latest_save->stack, includes, latest_save->ptr, const mds_kbdc_tree_include_t*));
return latest_save;
fail:
diff --git a/src/mds-kbdc/raw-data.c b/src/mds-kbdc/raw-data.c
index cf48486..d8976ec 100644
--- a/src/mds-kbdc/raw-data.c
+++ b/src/mds-kbdc/raw-data.c
@@ -408,8 +408,7 @@ int read_source_lines(const char* restrict pathname, mds_kbdc_source_code_t* res
}
/* Simplify file. */
- fail_if (xmalloc(real_content, content_size, char));
- memcpy(real_content, content, content_size * sizeof(char));
+ fail_if (xmemdup(real_content, content, content_size, char));
real_content_size = content_size;
content_size = remove_comments(content, content_size);
fail_if (xxrealloc(old, content, content_size, char));
diff --git a/src/mds-kbdc/string.c b/src/mds-kbdc/string.c
index 6ecf757..843757b 100644
--- a/src/mds-kbdc/string.c
+++ b/src/mds-kbdc/string.c
@@ -145,8 +145,7 @@ char32_t* string_dup(const char32_t* restrict string)
if (string == NULL)
return NULL;
n = string_length(string) + 1;
- fail_if (xmalloc(rc, n, char32_t));
- memcpy(rc, string, n * sizeof(char32_t));
+ fail_if (xmemdup(rc, string, n, char32_t));
return rc;
fail:
return NULL;
diff --git a/src/mds-kkbd.c b/src/mds-kkbd.c
index 1823f5c..afac365 100644
--- a/src/mds-kkbd.c
+++ b/src/mds-kkbd.c
@@ -368,8 +368,7 @@ int unmarshal_server(char* state_buf)
buf_get_next(state_buf, size_t, mapping_size);
if (mapping_size > 0)
{
- fail_if (xmalloc(mapping, mapping_size, int));
- memcpy(mapping, state_buf, mapping_size * sizeof(int));
+ fail_if (xmemdup(mapping, state_buf, mapping_size, int));
state_buf += mapping_size * sizeof(int) / sizeof(char);
}
fail_if (mds_message_unmarshal(&received, state_buf));
diff --git a/src/mds-registry/registry.c b/src/mds-registry/registry.c
index b13c7b4..4a35975 100644
--- a/src/mds-registry/registry.c
+++ b/src/mds-registry/registry.c
@@ -383,7 +383,8 @@ static int list_registry(const char* recv_client_id, const char* recv_message_id
/* Send message. */
fail_if (full_send(send_buffer + ptr, strlen(send_buffer + ptr)));
- return full_send(send_buffer, ptr);
+ fail_if (full_send(send_buffer, ptr));
+ return 0;
fail:
return -1;
}
diff --git a/src/mds-registry/slave.c b/src/mds-registry/slave.c
index 5e496f5..a8a23b6 100644
--- a/src/mds-registry/slave.c
+++ b/src/mds-registry/slave.c
@@ -431,15 +431,13 @@ size_t slave_unmarshal(slave_t* restrict this, char* restrict data)
buf_get_next(data, time_t, this->dethklok.tv_sec);
buf_get_next(data, long, this->dethklok.tv_nsec);
- n = (strlen((char*)data) + 1) * sizeof(char);
- fail_if (xbmalloc(this->client_id, n));
- memcpy(this->client_id, data, n);
- data += n, rc += n;
+ n = strlen((char*)data) + 1;
+ fail_if (xmemdup(this->client_id, data, n, char));
+ data += n, rc += n * sizeof(char);
- n = (strlen((char*)data) + 1) * sizeof(char);
- fail_if (xbmalloc(this->message_id, n));
- memcpy(this->message_id, data, n);
- data += n, rc += n;
+ n = strlen((char*)data) + 1;
+ fail_if (xmemdup(this->message_id, data, n, char));
+ data += n, rc += n * sizeof(char);
fail_if (xmalloc(this->wait_set, 1, hash_table_t));
fail_if (hash_table_create(this->wait_set));
@@ -448,10 +446,9 @@ size_t slave_unmarshal(slave_t* restrict this, char* restrict data)
while (m--)
{
- n = (strlen((char*)data) + 1) * sizeof(char);
- fail_if (xbmalloc(protocol, n));
- memcpy(protocol, data, n);
- data += n, rc += n;
+ n = strlen((char*)data) + 1;
+ fail_if (xmemdup(protocol, data, n, char));
+ data += n, rc += n * sizeof(char);
key = (size_t)(void*)protocol;
if (hash_table_put(this->wait_set, key, 1) == 0)
diff --git a/src/mds-server/client.c b/src/mds-server/client.c
index f72a2ce..a59873e 100644
--- a/src/mds-server/client.c
+++ b/src/mds-server/client.c
@@ -251,10 +251,8 @@ size_t client_unmarshal(client_t* restrict this, char* restrict data)
buf_get_next(data, size_t, this->send_pending_size);
if (this->send_pending_size > 0)
{
- fail_if (xmalloc(this->send_pending, this->send_pending_size, char));
- memcpy(this->send_pending, data, this->send_pending_size * sizeof(char));
- data += this->send_pending_size;
- rc += this->send_pending_size * sizeof(char);
+ fail_if (xmemdup(this->send_pending, data, this->send_pending_size, char));
+ data += this->send_pending_size, rc += this->send_pending_size * sizeof(char);
}
buf_get_next(data, size_t, n);
if (n > 0)
diff --git a/src/mds-server/interception-condition.c b/src/mds-server/interception-condition.c
index 92190af..64d9a10 100644
--- a/src/mds-server/interception-condition.c
+++ b/src/mds-server/interception-condition.c
@@ -70,10 +70,9 @@ size_t interception_condition_unmarshal(interception_condition_t* restrict this,
buf_get_next(data, size_t, this->header_hash);
buf_get_next(data, int64_t, this->priority);
buf_get_next(data, int, this->modifying);
- n = (strlen(data) + 1) * sizeof(char);
- fail_if (xbmalloc(this->condition, n));
- memcpy(this->condition, data, n);
- return sizeof(size_t) + sizeof(int64_t) + 2 * sizeof(int) + n;
+ n = strlen(data) + 1;
+ fail_if (xmemdup(this->condition, data, n, char));
+ return sizeof(size_t) + sizeof(int64_t) + 2 * sizeof(int) + n * sizeof(char);
fail:
return 0;
}
diff --git a/src/mds-server/multicast.c b/src/mds-server/multicast.c
index c3d09bc..4d56c31 100644
--- a/src/mds-server/multicast.c
+++ b/src/mds-server/multicast.c
@@ -133,8 +133,7 @@ size_t multicast_unmarshal(multicast_t* restrict this, char* restrict data)
}
if (this->message_length > 0)
{
- fail_if (xmalloc(this->message, this->message_length, char));
- memcpy(this->message, data, this->message_length * sizeof(char));
+ fail_if (xmemdup(this->message, data, this->message_length, char));
rc += this->message_length * sizeof(char);
}
return rc;
diff --git a/src/mds-server/receiving.c b/src/mds-server/receiving.c
index bc22fe2..e60fb42 100644
--- a/src/mds-server/receiving.c
+++ b/src/mds-server/receiving.c
@@ -78,8 +78,7 @@ static int modifying_notify(client_t* client, mds_message_t message, uint64_t mo
recipient = (client_t*)(void*)address;
fail_if (xmalloc(multicast = recipient->modify_message, 1, mds_message_t));
mds_message_zero_initialise(multicast);
- fail_if (xmalloc(multicast->payload, message.payload_size, char));
- memcpy(multicast->payload, message.payload, message.payload_size * sizeof(char));
+ fail_if (xmemdup(multicast->payload, message.payload, message.payload_size, char));
fail_if (xmalloc(multicast->headers, message.header_count, char*));
for (i = 0; i < message.header_count; i++, multicast->header_count++)
fail_if (xstrdup(multicast->headers[i], message.headers[i]));