From 9ed21175ac00f57f6abf3da84e82098e68fa3e94 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 5 Nov 2017 14:50:38 +0100 Subject: Work on changing style MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libmdsserver/client-list.c | 197 +++++++++++++++++++++-------------------- 1 file changed, 100 insertions(+), 97 deletions(-) (limited to 'src/libmdsserver/client-list.c') diff --git a/src/libmdsserver/client-list.c b/src/libmdsserver/client-list.c index 19eaf5a..02d555e 100644 --- a/src/libmdsserver/client-list.c +++ b/src/libmdsserver/client-list.c @@ -27,7 +27,7 @@ * The default initial capacity */ #ifndef CLIENT_LIST_DEFAULT_INITIAL_CAPACITY -#define CLIENT_LIST_DEFAULT_INITIAL_CAPACITY 8 +# define CLIENT_LIST_DEFAULT_INITIAL_CAPACITY 8 #endif @@ -38,19 +38,19 @@ * @param value The value to be rounded up to a power of two * @return The nearest, but not smaller, power of two */ -__attribute__((const)) -static size_t to_power_of_two(size_t value) +static size_t __attribute__((const)) +to_power_of_two(size_t value) { - value -= 1; - value |= value >> 1; - value |= value >> 2; - value |= value >> 4; - value |= value >> 8; - value |= value >> 16; + value -= 1; + value |= value >> 1; + value |= value >> 2; + value |= value >> 4; + value |= value >> 8; + value |= value >> 16; #if SIZE_MAX == UINT64_MAX - value |= value >> 32; + value |= value >> 32; #endif - return value + 1; + return value + 1; } @@ -61,21 +61,22 @@ static size_t to_power_of_two(size_t value) * @param capacity The minimum initial capacity of the client list, 0 for default * @return Non-zero on error, `errno` will have been set accordingly */ -int client_list_create(client_list_t* restrict this, size_t capacity) +int +client_list_create(client_list_t *restrict this, size_t capacity) { - /* Use default capacity of zero is specified. */ - if (capacity == 0) - capacity = CLIENT_LIST_DEFAULT_INITIAL_CAPACITY; - - /* Initialise the client list. */ - this->capacity = capacity = to_power_of_two(capacity); - this->size = 0; - this->clients = NULL; - fail_if (xmalloc(this->clients, capacity, uint64_t)); - - return 0; - fail: - return -1; + /* Use default capacity of zero is specified. */ + if (!capacity) + capacity = CLIENT_LIST_DEFAULT_INITIAL_CAPACITY; + + /* Initialise the client list. */ + this->capacity = capacity = to_power_of_two(capacity); + this->size = 0; + this->clients = NULL; + fail_if (xmalloc(this->clients, capacity, uint64_t)); + + return 0; +fail: + return -1; } @@ -85,10 +86,11 @@ int client_list_create(client_list_t* restrict this, size_t capacity) * * @param this The client list */ -void client_list_destroy(client_list_t* restrict this) +void +client_list_destroy(client_list_t *restrict this) { - free(this->clients); - this->clients = NULL; + free(this->clients); + this->clients = NULL; } @@ -99,17 +101,18 @@ void client_list_destroy(client_list_t* restrict this) * @param out Memory slot in which to store the new client list * @return Non-zero on error, `errno` will have been set accordingly */ -int client_list_clone(const client_list_t* restrict this, client_list_t* restrict out) +int +client_list_clone(const client_list_t *restrict this, client_list_t *restrict out) { - fail_if (xmemdup(out->clients, this->clients, this->capacity, uint64_t)); - - out->capacity = this->capacity; - out->size = this->size; - - return 0; - - fail: - return -1; + fail_if (xmemdup(out->clients, this->clients, this->capacity, uint64_t)); + + out->capacity = this->capacity; + out->size = this->size; + + return 0; + +fail: + return -1; } @@ -120,23 +123,23 @@ int client_list_clone(const client_list_t* restrict this, client_list_t* restric * @param client The client to add * @return Non-zero on error, `errno` will be set accordingly */ -int client_list_add(client_list_t* restrict this, uint64_t client) +int +client_list_add(client_list_t *restrict this, uint64_t client) { - if (this->size == this->capacity) - { - uint64_t* old = this->clients; - if (xrealloc(old, this->capacity <<= 1, uint64_t)) - { - this->capacity >>= 1; - this->clients = old; - fail_if (1); + uint64_t* old; + if (this->size == this->capacity) { + old = this->clients; + if (xrealloc(old, this->capacity <<= 1, uint64_t)) { + this->capacity >>= 1; + this->clients = old; + fail_if (1); + } } - } - - this->clients[this->size++] = client; - return 0; - fail: - return -1; + + this->clients[this->size++] = client; + return 0; +fail: + return -1; } @@ -146,29 +149,27 @@ int client_list_add(client_list_t* restrict this, uint64_t client) * @param this The list * @param client The client to remove */ -void client_list_remove(client_list_t* restrict this, uint64_t client) +void +client_list_remove(client_list_t *restrict this, uint64_t client) { - size_t i; - for (i = 0; i < this->size; i++) - { - if (this->clients[i] == client) - { - size_t n = (--(this->size) - i) * sizeof(uint64_t); - memmove(this->clients + i, this->clients + i + 1, n); - - if (this->size << 1 <= this->capacity) - { - uint64_t* old = this->clients; - if (xrealloc(old, this->capacity >>= 1, uint64_t)) - { - this->capacity <<= 1; - this->clients = old; + size_t i, n; + uint64_t *old; + for (i = 0; i < this->size; i++) { + if (this->clients[i] == client) { + n = (--(this->size) - i) * sizeof(uint64_t); + memmove(this->clients + i, this->clients + i + 1, n); + + if (this->size << 1 <= this->capacity) { + old = this->clients; + if (xrealloc(old, this->capacity >>= 1, uint64_t)) { + this->capacity <<= 1; + this->clients = old; + } + } + + return; } - } - - return; } - } } @@ -178,9 +179,10 @@ void client_list_remove(client_list_t* restrict this, uint64_t client) * @param this The list * @return The number of bytes to allocate to the output buffer */ -size_t client_list_marshal_size(const client_list_t* restrict this) +size_t +client_list_marshal_size(const client_list_t *restrict this) { - return 2 * sizeof(size_t) + this->size * sizeof(uint64_t) + sizeof(int); + return 2 * sizeof(size_t) + this->size * sizeof(uint64_t) + sizeof(int); } @@ -190,13 +192,14 @@ size_t client_list_marshal_size(const client_list_t* restrict this) * @param this The list * @param data Output buffer for the marshalled data */ -void client_list_marshal(const client_list_t* restrict this, char* restrict data) +void +client_list_marshal(const client_list_t *restrict this, char *restrict data) { - buf_set_next(data, int, CLIENT_LIST_T_VERSION); - buf_set_next(data, size_t, this->capacity); - buf_set_next(data, size_t, this->size); - - memcpy(data, this->clients, this->size * sizeof(uint64_t)); + buf_set_next(data, int, CLIENT_LIST_T_VERSION); + buf_set_next(data, size_t, this->capacity); + buf_set_next(data, size_t, this->size); + + memcpy(data, this->clients, this->size * sizeof(uint64_t)); } @@ -208,21 +211,21 @@ void client_list_marshal(const client_list_t* restrict this, char* restrict data * @return Non-zero on error, `errno` will be set accordingly. * Destroy the list on error. */ -int client_list_unmarshal(client_list_t* restrict this, char* restrict data) +int +client_list_unmarshal(client_list_t *restrict this, char *restrict data) { - /* buf_get(data, int, 0, CLIENT_LIST_T_VERSION); */ - buf_next(data, int, 1); - - this->clients = NULL; - - buf_get_next(data, size_t, this->capacity); - buf_get_next(data, size_t, this->size); - - fail_if (xmalloc(this->clients, this->capacity, uint64_t)); - memcpy(this->clients, data, this->size * sizeof(uint64_t)); - - return 0; - fail: - return -1; -} + /* buf_get(data, int, 0, CLIENT_LIST_T_VERSION); */ + buf_next(data, int, 1); + + this->clients = NULL; + + buf_get_next(data, size_t, this->capacity); + buf_get_next(data, size_t, this->size); + fail_if (xmalloc(this->clients, this->capacity, uint64_t)); + memcpy(this->clients, data, this->size * sizeof(uint64_t)); + + return 0; +fail: + return -1; +} -- cgit v1.2.3-70-g09d2