Clean up shared_ptr interface:
* remove release * add clean
This commit is contained in:
1 parent
8b7dfc8246
commit
5ff056656a
4 files changed
+26
-17
No files matched your search
+8
-5
@@ -41,7 +41,8 @@
|
|||||||
|
|
||||||
/* Interface to add to a structure to allow intrusive support.
|
/* Interface to add to a structure to allow intrusive support.
|
||||||
name: name of the intrusive shared pointer.
|
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) \
|
#define ISHARED_PTR_INTERFACE(name, type) \
|
||||||
int M_C(name, _cpt)
|
int M_C(name, _cpt)
|
||||||
|
|
||||||
@@ -57,9 +58,10 @@
|
|||||||
|
|
||||||
#define ISHAREDI_PTR_OPLIST(name, oplist) ( \
|
#define ISHAREDI_PTR_OPLIST(name, oplist) ( \
|
||||||
INIT(M_INIT_DEFAULT), \
|
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), \
|
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)), \
|
TYPE(M_C3(ishared_,name,_t)), \
|
||||||
OPLIST(oplist), \
|
OPLIST(oplist), \
|
||||||
SUBTYPE(M_C3(ishared_type_,name,_t)), \
|
SUBTYPE(M_C3(ishared_type_,name,_t)), \
|
||||||
@@ -105,9 +107,10 @@
|
|||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
static inline void \
|
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 \
|
static inline void \
|
||||||
|
|||||||
+6
-4
@@ -55,6 +55,7 @@
|
|||||||
INIT_SET(M_C3(shared_, name, _init_set)), \
|
INIT_SET(M_C3(shared_, name, _init_set)), \
|
||||||
SET(M_C3(shared_, name, _set)) \
|
SET(M_C3(shared_, name, _set)) \
|
||||||
INIT_MOVE(M_C3(shared_, name, _init_move)), \
|
INIT_MOVE(M_C3(shared_, name, _init_move)), \
|
||||||
|
CLEAN(M_C3(shared_, name, _clean)), \
|
||||||
MOVE(M_C3(shared_, name, _move)), \
|
MOVE(M_C3(shared_, name, _move)), \
|
||||||
SWAP(M_C3(shared_, name, _swap)) \
|
SWAP(M_C3(shared_, name, _swap)) \
|
||||||
)
|
)
|
||||||
@@ -150,7 +151,7 @@
|
|||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
static inline void \
|
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); \
|
SHAREDI_CONTRACT(dest); \
|
||||||
if (*dest != NULL) { \
|
if (*dest != NULL) { \
|
||||||
@@ -170,9 +171,10 @@
|
|||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
static inline void \
|
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 \
|
static inline void \
|
||||||
@@ -181,7 +183,7 @@
|
|||||||
{ \
|
{ \
|
||||||
SHAREDI_CONTRACT(dest); \
|
SHAREDI_CONTRACT(dest); \
|
||||||
SHAREDI_CONTRACT(shared); \
|
SHAREDI_CONTRACT(shared); \
|
||||||
M_C3(shared_, name, _release)(dest); \
|
M_C3(shared_, name, _clear)(dest); \
|
||||||
M_C3(shared_, name, _init_set)(dest, shared); \
|
M_C3(shared_, name, _init_set)(dest, shared); \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
|
|||||||
@@ -153,15 +153,17 @@ static void test_no_thread(void)
|
|||||||
#include "m-i-shared.h"
|
#include "m-i-shared.h"
|
||||||
// Tiny test structure
|
// Tiny test structure
|
||||||
typedef struct test_s {
|
typedef struct test_s {
|
||||||
|
ISHARED_PTR_INTERFACE(itest, struct test_s);
|
||||||
char buffer[52];
|
char buffer[52];
|
||||||
char bigbuffer[1000000];
|
char bigbuffer[1000000];
|
||||||
ISHARED_PTR_INTERFACE(itest, struct test_s);
|
|
||||||
} test_t;
|
} test_t;
|
||||||
|
|
||||||
static void test_init(test_t *p) { memset(p->buffer, 0x00, 52); }
|
static void test_init(test_t *p) { memset(p->buffer, 0x00, 52); }
|
||||||
static void test_clear(test_t *p) { memset(p->buffer, 0xFF, 52); }
|
static void test_clear(test_t *p) { memset(p->buffer, 0xFF, 52); }
|
||||||
|
|
||||||
ISHARED_PTR_DEF(itest, test_t,
|
ISHARED_PTR_DEF(itest, test_t,
|
||||||
(INIT(test_init M_IPTR), CLEAR(test_clear M_IPTR), DEL(free)))
|
(INIT(test_init M_IPTR), CLEAR(test_clear M_IPTR), DEL(free)))
|
||||||
|
|
||||||
static test_t *test_new(void)
|
static test_t *test_new(void)
|
||||||
{
|
{
|
||||||
test_t *p = malloc (sizeof (test_t));
|
test_t *p = malloc (sizeof (test_t));
|
||||||
@@ -169,8 +171,10 @@ static test_t *test_new(void)
|
|||||||
test_init (p);
|
test_init (p);
|
||||||
return ishared_itest_init(p);
|
return ishared_itest_init(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
BUFFER_DEF(itest, test_t *, 16, BUFFER_PUSH_INIT_POP_MOVE,
|
BUFFER_DEF(itest, test_t *, 16, BUFFER_PUSH_INIT_POP_MOVE,
|
||||||
ISHARED_PTR_OPLIST(itest))
|
ISHARED_PTR_OPLIST(itest))
|
||||||
|
|
||||||
static buffer_itest_t comm1;
|
static buffer_itest_t comm1;
|
||||||
static buffer_itest_t comm2;
|
static buffer_itest_t comm2;
|
||||||
|
|
||||||
@@ -182,7 +186,7 @@ static void *test_conso1(void *arg)
|
|||||||
buffer_itest_pop(&p, comm1);
|
buffer_itest_pop(&p, comm1);
|
||||||
for(int j = 0; j < 52; j++)
|
for(int j = 0; j < 52; j++)
|
||||||
assert (p->buffer[j] == (char) ((j * j * 17) + j * 42 + 1));
|
assert (p->buffer[j] == (char) ((j * j * 17) + j * 42 + 1));
|
||||||
ishared_itest_release(p);
|
ishared_itest_clear(p);
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -194,7 +198,7 @@ static void *test_conso2(void *arg)
|
|||||||
buffer_itest_pop(&p, comm2);
|
buffer_itest_pop(&p, comm2);
|
||||||
for(int j = 0; j < 52; j++)
|
for(int j = 0; j < 52; j++)
|
||||||
assert (p->buffer[j] == (char) ((j * j * 17) + j * 42 + 1));
|
assert (p->buffer[j] == (char) ((j * j * 17) + j * 42 + 1));
|
||||||
ishared_itest_release(p);
|
ishared_itest_clear(p);
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -207,7 +211,7 @@ static void *test_prod(void *arg)
|
|||||||
p->buffer[j] = (j * j * 17) + j * 42 + 1;
|
p->buffer[j] = (j * j * 17) + j * 42 + 1;
|
||||||
buffer_itest_push(comm1, p);
|
buffer_itest_push(comm1, p);
|
||||||
buffer_itest_push(comm2, p);
|
buffer_itest_push(comm2, p);
|
||||||
ishared_itest_release(p);
|
ishared_itest_clear(p);
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,21 +25,21 @@ int main(void)
|
|||||||
shared_int_init_set(p2, p1);
|
shared_int_init_set(p2, p1);
|
||||||
assert(f(p2) == 12);
|
assert(f(p2) == 12);
|
||||||
|
|
||||||
shared_int_release(p1);
|
shared_int_clean(p1);
|
||||||
assert(shared_int_NULL_p(p1));
|
assert(shared_int_NULL_p(p1));
|
||||||
assert(f(p2) == 12);
|
assert(f(p2) == 12);
|
||||||
|
|
||||||
shared_int_set(p1, p2);
|
shared_int_set(p1, p2);
|
||||||
|
|
||||||
shared_int_release(p2);
|
shared_int_clean(p2);
|
||||||
assert(shared_int_NULL_p(p2));
|
assert(shared_int_NULL_p(p2));
|
||||||
|
|
||||||
assert(f(p1) == 12);
|
assert(f(p1) == 12);
|
||||||
shared_int_set(p2, p1);
|
shared_int_set(p2, p1);
|
||||||
assert(f(p2) == 12);
|
assert(f(p2) == 12);
|
||||||
|
|
||||||
shared_int_release(p2);
|
shared_int_clean(p2);
|
||||||
shared_int_release(p1);
|
shared_int_clean(p1);
|
||||||
|
|
||||||
assert(shared_int_NULL_p(p1));
|
assert(shared_int_NULL_p(p1));
|
||||||
assert(shared_int_NULL_p(p2));
|
assert(shared_int_NULL_p(p2));
|
||||||
|
|||||||
Reference in new issue
Block a user