diff options
Diffstat (limited to 'src/libmdsserver')
-rw-r--r-- | src/libmdsserver/macros.h | 68 | ||||
-rw-r--r-- | src/libmdsserver/mds-message.c | 17 |
2 files changed, 43 insertions, 42 deletions
diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h index 89e47fa..40d68d0 100644 --- a/src/libmdsserver/macros.h +++ b/src/libmdsserver/macros.h @@ -142,7 +142,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 +155,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 +168,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]) /** @@ -180,7 +180,7 @@ * @return buffer: The buffer */ #define buf_next(buffer, type, count) \ - buffer += (count) * sizeof(type) / sizeof(char) + (buffer += (count) * sizeof(type) / sizeof(char)) /** @@ -192,7 +192,7 @@ * @return buffer: The buffer */ #define buf_prev(buffer, type, count) \ - buffer -= (count) * sizeof(type) / sizeof(char) + (buffer -= (count) * sizeof(type) / sizeof(char)) /** @@ -205,8 +205,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 +219,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 +273,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 +302,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 d616355..37ebf85 100644 --- a/src/libmdsserver/mds-message.c +++ b/src/libmdsserver/mds-message.c @@ -81,14 +81,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; } @@ -133,12 +131,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); |