diff options
Diffstat (limited to '')
-rw-r--r-- | src/libmdsserver/macros.h | 102 | ||||
-rw-r--r-- | src/libmdsserver/mds-message.c | 17 | ||||
-rw-r--r-- | src/mds-kbdc/compile-layout.c | 5 | ||||
-rw-r--r-- | src/mds-kbdc/tree.c | 8 | ||||
-rw-r--r-- | src/mds-server/slavery.c | 4 |
5 files changed, 75 insertions, 61 deletions
diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h index 7921fb5..b72fd0a 100644 --- a/src/libmdsserver/macros.h +++ b/src/libmdsserver/macros.h @@ -90,10 +90,18 @@ * @param mutex:pthread_mutex_t The mutex * @param instructions The instructions to run while the mutex is locked */ -#define with_mutex(mutex, instructions) \ - errno = pthread_mutex_lock(&(mutex)); \ - instructions \ - errno = pthread_mutex_unlock(&(mutex)) +#define with_mutex(mutex, instructions) \ + do \ + { \ + errno = pthread_mutex_lock(&(mutex)); \ + do \ + { \ + instructions ; \ + } \ + while (0); \ + errno = pthread_mutex_unlock(&(mutex)); \ + } \ + while (0) /** * Wrapper for `pthread_mutex_lock` and `pthread_mutex_unlock` with an embedded if-statement @@ -103,12 +111,18 @@ * @param instructions The instructions to run while the mutex is locked */ #define with_mutex_if(mutex, condition, instructions) \ - errno = pthread_mutex_lock(&(mutex)); \ - if (condition) \ + do \ { \ - instructions \ + errno = pthread_mutex_lock(&(mutex)); \ + if (condition) \ + do \ + { \ + instructions ; \ + } \ + while (0); \ + errno = pthread_mutex_unlock(&(mutex)); \ } \ - errno = pthread_mutex_unlock(&(mutex)) + while (0) /** @@ -142,7 +156,7 @@ * @return [type] A slot that can be set or get */ #define buf_cast(buffer, type, index) \ - ((type*)(buffer))[index] + (((type*)(buffer))[index]) /** @@ -155,7 +169,7 @@ * @return variable: The new value of the element */ #define buf_set(buffer, type, index, variable) \ - ((type*)(buffer))[index] = (variable) + (((type*)(buffer))[index] = (variable)) /** @@ -168,7 +182,7 @@ * @return variable: The value of the element */ #define buf_get(buffer, type, index, variable) \ - variable = ((const type*)(buffer))[index] + (variable = ((const type*)(buffer))[index]) /** @@ -177,10 +191,10 @@ * @param buffer:char* The buffer * @param type A data type * @param count:size_t The number elements of the data type `type` to increase the pointer with - * @retrun buffer: The buffer + * @return buffer: The buffer */ #define buf_next(buffer, type, count) \ - buffer += (count) * sizeof(type) / sizeof(char) + (buffer += (count) * sizeof(type) / sizeof(char)) /** @@ -189,10 +203,10 @@ * @param buffer:char* The buffer * @param type A data type * @param count:size_t The number elements of the data type `type` to decrease the pointer with - * @retrun buffer: The buffer + * @return buffer: The buffer */ #define buf_prev(buffer, type, count) \ - buffer -= (count) * sizeof(type) / sizeof(char) + (buffer -= (count) * sizeof(type) / sizeof(char)) /** @@ -205,8 +219,8 @@ * @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) + (buf_set(buffer, type, 0, variable), \ + buf_next(buffer, type, 1)) /** @@ -219,8 +233,8 @@ * @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) + (buf_get(buffer, type, 0, variable), \ + buf_next(buffer, type, 1)) /** @@ -273,24 +287,26 @@ * * @param condition The condition, it should evaluate the variable `fd` */ -#define close_files(condition) \ -{ \ - DIR* dir = opendir(SELF_FD); \ - struct dirent* file; \ - \ - if (dir == NULL) \ - perror(*argv); /* Well, that is just unfortunate, but we cannot really do anything. */ \ - else \ - while ((file = readdir(dir)) != NULL) \ - if (strcmp(file->d_name, ".") && strcmp(file->d_name, "..")) \ - { \ - int fd = atoi(file->d_name); \ - if (condition) \ - close(fd); \ - } \ - \ - closedir(dir); \ -} +#define close_files(condition) \ + do \ + { \ + DIR* dir = opendir(SELF_FD); \ + struct dirent* file; \ + \ + if (dir == NULL) \ + perror(*argv); /* Well, that is just unfortunate, but we cannot really do anything. */ \ + else \ + while ((file = readdir(dir)) != NULL) \ + if (strcmp(file->d_name, ".") && strcmp(file->d_name, "..")) \ + { \ + int fd = atoi(file->d_name); \ + if (condition) \ + close(fd); \ + } \ + \ + closedir(dir); \ + } \ + while (0) /** @@ -300,10 +316,14 @@ * @param elements:size_t The number of elements, in the array, to free * @scope i:size_t The variable `i` must be declared as `size_t` and avaiable for use */ -#define xfree(array, elements) \ - for (i = 0; i < elements; i++) \ - free((array)[i]); \ - free(array) +#define xfree(array, elements) \ + do \ + { \ + for (i = 0; i < (elements); i++) \ + free((array)[i]); \ + free(array), (array) = NULL; \ + } \ + while (0) /** diff --git a/src/libmdsserver/mds-message.c b/src/libmdsserver/mds-message.c index 68334de..5007f23 100644 --- a/src/libmdsserver/mds-message.c +++ b/src/libmdsserver/mds-message.c @@ -82,14 +82,12 @@ void mds_message_zero_initialise(mds_message_t* restrict this) */ void mds_message_destroy(mds_message_t* restrict this) { + size_t i; if (this->headers != NULL) - { - size_t i; - xfree(this->headers, this->header_count); - } + xfree(this->headers, this->header_count); - free(this->payload); - free(this->buffer); + free(this->payload), this->payload = NULL; + free(this->buffer), this->buffer = NULL; } @@ -136,12 +134,9 @@ static int mds_message_extend_buffer(mds_message_t* restrict this) */ static void reset_message(mds_message_t* restrict this) { + size_t i; if (this->headers != NULL) - { - size_t i; - xfree(this->headers, this->header_count); - this->headers = NULL; - } + xfree(this->headers, this->header_count); this->header_count = 0; free(this->payload); diff --git a/src/mds-kbdc/compile-layout.c b/src/mds-kbdc/compile-layout.c index c9ff65d..8e005c0 100644 --- a/src/mds-kbdc/compile-layout.c +++ b/src/mds-kbdc/compile-layout.c @@ -1071,7 +1071,7 @@ static size_t parse_variable(mds_kbdc_tree_t* restrict tree, const char* restric if (*raw == '0') goto bad; if (*raw == '.') goto bad; if (*raw == '\0') goto bad; - for (raw++; *raw; raw++) + for (; *raw; raw++) /* Check that the variable consists only of digits. */ if (('0' <= *raw) && (*raw <= '9')); /* However, it may end with a dot. */ @@ -2456,7 +2456,8 @@ static int compile_macro_call(mds_kbdc_tree_macro_call_t* restrict tree) /* Duplicate arguments and evaluate function calls, variable dereferences and escapes in the macro call arguments. */ - fail_if (arg = mds_kbdc_tree_dup(tree->arguments), arg == NULL); + if (tree->arguments) + fail_if (arg = mds_kbdc_tree_dup(tree->arguments), arg == NULL); fail_if (bad = evaluate_element(arg), bad < 0); if (bad) return 0; diff --git a/src/mds-kbdc/tree.c b/src/mds-kbdc/tree.c index b32c503..6ee98c1 100644 --- a/src/mds-kbdc/tree.c +++ b/src/mds-kbdc/tree.c @@ -242,7 +242,7 @@ void mds_kbdc_tree_free(mds_kbdc_tree_t* restrict this) /** - * Duplicate a subtree and goto `fail` on failure + * Duplicate a subtree and goto `pfail` on failure * * @param member:identifer The member in the tree to duplicate */ @@ -250,7 +250,7 @@ void mds_kbdc_tree_free(mds_kbdc_tree_t* restrict this) /** - * Duplicate a string and goto `fail` on failure + * Duplicate a string and goto `pfail` on failure * * @param member:identifer The member in the tree to duplicate */ @@ -258,7 +258,7 @@ void mds_kbdc_tree_free(mds_kbdc_tree_t* restrict this) /** - * Duplicate an UTF-32-string and goto `fail` on failure + * Duplicate an UTF-32-string and goto `pfail` on failure * * @param member:identifer The member in the tree to duplicate */ @@ -266,7 +266,7 @@ void mds_kbdc_tree_free(mds_kbdc_tree_t* restrict this) /** - * Duplicate a source code structure and goto `fail` on failure + * Duplicate a source code structure and goto `pfail` on failure * * @param member:identifer The member in the tree to copied */ diff --git a/src/mds-server/slavery.c b/src/mds-server/slavery.c index cf5e45d..088b6af 100644 --- a/src/mds-server/slavery.c +++ b/src/mds-server/slavery.c @@ -145,9 +145,7 @@ client_t* initialise_client(int client_fd) pthread_mutex_unlock(&slave_mutex); free(information); if (entry != LINKED_LIST_UNUSED) - { - with_mutex (slave_mutex, linked_list_remove(&client_list, entry);); - } + with_mutex (slave_mutex, linked_list_remove(&client_list, entry);); return errno = saved_errno, NULL; } |