From 89b0559e1980a1065094a9c2f88cc4ce9ecdb457 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 6 May 2014 00:53:10 +0200 Subject: add buf_set/get_next macros MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libmdsserver/fd-table.c | 14 +++++--------- src/libmdsserver/hash-table.c | 36 +++++++++++++----------------------- src/libmdsserver/macros.h | 28 ++++++++++++++++++++++++++++ src/libmdsserver/mds-message.c | 27 +++++++++++---------------- 4 files changed, 57 insertions(+), 48 deletions(-) (limited to 'src/libmdsserver') diff --git a/src/libmdsserver/fd-table.c b/src/libmdsserver/fd-table.c index e596abe..8e78b76 100644 --- a/src/libmdsserver/fd-table.c +++ b/src/libmdsserver/fd-table.c @@ -269,12 +269,9 @@ void fd_table_marshal(const fd_table_t* restrict this, char* restrict data) { size_t bitcap = (this->capacity + 63) / 64; - buf_set(data, int, 0, FD_TABLE_T_VERSION); - buf_next(data, int, 1); - - buf_set(data, size_t, 0, this->capacity); - buf_set(data, size_t, 1, this->size); - buf_next(data, size_t, 2); + buf_set_next(data, int, FD_TABLE_T_VERSION); + buf_set_next(data, size_t, this->capacity); + buf_set_next(data, size_t, this->size); memcpy(data, this->values, this->capacity * sizeof(size_t)); buf_next(data, size_t, this->capacity); @@ -299,9 +296,8 @@ int fd_table_unmarshal(fd_table_t* restrict this, char* restrict data, remap_fun /* buf_get(data, int, 0, FD_TABLE_T_VERSION) */ buf_next(data, int, 1); - buf_get(data, size_t, 0, this->capacity); - buf_get(data, size_t, 1, this->size); - buf_next(data, size_t, 2); + buf_get_next(data, size_t, this->capacity); + buf_get_next(data, size_t, this->size); this->values = NULL; this->used = NULL; diff --git a/src/libmdsserver/hash-table.c b/src/libmdsserver/hash-table.c index 332b7ce..4498616 100644 --- a/src/libmdsserver/hash-table.c +++ b/src/libmdsserver/hash-table.c @@ -404,16 +404,11 @@ void hash_table_marshal(const hash_table_t* restrict this, char* restrict data) { size_t i, n = this->capacity; - buf_set(data, int, 0, HASH_TABLE_T_VERSION); - buf_next(data, int, 1); - - 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); + buf_set_next(data, int, HASH_TABLE_T_VERSION); + buf_set_next(data, size_t, this->capacity); + buf_set_next(data, float, this->load_factor); + buf_set_next(data, size_t, this->threshold); + buf_set_next(data, size_t, this->size); for (i = 0; i < n; i++) { @@ -453,13 +448,10 @@ int hash_table_unmarshal(hash_table_t* restrict this, char* restrict data, remap this->key_comparator = NULL; this->hasher = NULL; - 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); + buf_get_next(data, size_t, this->capacity = n); + buf_get_next(data, float, this->load_factor); + buf_get_next(data, size_t, this->threshold); + buf_get_next(data, size_t, this->size); this->buckets = calloc(this->capacity, sizeof(hash_entry_t*)); if (this->buckets == NULL) @@ -469,8 +461,7 @@ int hash_table_unmarshal(hash_table_t* restrict this, char* restrict data, remap { size_t m; hash_entry_t* restrict bucket; - buf_get(data, size_t, 0, m); - buf_next(data, size_t, 1); + buf_get_next(data, size_t, m); this->buckets[i] = bucket = malloc(sizeof(hash_entry_t)); if (bucket == NULL) @@ -486,12 +477,11 @@ int hash_table_unmarshal(hash_table_t* restrict this, char* restrict data, remap if (bucket->next == NULL) return -1; } - buf_get(data, size_t, 0, bucket->key); - buf_get(data, size_t, 1, bucket->value); + buf_get_next(data, size_t, bucket->key); + buf_get_next(data, size_t, bucket->value); if (remapper != NULL) bucket->value = remapper(bucket->value); - buf_get(data, size_t, 2, bucket->hash); - buf_next(data, size_t, 3); + buf_get_next(data, size_t, bucket->hash); } } diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h index 2690917..f1c6076 100644 --- a/src/libmdsserver/macros.h +++ b/src/libmdsserver/macros.h @@ -152,5 +152,33 @@ buffer -= (count) * sizeof(type) / sizeof(char) +/** + * This macro combines `buf_set` with `buf_next`, it sets + * element zero and increase the pointer by one element + * + * @param buffer:char* The buffer + * @param type The data type of the elements for the data type to cast the buffer to + * @param variable:type The new value of the element + * @return :variable The new value of the element + */ +#define buf_set_next(buffer, type, variable) \ + buf_set(buffer, type, 0, variable); \ + buf_next(buffer, type, 1) + + +/** + * This macro combines `buf_set` with `buf_next`, it sets + * element zero and increase the pointer by one element + * + * @param buffer:char* The buffer + * @param type The data type of the elements for the data type to cast the buffer to + * @param variable:type Slot to set with the value of the element + * @return :variable The value of the element + */ +#define buf_get_next(buffer, type, variable) \ + buf_get(buffer, type, 0, variable); \ + buf_next(buffer, type, 1) + + #endif diff --git a/src/libmdsserver/mds-message.c b/src/libmdsserver/mds-message.c index 7a7cda2..5f17ec7 100644 --- a/src/libmdsserver/mds-message.c +++ b/src/libmdsserver/mds-message.c @@ -327,22 +327,19 @@ void mds_message_marshal(mds_message_t* this, char* data, int include_buffer) { size_t i, n; - buf_set(data, int, 0, MDS_MESSAGE_T_VERSION); - buf_next(data, int, 1); + buf_set_next(data, int, MDS_MESSAGE_T_VERSION); - buf_set(data, size_t, 0, this->header_count); - buf_set(data, size_t, 1, this->payload_size); + buf_set_next(data, size_t, this->header_count); + buf_set_next(data, size_t, this->payload_size); if (include_buffer) { - buf_set(data, size_t, 2, this->payload_ptr); - buf_set(data, size_t, 3, this->buffer_ptr); + buf_set_next(data, size_t, this->payload_ptr); + buf_set_next(data, size_t, this->buffer_ptr); } - buf_next(data, size_t, include_buffer ? 4 : 2); if (include_buffer) { - buf_set(data, int, 0, this->stage); - buf_next(data, int, 1); + buf_set_next(data, int, this->stage); } for (i = 0; i < this->header_count; i++) @@ -378,11 +375,10 @@ int mds_message_unmarshal(mds_message_t* this, char* data) buf_next(data, int, 1); this->header_count = 0; - 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); + buf_get_next(data, size_t, header_count); + buf_get_next(data, size_t, this->payload_size); + buf_get_next(data, size_t, this->payload_ptr); + buf_get_next(data, size_t, this->buffer_size = this->buffer_ptr); /* Make sure that the pointers are NULL so that they are not freed without being allocated when the message is @@ -391,8 +387,7 @@ int mds_message_unmarshal(mds_message_t* this, char* data) this->payload = NULL; this->buffer = NULL; - buf_get(data, int, 0, this->stage); - buf_next(data, int, 1); + buf_get_next(data, int, this->stage); /* To 2-power-multiple of 128 bytes. */ this->buffer_size >>= 7; -- cgit v1.2.3-70-g09d2