diff options
Diffstat (limited to '')
-rw-r--r-- | src/libmdsserver/macros.h | 102 |
1 files changed, 61 insertions, 41 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) /** |