From 7d98b094c9baba498078fba81e44be3299f3cc96 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 25 Sep 2020 18:17:40 +0200 Subject: Minor fixes. --- examples/share_ptr.c | 11 +++++++++-- stc/csptr.h | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/examples/share_ptr.c b/examples/share_ptr.c index 9180fcd4..9b412d93 100644 --- a/examples/share_ptr.c +++ b/examples/share_ptr.c @@ -6,7 +6,7 @@ typedef struct { cstr_t name, last; } Person; -Person* Person_make(Person* p, const char* name, const char* last) { +Person* Person_from(Person* p, const char* name, const char* last) { p->name = cstr(name), p->last = cstr(last); return p; } @@ -31,7 +31,7 @@ int main() { c_forrange (i, 10) { csptr_pe p = csptr_pe_make(c_new(Person)); p.get->name = cstr_from("Name %d", (i * 7) % 10); - p.get->last = cstr_from("Last %d", (i * 9) % 10); + p.get->last = cstr_from("Last %d", (i * 7) % 10); clist_pe_push_back(&queue, p); cvec_pe_push_back(&vec, csptr_pe_share(p)); // Don't forget to share! } @@ -53,6 +53,13 @@ int main() { c_foreach (i, cvec_pe, vec) printf(" %s\n", i.val->get->name.str); + Person lost; Person_from(&lost, "Name 5", "Last 5"); + csptr_pe ptmp = {&lost, NULL}; // share pointer without counter - OK. + clist_pe_iter_t lit = clist_pe_find(&queue, ptmp); + Person_del(&lost); + + if (lit.val) printf("Found: %s\n", lit.val->get->name.str); + puts("Destroy queue:"); clist_pe_del(&queue); diff --git a/stc/csptr.h b/stc/csptr.h index 82f8b3f8..42b8535a 100644 --- a/stc/csptr.h +++ b/stc/csptr.h @@ -89,7 +89,7 @@ typedef long atomic_count_t; #define using_csptr_3(X, Value, valueDestroy) \ typedef Value csptr_##X##_value_t; \ - typedef struct { csptr_##X##_value_t* get; atomic_count_t* use_count; } csptr_##X, csptr_##X##_t; \ + typedef struct { csptr_##X##_value_t* get; atomic_count_t* use_count; } csptr_##X; \ \ STC_INLINE csptr_##X \ csptr_##X##_make(csptr_##X##_value_t* p) { \ @@ -99,7 +99,7 @@ typedef long atomic_count_t; } \ STC_INLINE csptr_##X \ csptr_##X##_share(csptr_##X ptr) { \ - if (ptr.get) atomic_increment(ptr.use_count); \ + if (ptr.use_count) atomic_increment(ptr.use_count); \ return ptr; \ } \ \ -- cgit v1.2.3