From c49d626ed36f0af1f2ffe970c24b5177d9e9b197 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 18 May 2014 05:22:13 +0200 Subject: style + reduce code complexity 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 | 64 +++++++++++++++++++++++---------------------- src/libmdsserver/macros.h | 21 +++++++++++++-- 2 files changed, 52 insertions(+), 33 deletions(-) (limited to 'src/libmdsserver') diff --git a/src/libmdsserver/fd-table.c b/src/libmdsserver/fd-table.c index 9fe62ca..eb681f9 100644 --- a/src/libmdsserver/fd-table.c +++ b/src/libmdsserver/fd-table.c @@ -147,51 +147,53 @@ size_t fd_table_get(const fd_table_t* restrict this, int key) */ size_t fd_table_put(fd_table_t* restrict this, int key, size_t value) { + /* Override current value if the key is already used. */ if (fd_table_contains_key(this, key)) { size_t rc = fd_table_get(this, key); this->values[key] = value; return rc; } - else + + /* Grow the table if it is too small. */ + errno = 0; + if ((size_t)key >= this->capacity) { - errno = 0; - if ((size_t)key >= this->capacity) + size_t* old_values = this->values; + size_t old_bitcap, new_bitcap; + this->values = realloc(this->values, (this->capacity << 1) * sizeof(size_t)); + if (this->values == NULL) + { + this->values = old_values; + return 0; + } + + memset(this->values + this->capacity, 0, this->capacity * sizeof(size_t)); + + old_bitcap = (this->capacity + 63) / 64; + this->capacity <<= 1; + new_bitcap = (this->capacity + 63) / 64; + + if (new_bitcap > old_bitcap) { - size_t* old_values = this->values; - size_t old_bitcap, new_bitcap; - this->values = realloc(this->values, (this->capacity << 1) * sizeof(size_t)); - if (this->values == NULL) + uint64_t* old_used = this->used; + this->used = realloc(this->used, new_bitcap * sizeof(size_t)); + if (this->used == NULL) { - this->values = old_values; + this->used = old_used; + this->capacity >>= 1; return 0; } - memset(this->values + this->capacity, 0, this->capacity * sizeof(size_t)); - - old_bitcap = (this->capacity + 63) / 64; - this->capacity <<= 1; - new_bitcap = (this->capacity + 63) / 64; - - if (new_bitcap > old_bitcap) - { - uint64_t* old_used = this->used; - this->used = realloc(this->used, new_bitcap * sizeof(size_t)); - if (this->used == NULL) - { - this->used = old_used; - this->capacity >>= 1; - return 0; - } - - memset(this->used + old_bitcap, 0, (new_bitcap - old_bitcap) * sizeof(uint64_t)); - } + memset(this->used + old_bitcap, 0, (new_bitcap - old_bitcap) * sizeof(uint64_t)); } - this->used[key / 64] |= (uint64_t)1 << (key % 64); - this->values[key] = value; - this->size++; - return 0; } + + /* Store the entry. */ + this->used[key / 64] |= (uint64_t)1 << (key % 64); + this->values[key] = value; + this->size++; + return 0; } diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h index d70afc9..1623950 100644 --- a/src/libmdsserver/macros.h +++ b/src/libmdsserver/macros.h @@ -276,7 +276,7 @@ * * @param var The variable to which to assign the allocation * @param elements The number of elements to allocate - * @parma type The data type of the elements for which to create an allocation + * @param type The data type of the elements for which to create an allocation * @return :int Evaluates to true if an only if the allocation failed */ #define xmalloc(var, elements, type) \ @@ -288,12 +288,29 @@ * * @param var The variable to which to assign the allocation * @param elements The number of elements to allocate - * @parma type The data type of the elements for which to create an allocation + * @param type The data type of the elements for which to create an allocation * @return :int Evaluates to true if an only if the allocation failed */ #define xcalloc(var, elements, type) \ ((var = calloc(elements, sizeof(type))) == NULL) +/** + * Go to the label `pfail` if a condition is met + * + * @param CONDITION The condition + */ +#define fail_if(CONDITION) if (CONDITION) goto pfail + + +/** + * Run a set of instructions and return 1 if a condition is met + * + * @param CONDITION The condition + * @param INSTRUCTIONS The instruction (semicolon-terminated) + */ +#define exit_if(CONDITION, INSTRUCTIONS) if (CONDITION) { INSTRUCTIONS return 1; } + + #endif -- cgit v1.2.3-70-g09d2