From ae75ed2772e0cb51e658f4f0ebb2020b0031a125 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 5 May 2014 21:51:14 +0200 Subject: use more macros MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libmdsserver/macros.h | 97 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) (limited to 'src/libmdsserver/macros.h') diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h index 392e0ff..2690917 100644 --- a/src/libmdsserver/macros.h +++ b/src/libmdsserver/macros.h @@ -19,8 +19,11 @@ #define MDS_LIBMDSSERVER_MACROS_H +/* #include #include +#include +*/ /** @@ -55,5 +58,99 @@ fprintf(stderr, "%s: " format "\n", *argv, __VA_ARGS__); +/** + * Wrapper for `pthread_mutex_lock` and `pthread_mutex_unlock` + * + * @param mutex:pthread_mutex_t The mutex + * @param instructions The instructions to run while the mutex is locked + */ +#define with_mutex(mutex, instructions) \ + pthread_mutex_lock(&(mutex)); \ + instructions \ + pthread_mutex_unlock(&(mutex)) + + +/** + * Return the maximum value of two values + * + * @param a One of the values + * @param b The other one of the values + * @return The maximum value + */ +#define max(a, b) \ + (a < b ? b : a) + + +/** + * Return the minimum value of two values + * + * @param a One of the values + * @param b The other one of the values + * @return The minimum value + */ +#define min(a, b) \ + (a < b ? a : b) + + +/** + * Cast a buffer to another type and get the slot for an element + * + * @param buffer:char* The buffer + * @param type The data type of the elements for the data type to cast the buffer to + * @param index:size_t The index of the element to address + * @return [type] A slot that can be set or get + */ +#define buf_cast(buffer, type, index) \ + ((type*)(buffer))[index] + + +/** + * Set the value of an element a buffer that is being cast + * + * @param buffer:char* The buffer + * @param type The data type of the elements for the data type to cast the buffer to + * @param index:size_t The index of the element to address + * @param variable:type The new value of the element + * @return :variable The new value of the element + */ +#define buf_set(buffer, type, index, variable) \ + ((type*)(buffer))[index] = (variable) + + +/** + * Get the value of an element a buffer that is being cast + * + * @param buffer:char* The buffer + * @param type The data type of the elements for the data type to cast the buffer to + * @param index:size_t The index of the element to address + * @param variable:type Slot to set with the value of the element + * @return :variable The value of the element + */ +#define buf_get(buffer, type, index, variable) \ + variable = ((type*)(buffer))[index] + + +/** + * Increase the pointer of a buffer + * + * @param buffer:char* The buffer + * @param type A data type + * @param count The number elements of the data type `type` to increase the pointer with + */ +#define buf_next(buffer, type, count) \ + buffer += (count) * sizeof(type) / sizeof(char) + + +/** + * Decrease the pointer of a buffer + * + * @param buffer:char* The buffer + * @param type A data type + * @param count The number elements of the data type `type` to decrease the pointer with + */ +#define buf_prev(buffer, type, count) \ + buffer -= (count) * sizeof(type) / sizeof(char) + + #endif -- cgit v1.2.3-70-g09d2