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 +++++++++++--------------- src/mds-server.c | 43 +++++++++++++++++------------------------- 5 files changed, 74 insertions(+), 74 deletions(-) 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; diff --git a/src/mds-server.c b/src/mds-server.c index 55abea6..3f9e529 100644 --- a/src/mds-server.c +++ b/src/mds-server.c @@ -780,17 +780,14 @@ int marshal_server(int fd) /* Tell the new version of the program what version of the program it is marshalling. */ - buf_set(state_buf_, int, 0, MDS_SERVER_VARS_VERSION); - buf_next(state_buf_, int, 1); + buf_set_next(state_buf_, int, MDS_SERVER_VARS_VERSION); /* Marshal the program's running–exit state. */ - buf_set(state_buf_, sig_atomic_t, 0, running); - buf_next(state_buf_, sig_atomic_t, 1); + buf_set_next(state_buf_, sig_atomic_t, running); /* Tell the program how large the marshalled client list is and how any clients are marshalled. */ - buf_set(state_buf_, size_t, 0, list_size); - buf_set(state_buf_, size_t, 1, list_elements); - buf_next(state_buf_, size_t, 2); + buf_set_next(state_buf_, size_t, list_size); + buf_set_next(state_buf_, size_t, list_elements); /* Marshal the clients. */ for (node = client_list.edge;;) @@ -811,15 +808,13 @@ int marshal_server(int fd) msg_size = mds_message_marshal_size(&(value->message), 1); /* Marshal the address, it is used the the client list and the client map, that will be marshalled. */ - buf_set(state_buf_, size_t, 0, value_address); + buf_set_next(state_buf_, size_t, value_address); /* Tell the program how large the marshalled message is. */ - buf_set(state_buf_, size_t, 1, msg_size); + buf_set_next(state_buf_, size_t, msg_size); /* Marshal the client info. */ - buf_set(state_buf_, ssize_t, 2, value->list_entry); - buf_next(state_buf_, size_t, 3); - buf_set(state_buf_, int, 0, value->socket_fd); - buf_set(state_buf_, int, 1, value->open); - buf_next(state_buf_, int, 2); + buf_set_next(state_buf_, ssize_t, value->list_entry); + buf_set_next(state_buf_, int, value->socket_fd); + buf_set_next(state_buf_, int, value->open); /* Marshal the message. */ mds_message_marshal(&(value->message), state_buf_, 1); state_buf_ += msg_size / sizeof(char); @@ -969,13 +964,11 @@ int unmarshal_server(int fd) buf_next(state_buf_, int, 1); /* Unmarshal the program's running–exit state. */ - buf_get(state_buf_, sig_atomic_t, 0, running); - buf_next(state_buf_, sig_atomic_t, 1); + buf_get_next(state_buf_, sig_atomic_t, running); /* Get the marshalled size of the client list and how any clients that are marshalled. */ - buf_get(state_buf_, size_t, 0, list_size); - buf_get(state_buf_, size_t, 1, list_elements); - buf_next(state_buf_, size_t, 2); + buf_get_next(state_buf_, size_t, list_size); + buf_get_next(state_buf_, size_t, list_elements); /* Unmarshal the clients. */ for (i = 0; i < list_elements; i++) @@ -992,15 +985,13 @@ int unmarshal_server(int fd) } /* Unmarshal the address, it is used the the client list and the client map, that are also marshalled. */ - buf_get(state_buf_, size_t, 0, value_address); + buf_get_next(state_buf_, size_t, value_address); /* Get the marshalled size of the message. */ - buf_get(state_buf_, size_t, 1, msg_size); + buf_get_next(state_buf_, size_t, msg_size); /* Unmarshal the client info. */ - buf_get(state_buf_, ssize_t, 2, value->list_entry); - buf_next(state_buf_, size_t, 3); - buf_get(state_buf_, int, 0, value->socket_fd); - buf_get(state_buf_, int, 1, value->open); - buf_next(state_buf_, int, 2); + buf_get_next(state_buf_, ssize_t, value->list_entry); + buf_get_next(state_buf_, int, value->socket_fd); + buf_get_next(state_buf_, int, value->open); /* Unmarshal the message. */ if (mds_message_unmarshal(&(value->message), state_buf_)) { -- cgit v1.2.3-70-g09d2