diff options
| author | Tyge Løvset <[email protected]> | 2020-09-25 18:17:40 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-09-25 18:17:40 +0200 |
| commit | 7d98b094c9baba498078fba81e44be3299f3cc96 (patch) | |
| tree | 8cc3602a5eba12409f09fdf4ea1332073d401e52 | |
| parent | 569c7c1ed6a9a1b5b6a7c0a80c77d86781847038 (diff) | |
| download | STC-modified-7d98b094c9baba498078fba81e44be3299f3cc96.tar.gz STC-modified-7d98b094c9baba498078fba81e44be3299f3cc96.zip | |
Minor fixes.
| -rw-r--r-- | examples/share_ptr.c | 11 | ||||
| -rw-r--r-- | 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; \
} \
\
|
