aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/libmdsserver/macros.h14
-rw-r--r--src/mds-registry.c56
2 files changed, 35 insertions, 35 deletions
diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h
index 59537ff..cbef69c 100644
--- a/src/libmdsserver/macros.h
+++ b/src/libmdsserver/macros.h
@@ -323,6 +323,20 @@
/**
+ * Double to the size of an allocation on the heap
+ *
+ * @param old Variable in which to store the old value temporarily
+ * @param var The variable to which to assign the reallocation
+ * @param elements 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 growalloc(old, var, elements, type) \
+ (old = var, xrealloc(var, (elements) <<= 1, type) ? (var = old, (elements) >>= 1, perror(*argv), 1) : 0)
+
+
+
+/**
* Go to the label `pfail` if a condition is met
*
* @param CONDITION The condition
diff --git a/src/mds-registry.c b/src/mds-registry.c
index 83702a1..e65d262 100644
--- a/src/mds-registry.c
+++ b/src/mds-registry.c
@@ -93,6 +93,11 @@ static pthread_mutex_t reg_mutex;
*/
static pthread_cond_t reg_cond;
+/**
+ * Used to temporarily store the old value when reallocating heap-allocations
+ */
+static char* old;
+
/**
@@ -436,11 +441,8 @@ int registry_action(size_t length, int action, const char* recv_client_id, const
if (received.payload_size == length)
{
- if (xrealloc(received.payload, received.payload_size <<= 1, char))
+ if (growalloc(old, received.payload, received.payload_size, char))
{
- received.payload = payload;
- received.payload_size >>= 1;
- perror(*argv);
if (wait_set != NULL)
{
hash_table_destroy(wait_set, NULL, NULL);
@@ -578,20 +580,11 @@ int list_registry(const char* recv_client_id, const char* recv_message_id)
if (send_buffer_size == 0)
{
- if (xmalloc(send_buffer, 256, char))
- {
- perror(*argv);
- return -1;
- }
+ fail_if (xmalloc(send_buffer, 256, char));
send_buffer_size = 256;
}
- errno = pthread_mutex_lock(&reg_mutex);
- if (errno)
- {
- perror(*argv);
- return -1;
- }
+ fail_if ((errno = pthread_mutex_lock(&reg_mutex)));
for (i = 0; i < reg_table.capacity; i++)
{
@@ -603,17 +596,8 @@ int list_registry(const char* recv_client_id, const char* recv_message_id)
size_t len = strlen(command);
while (ptr + len + 1 >= send_buffer_size)
- {
- char* old = send_buffer;
- if (xrealloc(send_buffer, send_buffer_size <<= 1, char))
- {
- send_buffer = old;
- send_buffer_size >>= 1;
- perror(*argv);
- pthread_mutex_unlock(&reg_mutex);
- return -1;
- }
- }
+ if (growalloc(old, send_buffer, send_buffer_size, char))
+ goto fail_in_mutex;
memcpy(send_buffer + ptr, command, len * sizeof(char));
ptr += len;
@@ -626,15 +610,8 @@ int list_registry(const char* recv_client_id, const char* recv_message_id)
while (ptr + i >= send_buffer_size)
{
- char* old = send_buffer;
- if (xrealloc(send_buffer, send_buffer_size <<= 1, char))
- {
- send_buffer = old;
- send_buffer_size >>= 1;
- perror(*argv);
- pthread_mutex_unlock(&reg_mutex);
- return -1;
- }
+ if (growalloc(old, send_buffer, send_buffer_size, char))
+ goto fail_in_mutex;
}
sprintf(send_buffer + ptr, "To: %s\nIn response to: %s\nMessage ID: %" PRIi32 "\nLength: %" PRIu64 "\n\n",
@@ -647,6 +624,15 @@ int list_registry(const char* recv_client_id, const char* recv_message_id)
if (full_send(send_buffer + ptr, strlen(send_buffer + ptr)))
return 1;
return full_send(send_buffer, ptr);
+
+
+ fail_in_mutex:
+ pthread_mutex_unlock(&reg_mutex);
+ return -1;
+
+ pfail:
+ perror(*argv);
+ return -1;
}