Skip to content

Commit

Permalink
Update shared pointer new interface
Browse files Browse the repository at this point in the history
  • Loading branch information
P-p-H-d committed Nov 24, 2024
1 parent f21d427 commit c461b4e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
16 changes: 16 additions & 0 deletions m-shared-ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ fattr void M_F(name, _write_read2_unlock)(shared_t *out, const shared_t *src1, c
M_IF_METHOD(INIT, oplist)( extern shared_t *M_F(name, _new)(void); , ) \
M_IF_METHOD(INIT_SET, oplist)( extern shared_t *M_F(name, _new_copy)(const shared_t *); , ) \
M_IF_METHOD(SET, oplist)( extern void M_F(name, _copy)(shared_t *, const shared_t *); , ) \
M_IF_METHOD2(INIT_SET, TYPE, oplist)( extern shared_t *M_F(name, _new_from)(M_GET_TYPE oplist const); , ) \
extern shared_t *M_F(name, _acquire)(shared_t *); \
extern void M_F(name, _release)(shared_t *); \
extern void M_F(name, _set)(shared_t **, shared_t *); \
Expand Down Expand Up @@ -663,6 +664,21 @@ M_IF_METHOD(INIT_SET, oplist)( \
M_F(name, _read_unlock)(src); \
M_F(name, _init_lock)(out); \
return out; \
} \
\
M_IF_METHOD(TYPE,oplist)( , \
fattr shared_t *M_F(name, _new_from)(type const src); ) \
fattr shared_t *M_F(name, _new_from)(type const src) \
{ \
shared_t *out = M_CALL_NEW(oplist, shared_t); \
if (M_UNLIKELY_NOMEM( out == NULL)) { \
M_MEMORY_FULL(sizeof (shared_t)); \
abort(); \
} \
M_ON_EXCEPTION( M_CALL_FREE(oplist, out) ) \
M_CALL_INIT_SET(oplist, out->data, src); \
M_F(name, _init_lock)(out); \
return out; \
} \
, ) \
\
Expand Down
30 changes: 22 additions & 8 deletions tests/test-mshared-ptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@
SHARED_PTR_DECL_AS(shared_double, SharedDouble, M_BASIC_OPLIST)
SHARED_PTR_DEF_EXTERN_AS(shared_double, SharedDouble, double, M_BASIC_OPLIST)

static void test_double(void)
{
SharedDouble *p = shared_double_new();
SharedDouble *q = shared_double_new_from(2.0);
SharedDouble *r = shared_double_new_from(1.0);
SharedDouble *rr = shared_double_new_from(3.0);
shared_double_add(p, q, r);
assert(shared_double_equal_p(p, rr));
shared_double_sub(p, q, r);
assert(shared_double_equal_p(p, r));
shared_double_mul(p, q, r);
assert(shared_double_equal_p(p, q));
shared_double_div(p, q, r);
assert(shared_double_equal_p(p, q));
shared_double_reset(p);
shared_double_release(p);
shared_double_release(q);
shared_double_release(r);
shared_double_release(rr);
}

// TEST WITH STRING

SHARED_PTR_DECL(shared_string, STRING_OPLIST)
Expand Down Expand Up @@ -286,19 +307,12 @@ static void test_thread(void)
m_thread_join(idx);
}

static void test_double(void)
{
SharedDouble *d = shared_double_new();
shared_double_reset(d);
shared_double_clear(d);
}

int main(void)
{
test_string();
test_double();
test_array();
test_array_string();
test_thread();
test_double();
exit(0);
}

0 comments on commit c461b4e

Please sign in to comment.