aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmdsserver
diff options
context:
space:
mode:
Diffstat (limited to 'src/libmdsserver')
-rw-r--r--src/libmdsserver/fd-table.c30
-rw-r--r--src/libmdsserver/hash-table.c65
-rw-r--r--src/libmdsserver/linked-list.c46
-rw-r--r--src/libmdsserver/macros.h97
-rw-r--r--src/libmdsserver/mds-message.c49
5 files changed, 196 insertions, 91 deletions
diff --git a/src/libmdsserver/fd-table.c b/src/libmdsserver/fd-table.c
index 6514289..e596abe 100644
--- a/src/libmdsserver/fd-table.c
+++ b/src/libmdsserver/fd-table.c
@@ -17,6 +17,8 @@
*/
#include "fd-table.h"
+#include "macros.h"
+
#include <stdlib.h>
#include <string.h>
#include <errno.h>
@@ -267,15 +269,15 @@ void fd_table_marshal(const fd_table_t* restrict this, char* restrict data)
{
size_t bitcap = (this->capacity + 63) / 64;
- ((int*)data)[0] = FD_TABLE_T_VERSION;
- data += sizeof(int) / sizeof(char);
+ buf_set(data, int, 0, FD_TABLE_T_VERSION);
+ buf_next(data, int, 1);
- ((size_t*)data)[0] = this->capacity;
- ((size_t*)data)[1] = this->size;
- data += 2 * sizeof(size_t) / sizeof(char);
+ buf_set(data, size_t, 0, this->capacity);
+ buf_set(data, size_t, 1, this->size);
+ buf_next(data, size_t, 2);
memcpy(data, this->values, this->capacity * sizeof(size_t));
- data += this->capacity * sizeof(size_t) / sizeof(char);
+ buf_next(data, size_t, this->capacity);
memcpy(data, this->used, bitcap * sizeof(uint64_t));
}
@@ -294,15 +296,15 @@ int fd_table_unmarshal(fd_table_t* restrict this, char* restrict data, remap_fun
{
size_t bitcap;
- /* ((int*)data)[0] == FD_TABLE_T_VERSION */
- data += sizeof(int) / sizeof(char);
+ /* buf_get(data, int, 0, FD_TABLE_T_VERSION) */
+ buf_next(data, int, 1);
- this->capacity = ((size_t*)data)[0];
- this->size = ((size_t*)data)[1];
- data += 2 * sizeof(size_t) / sizeof(char);
+ buf_get(data, size_t, 0, this->capacity);
+ buf_get(data, size_t, 1, this->size);
+ buf_next(data, size_t, 2);
- this->values = NULL;
- this->used = NULL;
+ this->values = NULL;
+ this->used = NULL;
this->value_comparator = NULL;
this->values = malloc(this->capacity * sizeof(size_t));
@@ -315,7 +317,7 @@ int fd_table_unmarshal(fd_table_t* restrict this, char* restrict data, remap_fun
return -1;
memcpy(this->values, data, this->capacity * sizeof(size_t));
- data += this->capacity * sizeof(size_t) / sizeof(char);
+ buf_next(data, size_t, this->capacity);
memcpy(this->used, data, bitcap * sizeof(uint64_t));
diff --git a/src/libmdsserver/hash-table.c b/src/libmdsserver/hash-table.c
index 2dbf8ad..332b7ce 100644
--- a/src/libmdsserver/hash-table.c
+++ b/src/libmdsserver/hash-table.c
@@ -17,6 +17,8 @@
*/
#include "hash-table.h"
+#include "macros.h"
+
#include <stdlib.h>
#include <errno.h>
@@ -402,16 +404,16 @@ void hash_table_marshal(const hash_table_t* restrict this, char* restrict data)
{
size_t i, n = this->capacity;
- ((int*)data)[0] = HASH_TABLE_T_VERSION;
- data += sizeof(int) / sizeof(char);
+ buf_set(data, int, 0, HASH_TABLE_T_VERSION);
+ buf_next(data, int, 1);
- ((size_t*)data)[0] = this->capacity;
- data += 1 * sizeof(size_t) / sizeof(char);
- ((float*)data)[0] = this->load_factor;
- data += 1 * sizeof(float) / sizeof(char);
- ((size_t*)data)[0] = this->threshold;
- ((size_t*)data)[1] = this->size;
- data += 2 * sizeof(size_t) / sizeof(char);
+ buf_set(data, size_t, 0, this->capacity);
+ buf_next(data, size_t, 1);
+ buf_set(data, float, 0, this->load_factor);
+ buf_next(data, float, 1);
+ buf_set(data, size_t, 0, this->threshold);
+ buf_set(data, size_t, 1, this->size);
+ buf_next(data, size_t, 2);
for (i = 0; i < n; i++)
{
@@ -419,14 +421,14 @@ void hash_table_marshal(const hash_table_t* restrict this, char* restrict data)
size_t m = 0;
while (bucket != NULL)
{
- ((size_t*)data)[1 + m * 3 + 0] = bucket->key;
- ((size_t*)data)[1 + m * 3 + 1] = bucket->value;
- ((size_t*)data)[1 + m * 3 + 2] = bucket->hash;
+ buf_set(data, size_t, 1 + m * 3 + 0, bucket->key);
+ buf_set(data, size_t, 1 + m * 3 + 1, bucket->value);
+ buf_set(data, size_t, 1 + m * 3 + 2, bucket->hash);
bucket = bucket->next;
m++;
}
- ((size_t*)data)[0] = m;
- data += (1 + m * 3) * sizeof(size_t) / sizeof(char);
+ buf_set(data, size_t, 0, m);
+ buf_next(data, size_t, 1 + m * 3);
}
}
@@ -444,20 +446,20 @@ int hash_table_unmarshal(hash_table_t* restrict this, char* restrict data, remap
{
size_t i, n;
- /* ((int*)data)[0] == HASH_TABLE_T_VERSION */
- data += sizeof(int) / sizeof(char);
+ /* buf_get(data, int, 0, HASH_TABLE_T_VERSION); */
+ buf_next(data, int, 1);
this->value_comparator = NULL;
- this->key_comparator = NULL;
- this->hasher = NULL;
+ this->key_comparator = NULL;
+ this->hasher = NULL;
- this->capacity = n = ((size_t*)data)[0];
- data += 1 * sizeof(size_t) / sizeof(char);
- this->load_factor = ((float*)data)[0];
- data += 1 * sizeof(float) / sizeof(char);
- this->threshold = ((size_t*)data)[0];
- this->size = ((size_t*)data)[1];
- data += 2 * sizeof(size_t) / sizeof(char);
+ buf_get(data, size_t, 0, this->capacity = n);
+ buf_next(data, size_t, 1);
+ buf_get(data, float, 0, this->load_factor);
+ buf_next(data, float, 1);
+ buf_get(data, size_t, 0, this->threshold);
+ buf_get(data, size_t, 1, this->size);
+ buf_next(data, size_t, 2);
this->buckets = calloc(this->capacity, sizeof(hash_entry_t*));
if (this->buckets == NULL)
@@ -465,9 +467,10 @@ int hash_table_unmarshal(hash_table_t* restrict this, char* restrict data, remap
for (i = 0; i < n; i++)
{
- size_t m = ((size_t*)data)[0];
+ size_t m;
hash_entry_t* restrict bucket;
- data += 1 * sizeof(size_t) / sizeof(char);
+ buf_get(data, size_t, 0, m);
+ buf_next(data, size_t, 1);
this->buckets[i] = bucket = malloc(sizeof(hash_entry_t));
if (bucket == NULL)
@@ -483,12 +486,12 @@ int hash_table_unmarshal(hash_table_t* restrict this, char* restrict data, remap
if (bucket->next == NULL)
return -1;
}
- bucket->key = ((size_t*)data)[0];
- bucket->value = ((size_t*)data)[1];
+ buf_get(data, size_t, 0, bucket->key);
+ buf_get(data, size_t, 1, bucket->value);
if (remapper != NULL)
bucket->value = remapper(bucket->value);
- bucket->hash = ((size_t*)data)[2];
- data += 3 * sizeof(size_t) / sizeof(char);
+ buf_get(data, size_t, 2, bucket->hash);
+ buf_next(data, size_t, 3);
}
}
diff --git a/src/libmdsserver/linked-list.c b/src/libmdsserver/linked-list.c
index 601bda0..1413358 100644
--- a/src/libmdsserver/linked-list.c
+++ b/src/libmdsserver/linked-list.c
@@ -17,6 +17,8 @@
*/
#include "linked-list.h"
+#include "macros.h"
+
#include <string.h>
#include <errno.h>
@@ -442,23 +444,23 @@ size_t linked_list_marshal_size(const linked_list_t* restrict this)
*/
void linked_list_marshal(const linked_list_t* restrict this, char* restrict data)
{
- ((int*)data)[0] = LINKED_LIST_T_VERSION;
- data += sizeof(int) / sizeof(char);
+ buf_set(data, int, 0, LINKED_LIST_T_VERSION);
+ buf_next(data, int, 1);
- ((size_t*)data)[0] = this->capacity;
- ((size_t*)data)[1] = this->end;
- ((size_t*)data)[2] = this->reuse_head;
- ((ssize_t*)data)[3] = this->edge;
- data += 4 * sizeof(size_t) / sizeof(char);
+ buf_set(data, size_t, 0, this->capacity);
+ buf_set(data, size_t, 1, this->end);
+ buf_set(data, size_t, 2, this->reuse_head);
+ buf_set(data, ssize_t, 3, this->edge);
+ buf_next(data, size_t, 4);
memcpy(data, this->reusable, this->reuse_head * sizeof(ssize_t));
- data += this->reuse_head * sizeof(ssize_t) / sizeof(char);
+ buf_next(data, ssize_t, this->reuse_head);
memcpy(data, this->values, this->end * sizeof(size_t));
- data += this->end * sizeof(size_t) / sizeof(char);
+ buf_next(data, size_t, this->end);
memcpy(data, this->next, this->end * sizeof(ssize_t));
- data += this->end * sizeof(ssize_t) / sizeof(char);
+ buf_next(data, ssize_t, this->end);
memcpy(data, this->previous, this->end * sizeof(ssize_t));
}
@@ -476,19 +478,19 @@ int linked_list_unmarshal(linked_list_t* restrict this, char* restrict data)
{
size_t n;
- /* ((int*)data)[0] == LINKED_LIST_T_VERSION */
- data += sizeof(int) / sizeof(char);
+ /* buf_get(data, int, 0, LINKED_LIST_T_VERSION); */
+ buf_next(data, int, 1);
this->reusable = NULL;
- this->values = NULL;
- this->next = NULL;
+ this->values = NULL;
+ this->next = NULL;
this->previous = NULL;
- this->capacity = ((size_t*)data)[0];
- this->end = ((size_t*)data)[1];
- this->reuse_head = ((size_t*)data)[2];
- this->edge = ((ssize_t*)data)[3];
- data += 4 * sizeof(size_t) / sizeof(char);
+ buf_get(data, size_t, 0, this->capacity);
+ buf_get(data, size_t, 1, this->end);
+ buf_get(data, size_t, 2, this->reuse_head);
+ buf_get(data, ssize_t, 3, this->edge);
+ buf_next(data, size_t, 4);
n = this->capacity * sizeof(size_t);
@@ -498,13 +500,13 @@ int linked_list_unmarshal(linked_list_t* restrict this, char* restrict data)
if ((this->previous = malloc(n)) == NULL) return -1;
memcpy(this->reusable, data, this->reuse_head * sizeof(ssize_t));
- data += this->reuse_head * sizeof(ssize_t) / sizeof(char);
+ buf_next(data, ssize_t, this->reuse_head);
memcpy(this->values, data, this->end * sizeof(size_t));
- data += this->end * sizeof(size_t) / sizeof(char);
+ buf_next(data, size_t, this->end);
memcpy(this->next, data, this->end * sizeof(ssize_t));
- data += this->end * sizeof(ssize_t) / sizeof(char);
+ buf_next(data, ssize_t, this->end);
memcpy(this->previous, data, this->end * sizeof(ssize_t));
diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h
index 392e0ff..2690917 100644
--- a/src/libmdsserver/macros.h
+++ b/src/libmdsserver/macros.h
@@ -19,8 +19,11 @@
#define MDS_LIBMDSSERVER_MACROS_H
+/*
#include <stdio.h>
#include <unistd.h>
+#include <pthread.h>
+*/
/**
@@ -55,5 +58,99 @@
fprintf(stderr, "%s: " format "\n", *argv, __VA_ARGS__);
+/**
+ * Wrapper for `pthread_mutex_lock` and `pthread_mutex_unlock`
+ *
+ * @param mutex:pthread_mutex_t The mutex
+ * @param instructions The instructions to run while the mutex is locked
+ */
+#define with_mutex(mutex, instructions) \
+ pthread_mutex_lock(&(mutex)); \
+ instructions \
+ pthread_mutex_unlock(&(mutex))
+
+
+/**
+ * Return the maximum value of two values
+ *
+ * @param a One of the values
+ * @param b The other one of the values
+ * @return The maximum value
+ */
+#define max(a, b) \
+ (a < b ? b : a)
+
+
+/**
+ * Return the minimum value of two values
+ *
+ * @param a One of the values
+ * @param b The other one of the values
+ * @return The minimum value
+ */
+#define min(a, b) \
+ (a < b ? a : b)
+
+
+/**
+ * Cast a buffer to another type and get the slot for an element
+ *
+ * @param buffer:char* The buffer
+ * @param type The data type of the elements for the data type to cast the buffer to
+ * @param index:size_t The index of the element to address
+ * @return [type] A slot that can be set or get
+ */
+#define buf_cast(buffer, type, index) \
+ ((type*)(buffer))[index]
+
+
+/**
+ * Set the value of an element a buffer that is being cast
+ *
+ * @param buffer:char* The buffer
+ * @param type The data type of the elements for the data type to cast the buffer to
+ * @param index:size_t The index of the element to address
+ * @param variable:type The new value of the element
+ * @return :variable The new value of the element
+ */
+#define buf_set(buffer, type, index, variable) \
+ ((type*)(buffer))[index] = (variable)
+
+
+/**
+ * Get the value of an element a buffer that is being cast
+ *
+ * @param buffer:char* The buffer
+ * @param type The data type of the elements for the data type to cast the buffer to
+ * @param index:size_t The index of the element to address
+ * @param variable:type Slot to set with the value of the element
+ * @return :variable The value of the element
+ */
+#define buf_get(buffer, type, index, variable) \
+ variable = ((type*)(buffer))[index]
+
+
+/**
+ * Increase the pointer of a buffer
+ *
+ * @param buffer:char* The buffer
+ * @param type A data type
+ * @param count The number elements of the data type `type` to increase the pointer with
+ */
+#define buf_next(buffer, type, count) \
+ buffer += (count) * sizeof(type) / sizeof(char)
+
+
+/**
+ * Decrease the pointer of a buffer
+ *
+ * @param buffer:char* The buffer
+ * @param type A data type
+ * @param count The number elements of the data type `type` to decrease the pointer with
+ */
+#define buf_prev(buffer, type, count) \
+ buffer -= (count) * sizeof(type) / sizeof(char)
+
+
#endif
diff --git a/src/libmdsserver/mds-message.c b/src/libmdsserver/mds-message.c
index 99554cb..7a7cda2 100644
--- a/src/libmdsserver/mds-message.c
+++ b/src/libmdsserver/mds-message.c
@@ -17,6 +17,8 @@
*/
#include "mds-message.h"
+#include "macros.h"
+
#include <stdlib.h>
#include <string.h>
#include <errno.h>
@@ -325,36 +327,36 @@ void mds_message_marshal(mds_message_t* this, char* data, int include_buffer)
{
size_t i, n;
- ((int*)data)[0] = MDS_MESSAGE_T_VERSION;
- data += sizeof(int) / sizeof(char);
+ buf_set(data, int, 0, MDS_MESSAGE_T_VERSION);
+ buf_next(data, int, 1);
- ((size_t*)data)[0] = this->header_count;
- ((size_t*)data)[1] = this->payload_size;
+ buf_set(data, size_t, 0, this->header_count);
+ buf_set(data, size_t, 1, this->payload_size);
if (include_buffer)
{
- ((size_t*)data)[2] = this->payload_ptr;
- ((size_t*)data)[3] = this->buffer_ptr;
+ buf_set(data, size_t, 2, this->payload_ptr);
+ buf_set(data, size_t, 3, this->buffer_ptr);
}
- data += (include_buffer ? 4 : 2) * sizeof(size_t) / sizeof(char);
+ buf_next(data, size_t, include_buffer ? 4 : 2);
if (include_buffer)
{
- ((int*)data)[0] = this->stage;
- data += sizeof(int) / sizeof(char);
+ buf_set(data, int, 0, this->stage);
+ buf_next(data, int, 1);
}
for (i = 0; i < this->header_count; i++)
{
n = strlen(this->headers[i]) + 1;
memcpy(data, this->headers[i], n * sizeof(char));
- data += n;
+ buf_next(data, char, n);
}
memcpy(data, this->payload, this->payload_size * sizeof(char));
if (include_buffer)
{
- data += this->payload_size;
+ buf_next(data, char, this->payload_size);
memcpy(data, this->buffer, this->buffer_ptr * sizeof(char));
}
}
@@ -372,26 +374,25 @@ int mds_message_unmarshal(mds_message_t* this, char* data)
{
size_t i, n, header_count;
- /* ((int*)data)[0] == MDS_MESSAGE_T_VERSION */
- data += sizeof(int) / sizeof(char);
+ /* buf_get(data, int, 0, MDS_MESSAGE_T_VERSION); */
+ buf_next(data, int, 1);
- header_count = ((size_t*)data)[0];
this->header_count = 0;
- this->payload_size = ((size_t*)data)[1];
- this->payload_ptr = ((size_t*)data)[2];
- this->buffer_ptr = ((size_t*)data)[3];
- this->buffer_size = this->buffer_ptr;
- data += 4 * sizeof(size_t) / sizeof(char);
+ buf_get(data, size_t, 0, header_count);
+ buf_get(data, size_t, 1, this->payload_size);
+ buf_get(data, size_t, 2, this->payload_ptr);
+ buf_get(data, size_t, 3, this->buffer_size = this->buffer_ptr);
+ buf_next(data, size_t, 4);
/* Make sure that the pointers are NULL so that they are
not freed without being allocated when the message is
destroyed if this function fails. */
this->headers = NULL;
this->payload = NULL;
- this->buffer = NULL;
+ this->buffer = NULL;
- this->stage = ((int*)data)[0];
- data += sizeof(int) / sizeof(char);
+ buf_get(data, int, 0, this->stage);
+ buf_next(data, int, 1);
/* To 2-power-multiple of 128 bytes. */
this->buffer_size >>= 7;
@@ -441,12 +442,12 @@ int mds_message_unmarshal(mds_message_t* this, char* data)
if (this->headers[i] == NULL)
return -1;
memcpy(this->headers[i], data, n * sizeof(char));
- data += n;
+ buf_next(data, char, n);
this->header_count++;
}
memcpy(this->payload, data, this->payload_size * sizeof(char));
- data += this->payload_size;
+ buf_next(data, char, this->payload_size);
memcpy(this->buffer, data, this->buffer_ptr * sizeof(char));