Only one alloc per init_new.
This commit is contained in:
1 parent
4fa296c379
commit
ba37d183a7
1 file changed
+22
-13
+22
-13
@@ -64,6 +64,11 @@
|
|||||||
type *data; \
|
type *data; \
|
||||||
} *M_C3(shared_, name, _t)[1]; \
|
} *M_C3(shared_, name, _t)[1]; \
|
||||||
\
|
\
|
||||||
|
typedef struct M_C3(shared_contain_, name, _s) { \
|
||||||
|
type data; \
|
||||||
|
struct M_C3(shared_, name, _s) ptr; \
|
||||||
|
} M_C3(shared_contain_, name, _t)[1]; \
|
||||||
|
\
|
||||||
typedef union { \
|
typedef union { \
|
||||||
type *ptr; \
|
type *ptr; \
|
||||||
const type *cptr; \
|
const type *cptr; \
|
||||||
@@ -91,7 +96,7 @@
|
|||||||
*shared = NULL; \
|
*shared = NULL; \
|
||||||
return; \
|
return; \
|
||||||
} \
|
} \
|
||||||
ptr = M_MEMORY_ALLOC (struct M_C3(shared_, name, _s)); \
|
ptr = M_GET_NEW oplist (struct M_C3(shared_, name, _s)); \
|
||||||
if (ptr == NULL) { \
|
if (ptr == NULL) { \
|
||||||
M_MEMORY_FULL(sizeof(struct M_C3(shared_, name, _s))); \
|
M_MEMORY_FULL(sizeof(struct M_C3(shared_, name, _s))); \
|
||||||
return; \
|
return; \
|
||||||
@@ -105,19 +110,15 @@
|
|||||||
static inline void \
|
static inline void \
|
||||||
M_C3(shared_, name, _init_new)(M_C3(shared_, name, _t) shared) \
|
M_C3(shared_, name, _init_new)(M_C3(shared_, name, _t) shared) \
|
||||||
{ \
|
{ \
|
||||||
struct M_C3(shared_, name, _s) *ptr; \
|
/* NOTE: Alloc 1 struct with both structures. */ \
|
||||||
ptr = M_MEMORY_ALLOC (struct M_C3(shared_, name, _s)); \
|
struct M_C3(shared_contain_, name, _s) *p = \
|
||||||
if (ptr == NULL) { \
|
M_GET_NEW oplist (struct M_C3(shared_contain_, name, _s)); \
|
||||||
M_MEMORY_FULL(sizeof(struct M_C3(shared_, name, _s))); \
|
if (p == NULL) { \
|
||||||
return; \
|
M_MEMORY_FULL(sizeof(struct M_C3(shared_contain_, name, _s))); \
|
||||||
} \
|
|
||||||
type *data; \
|
|
||||||
data = M_GET_NEW oplist (sizeof (type) ); \
|
|
||||||
if (data == NULL) { \
|
|
||||||
M_MEMORY_FREE(ptr); \
|
|
||||||
M_MEMORY_FULL(sizeof(type)); \
|
|
||||||
return; \
|
return; \
|
||||||
} \
|
} \
|
||||||
|
struct M_C3(shared_, name, _s) *ptr = &p->ptr; \
|
||||||
|
type *data = &p->data; \
|
||||||
M_GET_INIT oplist(*data); \
|
M_GET_INIT oplist(*data); \
|
||||||
ptr->cpt = 1; \
|
ptr->cpt = 1; \
|
||||||
ptr->data = data; \
|
ptr->data = data; \
|
||||||
@@ -152,7 +153,15 @@
|
|||||||
if (atomic_fetch_sub(&((*dest)->cpt), 1) == 1) { \
|
if (atomic_fetch_sub(&((*dest)->cpt), 1) == 1) { \
|
||||||
M_GET_CLEAR oplist (*(*dest)->data); \
|
M_GET_CLEAR oplist (*(*dest)->data); \
|
||||||
M_GET_DEL oplist ((*dest)->data); \
|
M_GET_DEL oplist ((*dest)->data); \
|
||||||
M_MEMORY_FREE (*dest); \
|
/* Test if the shared pointer and the type are part of the same \
|
||||||
|
structure */ \
|
||||||
|
/* Note: Not sure it is 100% standard compliant, but I am \
|
||||||
|
pretty sure than no implementation breaks on this. */ \
|
||||||
|
if (M_TYPE_FROM_FIELD(struct M_C3(shared_contain_, name,_s), \
|
||||||
|
(*dest)->data, type, data) \
|
||||||
|
!= M_TYPE_FROM_FIELD(struct M_C3(shared_contain_, name,_s), \
|
||||||
|
*dest, struct M_C3(shared_, name, _s), ptr) ) \
|
||||||
|
M_GET_DEL oplist (*dest); \
|
||||||
} \
|
} \
|
||||||
*dest = NULL; \
|
*dest = NULL; \
|
||||||
} \
|
} \
|
||||||
|
|||||||
Reference in new issue
Block a user