diff --git a/README.md b/README.md index 3d707383..c268c126 100644 --- a/README.md +++ b/README.md @@ -4031,10 +4031,22 @@ Provide the static initialization value of an object defined with a ISHARED\_PTR\_INTERFACE extra fields. It shall be used only for global variables with the \_init\_once function. -Usage: +Usage (provided that the interface is used as the first element of the structure): struct mystruct variable = { ISHARED_PTR_STATIC_INIT(ishared_double, struct mystruct) }; +#### ISHARED\_PTR\_STATIC\_DESIGNATED\_INIT(name, type) + +Provide the static initialization value of an object defined with a +ISHARED\_PTR\_INTERFACE extra fields. It shall be used only for global +variables with the \_init\_once function. + +It uses designated initializers to set the right fields. + +Usage: + + struct mystruct variable = { ISHARED_PTR_STATIC_DESIGNATED_INIT(ishared_double, struct mystruct) }; + #### ISHARED\_PTR\_DEF(name, type[, oplist]) Define the associated methods to handle the shared pointer named 'name' diff --git a/m-i-shared.h b/m-i-shared.h index 7f07af62..cd2c9ae2 100644 --- a/m-i-shared.h +++ b/m-i-shared.h @@ -46,10 +46,14 @@ M_BEGIN_PROTECTED_CODE atomic_int M_C(name, _cpt) -/* Value of the interface field for static intialization. */ -#define ISHARED_PTR_STATIC_INIT(name, type) \ +/* Value of the interface field for static intialization (Uses C99 designated element). */ +#define ISHARED_PTR_STATIC_DESIGNATED_INIT(name, type) \ .M_C(name, _cpt) = ATOMIC_VAR_INIT(0) +/* Value of the interface field for static intialization (Uses C89 designated element). */ +#define ISHARED_PTR_STATIC_INIT(name, type) \ + ATOMIC_VAR_INIT(0) + /* Define the intrusive shared pointer type and its static inline functions. USAGE: ISHARED_PTR_DEF(name, type, [, oplist]) */ diff --git a/tests/test-mishared.c b/tests/test-mishared.c index ee7eeae5..171d81f6 100644 --- a/tests/test-mishared.c +++ b/tests/test-mishared.c @@ -77,7 +77,7 @@ static void test_single(void) ishared1_clear(p2); - struct ishared_s *x = malloc(sizeof (struct ishared_s)); + struct ishared_s *x = (struct ishared_s *) malloc(sizeof (struct ishared_s)); assert(x != NULL); ishared_init_data(x); p1 = ishared1_init(x); @@ -85,7 +85,11 @@ static void test_single(void) ishared1_clear(p1); } -static struct ishared_s g_var = {ISHARED_PTR_STATIC_INIT(ishared2, struct ishared_s) }; +static struct ishared_s g_var = { + ISHARED_PTR_STATIC_INIT(ishared1, struct ishared_s), + ISHARED_PTR_STATIC_INIT(ishared2, struct ishared_s), + {} +}; static void test_static(void) { @@ -105,7 +109,7 @@ static void test_static(void) #define MAX_THREAD 256 #define MAX_VAR 5 -#define VAL(x) { ISHARED_PTR_STATIC_INIT(ishared2, struct ishared_s) } +#define VAL(x) { ISHARED_PTR_STATIC_INIT(ishared1, struct ishared_s), ISHARED_PTR_STATIC_INIT(ishared2, struct ishared_s), {} } static struct ishared_s g_tab[MAX_VAR] = { M_MAP_C(VAL, M_SEQ(1, MAX_VAR)) };