diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-05-05 21:51:14 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-05-05 21:51:14 +0200 |
commit | ae75ed2772e0cb51e658f4f0ebb2020b0031a125 (patch) | |
tree | 6b768c6b430baed89b64ba1cbddc972fb1f040c5 /src | |
parent | use more macros (diff) | |
download | mds-ae75ed2772e0cb51e658f4f0ebb2020b0031a125.tar.gz mds-ae75ed2772e0cb51e658f4f0ebb2020b0031a125.tar.bz2 mds-ae75ed2772e0cb51e658f4f0ebb2020b0031a125.tar.xz |
use more macros
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/libmdsserver/fd-table.c | 30 | ||||
-rw-r--r-- | src/libmdsserver/hash-table.c | 65 | ||||
-rw-r--r-- | src/libmdsserver/linked-list.c | 46 | ||||
-rw-r--r-- | src/libmdsserver/macros.h | 97 | ||||
-rw-r--r-- | src/libmdsserver/mds-message.c | 49 | ||||
-rw-r--r-- | src/mds-server.c | 127 |
6 files changed, 253 insertions, 161 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)); diff --git a/src/mds-server.c b/src/mds-server.c index 17c7972..826e0d2 100644 --- a/src/mds-server.c +++ b/src/mds-server.c @@ -57,8 +57,7 @@ static char** argv; /** - * The program run state, 1 when running, - * 0 when shutting down + * The program run state, 1 when running, 0 when shutting down */ static volatile sig_atomic_t running = 1; @@ -321,18 +320,14 @@ int main(int argc_, char** argv_) } /* Increase number of running slaves. */ - pthread_mutex_lock(&slave_mutex); - running_slaves++; - pthread_mutex_unlock(&slave_mutex); + with_mutex(slave_mutex, running_slaves++;); /* Start slave thread. */ errno = pthread_create(&_slave_thread, NULL, slave_loop, (void*)(intptr_t)client_fd); if (errno) { perror(*argv); - pthread_mutex_lock(&slave_mutex); - running_slaves--; - pthread_mutex_unlock(&slave_mutex); + with_mutex(slave_mutex, running_slaves--;); } } if (reexecing) @@ -340,10 +335,9 @@ int main(int argc_, char** argv_) /* Wait for all slaves to close. */ - pthread_mutex_lock(&slave_mutex); - while (running_slaves > 0) - pthread_cond_wait(&slave_cond, &slave_mutex); - pthread_mutex_unlock(&slave_mutex); + with_mutex(slave_mutex, + while (running_slaves > 0) + pthread_cond_wait(&slave_cond, &slave_mutex);); /* Release resources. */ @@ -370,10 +364,9 @@ int main(int argc_, char** argv_) pthread_cond_destroy(&slave_cond); /* Join with all slaves threads. */ - pthread_mutex_lock(&slave_mutex); - while (running_slaves > 0) - pthread_cond_wait(&slave_cond, &slave_mutex); - pthread_mutex_unlock(&slave_mutex); + with_mutex(slave_mutex, + while (running_slaves > 0) + pthread_cond_wait(&slave_cond, &slave_mutex);); /* Marshal the state of the server. */ xsnprintf(shm_path, SHM_PATH_PATTERN, (unsigned long int)pid); @@ -551,12 +544,11 @@ void* slave_loop(void* data) fd_table_remove(&client_map, socket_fd); /* Unlist client and decrease the slave count. */ - pthread_mutex_lock(&slave_mutex); - if (entry != LINKED_LIST_UNUSED) - linked_list_remove(&client_list, entry); - running_slaves--; - pthread_cond_signal(&slave_cond); - pthread_mutex_unlock(&slave_mutex); + with_mutex(slave_mutex, + if (entry != LINKED_LIST_UNUSED) + linked_list_remove(&client_list, entry); + running_slaves--; + pthread_cond_signal(&slave_cond);); return NULL; @@ -566,10 +558,9 @@ void* slave_loop(void* data) this is done because re-exec causes a race-condition between the acception of a slave and the execution of the the slave thread. */ - pthread_mutex_lock(&slave_mutex); - running_slaves--; - pthread_cond_signal(&slave_cond); - pthread_mutex_unlock(&slave_mutex); + with_mutex(slave_mutex, + running_slaves--; + pthread_cond_signal(&slave_cond);); return NULL; } @@ -722,17 +713,17 @@ int marshal_server(int fd) /* Tell the new version of the program what version of the program it is marshalling. */ - ((int*)state_buf_)[0] = MDS_SERVER_VARS_VERSION; - state_buf_ += 1 * sizeof(int) / sizeof(char); + buf_set(state_buf_, int, 0, MDS_SERVER_VARS_VERSION); + buf_next(state_buf_, int, 1); /* Marshal the program's running–exit state. */ - ((sig_atomic_t*)state_buf_)[0] = running; - state_buf_ += 1 * sizeof(sig_atomic_t) / sizeof(char); + buf_set(state_buf_, sig_atomic_t, 0, running); + buf_next(state_buf_, sig_atomic_t, 1); /* Tell the program how large the marshalled client list is and how any clients are marshalled. */ - ((size_t*)state_buf_)[0] = list_size; - ((size_t*)state_buf_)[1] = list_elements; - state_buf_ += 2 * sizeof(size_t) / sizeof(char); + 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); /* Marshal the clients. */ for (node = client_list.edge;;) @@ -753,15 +744,15 @@ 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. */ - ((size_t*)state_buf_)[0] = value_address; + buf_set(state_buf_, size_t, 0, value_address); /* Tell the program how large the marshalled message is. */ - ((size_t*)state_buf_)[1] = msg_size; + buf_set(state_buf_, size_t, 1, msg_size); /* Marshal the client info. */ - ((ssize_t*)state_buf_)[2] = value->list_entry; - state_buf_ += 3 * sizeof(size_t) / sizeof(char); - ((int*)state_buf_)[0] = value->socket_fd; - ((int*)state_buf_)[1] = value->open; - state_buf_ += 2 * sizeof(int) / sizeof(char); + 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); /* Marshal the message. */ mds_message_marshal(&(value->message), state_buf_, 1); state_buf_ += msg_size / sizeof(char); @@ -775,8 +766,8 @@ int marshal_server(int fd) wrote = write(fd, state_buf, state_n); if (errno && (errno != EINTR)) goto fail; - state_n -= (size_t)(wrote < 0 ? 0 : wrote); - state_buf += (size_t)(wrote < 0 ? 0 : wrote); + state_n -= (size_t)max(wrote, 0); + state_buf += (size_t)max(wrote, 0); } free(state_buf); @@ -791,8 +782,8 @@ int marshal_server(int fd) wrote = write(fd, state_buf, list_size); if (errno && (errno != EINTR)) goto fail; - list_size -= (size_t)(wrote < 0 ? 0 : wrote); - state_buf += (size_t)(wrote < 0 ? 0 : wrote); + list_size -= (size_t)max(wrote, 0); + state_buf += (size_t)max(wrote, 0); } free(state_buf); @@ -807,8 +798,8 @@ int marshal_server(int fd) wrote = write(fd, state_buf, map_size); if (errno && (errno != EINTR)) goto fail; - map_size -= (size_t)(wrote < 0 ? 0 : wrote); - state_buf += (size_t)(wrote < 0 ? 0 : wrote); + map_size -= (size_t)max(wrote, 0); + state_buf += (size_t)max(wrote, 0); } free(state_buf); @@ -907,17 +898,17 @@ int unmarshal_server(int fd) /* Get the marshal protocal version. Not needed, there is only the one version right now. */ - /* MDS_SERVER_VARS_VERSION == ((int*)state_buf_)[0]; */ - state_buf_ += 1 * sizeof(int) / sizeof(char); + /* buf_get(state_buf_, int, 0, MDS_SERVER_VARS_VERSION); */ + buf_next(state_buf_, int, 1); /* Unmarshal the program's running–exit state. */ - running = ((sig_atomic_t*)state_buf_)[0]; - state_buf_ += 1 * sizeof(sig_atomic_t) / sizeof(char); + buf_get(state_buf_, sig_atomic_t, 0, running); + buf_next(state_buf_, sig_atomic_t, 1); /* Get the marshalled size of the client list and how any clients that are marshalled. */ - list_size = ((size_t*)state_buf_)[0]; - list_elements = ((size_t*)state_buf_)[1]; - state_buf_ += 2 * sizeof(size_t) / sizeof(char); + 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); /* Unmarshal the clients. */ for (i = 0; i < list_elements; i++) @@ -934,23 +925,23 @@ int unmarshal_server(int fd) } /* Unmarshal the address, it is used the the client list and the client map, that are also marshalled. */ - value_address = ((size_t*)state_buf_)[0]; + buf_get(state_buf_, size_t, 0, value_address); /* Get the marshalled size of the message. */ - msg_size = ((size_t*)state_buf_)[1]; + buf_get(state_buf_, size_t, 1, msg_size); /* Unmarshal the client info. */ - value->list_entry = ((ssize_t*)state_buf_)[2]; - state_buf_ += 3 * sizeof(size_t) / sizeof(char); - value->socket_fd = ((int*)state_buf_)[0]; - value->open = ((int*)state_buf_)[1]; - state_buf_ += 2 * sizeof(int) / sizeof(char); + 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); /* Unmarshal the message. */ if (mds_message_unmarshal(&(value->message), state_buf_)) { perror(*argv); mds_message_destroy(&(value->message)); free(value); - state_buf_ -= 2 * sizeof(int) / sizeof(char); - state_buf_ -= 3 * sizeof(size_t) / sizeof(char); + buf_prev(state_buf_, int, 2); + buf_prev(state_buf_, size_t, 3); goto clients_fail; } state_buf_ += msg_size / sizeof(char); @@ -969,8 +960,8 @@ int unmarshal_server(int fd) the caller because there are conditions where we cannot get here anyway. */ msg_size = ((size_t*)state_buf_)[1]; - state_buf_ += 3 * sizeof(size_t) / sizeof(char); - state_buf_ += 2 * sizeof(int) / sizeof(char); + buf_next(state_buf_, size_t, 3); + buf_next(state_buf_, int, 2); state_buf_ += msg_size / sizeof(char); } break; @@ -1012,18 +1003,14 @@ int unmarshal_server(int fd) int socket_fd = client->socket_fd; /* Increase number of running slaves. */ - pthread_mutex_lock(&slave_mutex); - running_slaves++; - pthread_mutex_unlock(&slave_mutex); + with_mutex(slave_mutex, running_slaves++;); /* Start slave thread. */ errno = pthread_create(&_slave_thread, NULL, slave_loop, (void*)(intptr_t)socket_fd); if (errno) { perror(*argv); - pthread_mutex_lock(&slave_mutex); - running_slaves--; - pthread_mutex_unlock(&slave_mutex); + with_mutex(slave_mutex, running_slaves--;); } } } |