From ead25c905a1fd75eed1cbb7b387c9fda466a69fd Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Thu, 7 Mar 2019 07:42:50 +0100 Subject: [PATCH] Factorize common macro for FLA --- m-core.h | 28 ++++++++++++++++++++++------ m-deque.h | 10 +--------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/m-core.h b/m-core.h index 8cfca232..80f91cc4 100644 --- a/m-core.h +++ b/m-core.h @@ -1472,6 +1472,7 @@ M_PARSE_DEFAULT_TYPE_DEF(m_core_parse_ldouble, long double, strtold, ) (y) = _tmp; \ } while (0) + /* Check if 'n' is assignable to an object of type 'type'. It is as if we create a temporary of type 'type' and assign 'n' to it. Return the object 'n' if it is possible. @@ -1484,6 +1485,7 @@ M_PARSE_DEFAULT_TYPE_DEF(m_core_parse_ldouble, long double, strtold, ) # define M_ASSIGN_CAST(type, n) static_cast(n) #endif + /* Cast 'n' of type 'type*' into 'type const*'. This is like (type const*)p but safer as the type of 'n' is checked, and more robust for double arrays type. @@ -1499,10 +1501,12 @@ M_PARSE_DEFAULT_TYPE_DEF(m_core_parse_ldouble, long double, strtold, ) # define M_CONST_CAST(type, n) const_cast(n) #endif + /* * From a pointer to the 'field' field of type 'field_type' of a 'type' structure, * return the pointer to the structure. - * NOTE: Cast Inside! + * Used to handle intrusive structure. + * NOTE: Use an ASSIGN_CAST to make the cast a little bit safer. */ #define M_TYPE_FROM_FIELD(type, ptr, field_type, field) \ ((type *)(void*)( (char *)M_ASSIGN_CAST(field_type*, (ptr)) - offsetof(type, field) )) @@ -1510,13 +1514,25 @@ M_PARSE_DEFAULT_TYPE_DEF(m_core_parse_ldouble, long double, strtold, ) #define M_CTYPE_FROM_FIELD(type, ptr, field_type, field) \ ((type const *)(const void*)( (const char *)M_ASSIGN_CAST(field_type const *, (ptr)) - offsetof(type const, field) )) -/* Used to generate alignement field for cache alignement within a structure - Take the name of the field, and a list of the type of the fiels that previously fill in the cache line. + +/* Use to generate a dummy alignment field for cache alignment within a structure + Take the name of the dummy field, and a list of the type of the fields that previously fill in the structure. */ -#define M_SIZEOF(id) sizeof (id) -#define M_ADD_SIZE(a,b) a + b +#define M_APPLY_SIZEOF(id) sizeof (id) +#define M_ADD_SIZE(a,b) a + b #define M_CACHELINE_ALIGN(name, ...) \ - char name[M_ALIGN_FOR_CACHELINE_EXCLUSION > M_REDUCE(M_SIZEOF, M_ADD_SIZE, __VA_ARGS__) ? M_ALIGN_FOR_CACHELINE_EXCLUSION - M_REDUCE(M_SIZEOF, M_ADD_SIZE, __VA_ARGS__) : 1] + char name[M_ALIGN_FOR_CACHELINE_EXCLUSION > M_REDUCE(M_APPLY_SIZEOF, M_ADD_SIZE, __VA_ARGS__) \ + ? M_ALIGN_FOR_CACHELINE_EXCLUSION - M_REDUCE(M_APPLY_SIZEOF, M_ADD_SIZE, __VA_ARGS__) : 1] + + +/* C++ doesn't support flexible array within a structure. + Let's define at least one element for an array. */ +#ifdef __cplusplus +# define M_MIN_FLEX_ARRAY_SIZE 1 +#else +# define M_MIN_FLEX_ARRAY_SIZE +#endif + /************************************************************/ diff --git a/m-deque.h b/m-deque.h index f0505535..d827e937 100644 --- a/m-deque.h +++ b/m-deque.h @@ -46,14 +46,6 @@ #define DEQUEUI_DEFAULT_SIZE 8 -/* C++ doesn't support flexible array within a structure. - Let's define at least one element. */ -#ifdef __cplusplus -# define DEQUEI_MIN_ARRAY_SIZE 1 -#else -# define DEQUEI_MIN_ARRAY_SIZE -#endif - #define DEQUEI_CONTRACT(d) do { \ assert ((d) != NULL); \ assert ((d)->default_size >= DEQUEUI_DEFAULT_SIZE); \ @@ -74,7 +66,7 @@ typedef struct M_C(name, _node_s) { \ ILIST_INTERFACE(M_C(name, _node_list), struct M_C(name, _node_s)); \ size_t size; \ - type data[DEQUEI_MIN_ARRAY_SIZE]; \ + type data[M_MIN_FLEX_ARRAY_SIZE]; \ } node_t; \ \ /* Each node is allocated with a variable size (so we use \