aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmdsserver
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-12-10 02:21:00 +0100
committerMattias Andrée <maandree@operamail.com>2014-12-10 02:21:00 +0100
commit7179f51176e1f3af53694e09d7ea2ca602403cf1 (patch)
treedb24424529090c7ac819e2ec9724c855dbb2d53e /src/libmdsserver
parenttypo (diff)
downloadmds-7179f51176e1f3af53694e09d7ea2ca602403cf1.tar.gz
mds-7179f51176e1f3af53694e09d7ea2ca602403cf1.tar.bz2
mds-7179f51176e1f3af53694e09d7ea2ca602403cf1.tar.xz
no more direct allocations, always use macros, unless using alloca
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/libmdsserver')
-rw-r--r--src/libmdsserver/client-list.c13
-rw-r--r--src/libmdsserver/linked-list.c45
-rw-r--r--src/libmdsserver/macros.h51
-rw-r--r--src/libmdsserver/util.c3
4 files changed, 69 insertions, 43 deletions
diff --git a/src/libmdsserver/client-list.c b/src/libmdsserver/client-list.c
index 24403cb..33b6844 100644
--- a/src/libmdsserver/client-list.c
+++ b/src/libmdsserver/client-list.c
@@ -106,7 +106,7 @@ int client_list_clone(const client_list_t* restrict this, client_list_t* restric
out->clients = NULL;
- fail_if ((new_clients = malloc(n)) == NULL);
+ fail_if (xbmalloc(new_clients, n));
out->clients = new_clients;
@@ -221,8 +221,6 @@ void client_list_marshal(const client_list_t* restrict this, char* restrict data
*/
int client_list_unmarshal(client_list_t* restrict this, char* restrict data)
{
- size_t n;
-
/* buf_get(data, int, 0, CLIENT_LIST_T_VERSION); */
buf_next(data, int, 1);
@@ -231,13 +229,8 @@ int client_list_unmarshal(client_list_t* restrict this, char* restrict data)
buf_get_next(data, size_t, this->capacity);
buf_get_next(data, size_t, this->size);
- n = this->capacity * sizeof(uint64_t);
-
- fail_if ((this->clients = malloc(n)) == NULL);
-
- n = this->size * sizeof(uint64_t);
-
- memcpy(this->clients, data, n);
+ fail_if (xmalloc(this->clients, this->capacity, uint64_t));
+ memcpy(this->clients, data, this->size * sizeof(uint64_t));
return 0;
fail:
diff --git a/src/libmdsserver/linked-list.c b/src/libmdsserver/linked-list.c
index f00daf3..1dea87a 100644
--- a/src/libmdsserver/linked-list.c
+++ b/src/libmdsserver/linked-list.c
@@ -125,10 +125,10 @@ int linked_list_clone(const linked_list_t* restrict this, linked_list_t* restric
out->previous = NULL;
out->reusable = NULL;
- 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);
+ 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;
@@ -242,31 +242,22 @@ int linked_list_pack(linked_list_t* restrict this)
*/
static ssize_t linked_list_get_next(linked_list_t* restrict this)
{
+ size_t* tmp_values;
+ ssize_t* tmp;
+
if (this->reuse_head > 0)
return this->reusable[--(this->reuse_head)];
if (this->end == this->capacity)
{
- size_t* old_values;
- ssize_t* old;
-
if ((ssize_t)(this->end) < 0)
fail_if ((errno = ENOMEM));
this->capacity <<= 1;
-
-#define __realloc(new_var, old_var, type) \
- if ((new_var = realloc(old_var = new_var, this->capacity * sizeof(type))) == NULL) \
- { \
- new_var = old_var; \
- fail_if (1); \
- }
-
- __realloc(this->values, old_values, size_t)
- __realloc(this->next, old, ssize_t)
- __realloc(this->previous, old, ssize_t)
- __realloc(this->reusable, old, ssize_t)
-
-#undef __realloc
+
+ fail_if (yrealloc(tmp_values, this->values, this->capacity, size_t));
+ fail_if (yrealloc(tmp, this->next, this->capacity, ssize_t));
+ fail_if (yrealloc(tmp, this->previous, this->capacity, ssize_t));
+ fail_if (yrealloc(tmp, this->reusable, this->capacity, ssize_t));
}
return (ssize_t)(this->end++);
fail:
@@ -438,8 +429,6 @@ void linked_list_marshal(const linked_list_t* restrict this, char* restrict data
*/
int linked_list_unmarshal(linked_list_t* restrict this, char* restrict data)
{
- size_t n;
-
/* buf_get(data, int, 0, LINKED_LIST_T_VERSION); */
buf_next(data, int, 1);
@@ -454,12 +443,10 @@ int linked_list_unmarshal(linked_list_t* restrict this, char* restrict data)
buf_get(data, ssize_t, 3, this->edge);
buf_next(data, size_t, 4);
- n = this->capacity * sizeof(size_t);
-
- 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);
+ fail_if (xmalloc(this->reusable, this->capacity, size_t));
+ fail_if (xmalloc(this->values, this->capacity, size_t));
+ fail_if (xmalloc(this->next, this->capacity, size_t));
+ fail_if (xmalloc(this->previous, this->capacity, size_t));
memcpy(this->reusable, data, this->reuse_head * sizeof(ssize_t));
buf_next(data, ssize_t, this->reuse_head);
diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h
index b39d750..672c0a3 100644
--- a/src/libmdsserver/macros.h
+++ b/src/libmdsserver/macros.h
@@ -339,6 +339,17 @@
/**
+ * `malloc` wrapper that returns whether the allocation was not successful
+ *
+ * @param var:type* The variable to which to assign the allocation
+ * @param bytes:size_t The number of bytes to allocate
+ * @return :int Evaluates to true if an only if the allocation failed
+ */
+#define xbmalloc(var, elements) \
+ ((var = malloc(elements)) == NULL)
+
+
+/**
* `calloc` wrapper that returns whether the allocation was not successful
*
* @param var:type* The variable to which to assign the allocation
@@ -351,6 +362,17 @@
/**
+ * `calloc` wrapper that returns whether the allocation was not successful
+ *
+ * @param var:type* The variable to which to assign the allocation
+ * @param bytes:size_t The number of bytes to allocate
+ * @return :int Evaluates to true if an only if the allocation failed
+ */
+#define xbcalloc(var, bytes) \
+ ((var = calloc(bytes, sizeof(char))) == NULL)
+
+
+/**
* `realloc` wrapper that returns whether the allocation was not successful
*
* @param var:type* The variable to which to assign the reallocation
@@ -365,7 +387,7 @@
/**
* `xrealloc` that stores the old variable
*
- * @param old:type* The variable to which to store with the old variable that needs
+ * @param old:type* The variable to which to store with the old variable that needs
* to be `free`:ed on failure, and set to `NULL` on success.
* @param var:type* The variable to which to assign the reallocation
* @param elements:size_t The number of elements to allocate
@@ -373,7 +395,21 @@
* @return :int Evaluates to true if an only if the allocation failed
*/
#define xxrealloc(old, var, elements, type) \
- (old = var, (xrealloc(var, elements, type) ? 1 : (old = NULL, 0)))
+ (old = var, (((var = realloc(var, (elements) * sizeof(type))) == NULL) ? 1 : (old = NULL, 0)))
+
+
+/**
+ * `xrealloc` that restores the variable on failure
+ *
+ * @param tmp:type* The variable to which to store with the old variable temporarily
+ * @param var:type* The variable to which to assign the reallocation
+ * @param elements:size_t The number of elements to allocate
+ * @param type The data type of the elements for which to create an allocation
+ * @return :int Evaluates to true if an only if the allocation failed
+ */
+#define yrealloc(tmp, var, elements, type) \
+ ((tmp = var, (var = realloc(var, (elements) * sizeof(type))) == NULL) \
+ ? (var = tmp, tmp = NULL, 1) : (tmp = NULL, 0))
/**
@@ -390,6 +426,17 @@
/**
+ * `strdup` wrapper that returns whether the allocation was not successful
+ *
+ * @param var:char* The variable to which to assign the duplicate
+ * @param original:const char* The string to duplicate
+ * @return :int Evaluates to true if an only if the allocation failed
+ */
+#define xstrdup(var, original) \
+ (original ? ((var = strdup(original)) == NULL) : (var = NULL, 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/util.c b/src/libmdsserver/util.c
index 123463c..211de32 100644
--- a/src/libmdsserver/util.c
+++ b/src/libmdsserver/util.c
@@ -135,8 +135,7 @@ void reexec_server(int argc, char** argv, int reexeced)
if (reexeced == 0)
{
*reexec_args_++ = *argv;
- *reexec_args_ = strdup("--re-exec");
- fail_if (*reexec_args_ == NULL);
+ fail_if (xstrdup(*reexec_args_, "--re-exec"));
for (i = 1; i < argc; i++)
reexec_args_[i] = argv[i];
}