Clean up shared_ptr interface:

* remove release
* add clean
This commit is contained in:
Patrick Pelissier committed 2017-03-16 22:26:15 +01:00
1 parent 8b7dfc8246
commit 5ff056656a
4 files changed
+26 -17

No files matched your search

+8 -5
View File
@@ -41,7 +41,8 @@
/* Interface to add to a structure to allow intrusive support.
name: name of the intrusive shared pointer.
type: name of the type of the structure (aka. struct test_s) - not used currently */
type: name of the type of the structure (aka. struct test_s) - not used currently.
NOTE: There can be only one interface of this kind in a type! */
#define ISHARED_PTR_INTERFACE(name, type) \
int M_C(name, _cpt)
@@ -57,9 +58,10 @@
#define ISHAREDI_PTR_OPLIST(name, oplist) ( \
INIT(M_INIT_DEFAULT), \
CLEAR(M_C3(ishared_, name, _clear)), \
SET(M_C3(ishared_, name, _set) M_IPTR), \
INIT_SET(M_C3(ishared_, name, _init_set2) M_IPTR), \
SET(M_C3(ishared_, name, _set) M_IPTR), \
CLEAR(M_C3(ishared_, name, _clear)), \
CLEAN(M_C3(ishared_, name, _clean) M_IPTR), \
TYPE(M_C3(ishared_,name,_t)), \
OPLIST(oplist), \
SUBTYPE(M_C3(ishared_type_,name,_t)), \
@@ -105,9 +107,10 @@
} \
\
static inline void \
M_C3(ishared_, name, _release)(type *shared) \
M_C3(ishared_, name, _clean)(type **shared) \
{ \
M_C3(ishared_, name, _clear)(shared); \
M_C3(ishared_, name, _clear)(*shared); \
*shared = NULL; \
} \
\
static inline void \
+6 -4
View File
@@ -55,6 +55,7 @@
INIT_SET(M_C3(shared_, name, _init_set)), \
SET(M_C3(shared_, name, _set)) \
INIT_MOVE(M_C3(shared_, name, _init_move)), \
CLEAN(M_C3(shared_, name, _clean)), \
MOVE(M_C3(shared_, name, _move)), \
SWAP(M_C3(shared_, name, _swap)) \
)
@@ -150,7 +151,7 @@
} \
\
static inline void \
M_C3(shared_, name, _release)(M_C3(shared_, name, _t) dest) \
M_C3(shared_, name, _clear)(M_C3(shared_, name, _t) dest) \
{ \
SHAREDI_CONTRACT(dest); \
if (*dest != NULL) { \
@@ -170,9 +171,10 @@
} \
\
static inline void \
M_C3(shared_, name, _clear)(M_C3(shared_, name, _t) dest) \
M_C3(shared_, name, _clean)(M_C3(shared_, name, _t) dest) \
{ \
M_C3(shared_, name, _release)(dest); \
M_C3(shared_, name, _clear)(dest); \
*dest = NULL; \
} \
\
static inline void \
@@ -181,7 +183,7 @@
{ \
SHAREDI_CONTRACT(dest); \
SHAREDI_CONTRACT(shared); \
M_C3(shared_, name, _release)(dest); \
M_C3(shared_, name, _clear)(dest); \
M_C3(shared_, name, _init_set)(dest, shared); \
} \
\
+8 -4
View File
@@ -153,15 +153,17 @@ static void test_no_thread(void)
#include "m-i-shared.h"
// Tiny test structure
typedef struct test_s {
ISHARED_PTR_INTERFACE(itest, struct test_s);
char buffer[52];
char bigbuffer[1000000];
ISHARED_PTR_INTERFACE(itest, struct test_s);
} test_t;
static void test_init(test_t *p) { memset(p->buffer, 0x00, 52); }
static void test_clear(test_t *p) { memset(p->buffer, 0xFF, 52); }
ISHARED_PTR_DEF(itest, test_t,
(INIT(test_init M_IPTR), CLEAR(test_clear M_IPTR), DEL(free)))
static test_t *test_new(void)
{
test_t *p = malloc (sizeof (test_t));
@@ -169,8 +171,10 @@ static test_t *test_new(void)
test_init (p);
return ishared_itest_init(p);
}
BUFFER_DEF(itest, test_t *, 16, BUFFER_PUSH_INIT_POP_MOVE,
ISHARED_PTR_OPLIST(itest))
static buffer_itest_t comm1;
static buffer_itest_t comm2;
@@ -182,7 +186,7 @@ static void *test_conso1(void *arg)
buffer_itest_pop(&p, comm1);
for(int j = 0; j < 52; j++)
assert (p->buffer[j] == (char) ((j * j * 17) + j * 42 + 1));
ishared_itest_release(p);
ishared_itest_clear(p);
}
return NULL;
}
@@ -194,7 +198,7 @@ static void *test_conso2(void *arg)
buffer_itest_pop(&p, comm2);
for(int j = 0; j < 52; j++)
assert (p->buffer[j] == (char) ((j * j * 17) + j * 42 + 1));
ishared_itest_release(p);
ishared_itest_clear(p);
}
return NULL;
}
@@ -207,7 +211,7 @@ static void *test_prod(void *arg)
p->buffer[j] = (j * j * 17) + j * 42 + 1;
buffer_itest_push(comm1, p);
buffer_itest_push(comm2, p);
ishared_itest_release(p);
ishared_itest_clear(p);
}
return NULL;
}
+4 -4
View File
@@ -25,21 +25,21 @@ int main(void)
shared_int_init_set(p2, p1);
assert(f(p2) == 12);
shared_int_release(p1);
shared_int_clean(p1);
assert(shared_int_NULL_p(p1));
assert(f(p2) == 12);
shared_int_set(p1, p2);
shared_int_release(p2);
shared_int_clean(p2);
assert(shared_int_NULL_p(p2));
assert(f(p1) == 12);
shared_int_set(p2, p1);
assert(f(p2) == 12);
shared_int_release(p2);
shared_int_release(p1);
shared_int_clean(p2);
shared_int_clean(p1);
assert(shared_int_NULL_p(p1));
assert(shared_int_NULL_p(p2));