From 79c83eba7933f49e766dbeb85f9c40ee17e06edf Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 22 Dec 2021 12:35:36 +0100 Subject: Renamed csptr to carc. i_key/val_ref renamed to i_key/val_sptr. Change inspired by Rust Arc/Rc. cbox name is taken from Rust Box type. --- examples/box.c | 3 +-- examples/box2.c | 8 ++++---- examples/new_sptr.c | 16 ++++++++-------- examples/sptr.c | 4 ++-- examples/sptr_demo.c | 10 +++++----- examples/sptr_erase.c | 8 ++++---- examples/sptr_music.c | 4 ++-- examples/sptr_pthread.c | 20 +++++++++++--------- examples/sptr_to_maps.c | 8 ++++---- 9 files changed, 41 insertions(+), 40 deletions(-) (limited to 'examples') diff --git a/examples/box.c b/examples/box.c index ae10a89f..d97545b6 100644 --- a/examples/box.c +++ b/examples/box.c @@ -28,10 +28,9 @@ void Person_drop(Person* p) { #include #define i_type Persons -#define i_val_ref PBox // binds PBox_cmp, ... +#define i_val_sptr PBox // informs that PBox is a smart pointer. #include - int main() { c_auto (Persons, vec) diff --git a/examples/box2.c b/examples/box2.c index 5549dade..d79ea32d 100644 --- a/examples/box2.c +++ b/examples/box2.c @@ -26,8 +26,8 @@ struct { #include // cbox_Rectangle // Box in box: -#define i_val_bind cbox_Point // NB: adviced to use i_val_arc when value is a cbox or csptr! - // it will auto-set i_drop, i_from, i_cmp for you. +#define i_val_sptr cbox_Point // NB: adviced to use i_val_sptr when value is a cbox or carc! + // it will auto-set i_drop, i_from, i_cmp for you. #define i_opt c_no_cmp #define i_tag BoxPoint #include // cbox_BoxPoint @@ -62,10 +62,10 @@ int main(void) { }); // The output of functions can be boxed - boxed_point = cbox_Point_new(origin()); + boxed_point = cbox_Point_from(origin()); // Double indirection - box_in_a_box = cbox_BoxPoint_new(boxed_origin()); + box_in_a_box = cbox_BoxPoint_from(boxed_origin()); printf("Point occupies %zu bytes on the stack\n", sizeof(point)); diff --git a/examples/new_sptr.c b/examples/new_sptr.c index ce31478e..045eef77 100644 --- a/examples/new_sptr.c +++ b/examples/new_sptr.c @@ -14,29 +14,29 @@ void Person_drop(Person* p) { #define i_drop Person_drop #define i_opt c_no_cmp #define i_tag person -#include +#include // ... #define i_val int #define i_drop(x) printf("drop: %d\n", *(x)) -#include +#include -#define i_val_bind csptr_int +#define i_val_bind carc_int #define i_tag iptr #include int main(void) { - c_autovar (csptr_person p = csptr_person_from(Person_new("John", "Smiths")), csptr_person_drop(&p)) - c_autovar (csptr_person q = csptr_person_clone(p), csptr_person_drop(&q)) // share the pointer + c_autovar (carc_person p = carc_person_from(Person_new("John", "Smiths")), carc_person_drop(&p)) + c_autovar (carc_person q = carc_person_clone(p), carc_person_drop(&q)) // share the pointer { printf("%s %s. uses: %lu\n", q.get->name.str, q.get->last.str, *q.use_count); } c_auto (cstack_iptr, stk) { - cstack_iptr_push(&stk, csptr_int_from(10)); - cstack_iptr_push(&stk, csptr_int_from(20)); - cstack_iptr_push(&stk, csptr_int_from(30)); + cstack_iptr_push(&stk, carc_int_from(10)); + cstack_iptr_push(&stk, carc_int_from(20)); + cstack_iptr_push(&stk, carc_int_from(30)); cstack_iptr_emplace(&stk, *cstack_iptr_top(&stk)); cstack_iptr_emplace(&stk, *cstack_iptr_begin(&stk).ref); diff --git a/examples/sptr.c b/examples/sptr.c index e1d734e0..0c22bd68 100644 --- a/examples/sptr.c +++ b/examples/sptr.c @@ -25,10 +25,10 @@ void Person_drop(Person* p) { #define i_type PSPtr #define i_val_bind Person // binds Person_cmp, ... -#include +#include #define i_type Persons -#define i_val_ref PSPtr // binds PSPtr_cmp, ... +#define i_val_sptr PSPtr // binds PSPtr_cmp, ... #include diff --git a/examples/sptr_demo.c b/examples/sptr_demo.c index a5c642b0..8626e2d4 100644 --- a/examples/sptr_demo.c +++ b/examples/sptr_demo.c @@ -5,18 +5,18 @@ void int_drop(int* x) { printf("drop: %d\n", *x); } -// csptr implements its own clone method using reference counting, +// carc implements its own clone method using reference counting, // so 'i_valfrom' need not be defined (will be ignored). -#define i_type iref // set type name to be defined (instead of 'csptr_int') +#define i_type iref // set type name to be defined (instead of 'carc_int') #define i_val int #define i_drop int_drop // optional, just to display the elements destroyed -#include // iref +#include // iref -#define i_key_ref iref // note: use i_key_bind instead of i_key for csptr/cbox elements +#define i_key_sptr iref // note: use i_key_bind instead of i_key for carc/cbox elements #include // csset_iref (like: std::set>) -#define i_val_ref iref // note: as above. +#define i_val_sptr iref // note: as above. #include // cvec_iref (like: std::vector>) int main() diff --git a/examples/sptr_erase.c b/examples/sptr_erase.c index fbb141b4..d40d8fe7 100644 --- a/examples/sptr_erase.c +++ b/examples/sptr_erase.c @@ -5,14 +5,14 @@ void show_drop(int* x) { printf("drop: %d\n", *x); } #define i_type Arc #define i_val int #define i_drop show_drop -// csptr/cbox will use pointer address comparison of i_val +// carc/cbox will use pointer address comparison of i_val // if 'i_opt c_no_cmp' is defined, otherwise i_cmp is used -// to compare object values. See the twodifferences by +// to compare object values. See the two differences by // commenting out the next line. -#include // Shared pointer to int +#include // Shared pointer to int #define i_type Vec -#define i_val_ref Arc +#define i_val_sptr Arc #include // Vec: cvec diff --git a/examples/sptr_music.c b/examples/sptr_music.c index 1d58cc46..42bb3455 100644 --- a/examples/sptr_music.c +++ b/examples/sptr_music.c @@ -21,10 +21,10 @@ void Song_drop(Song* s) { #define i_val Song #define i_drop Song_drop #define i_opt c_no_cmp -#include +#include #define i_type SongVec -#define i_val_ref SongPtr +#define i_val_sptr SongPtr #include void example3() diff --git a/examples/sptr_pthread.c b/examples/sptr_pthread.c index d71ce97d..d81d9810 100644 --- a/examples/sptr_pthread.c +++ b/examples/sptr_pthread.c @@ -11,13 +11,13 @@ struct Base int value; } typedef Base; +#define i_type BaseRc #define i_val Base -#define i_drop(x) printf("Base::~Base()\n") -#define i_tag base +#define i_valdrop(x) printf("Drop Base: %d\n", (x)->value) #define i_opt c_no_cmp -#include +#include -void* thr(csptr_base* lp) +void* thr(BaseRc* lp) { sleep(1); static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; @@ -25,30 +25,32 @@ void* thr(csptr_base* lp) { printf("local pointer in a thread:\n" " p.get() = %p, p.use_count() = %ld\n", (void*)lp->get, *lp->use_count); + /* safe to modify base here */ + lp->get->value += 1; } /* atomically decrease ref. */ - csptr_base_drop(lp); + BaseRc_drop(lp); return NULL; } int main() { - csptr_base p = csptr_base_from((Base){42}); + BaseRc p = BaseRc_from((Base){0}); printf("Created a Base\n" " p.get() = %p, p.use_count() = %ld\n", (void*)p.get, *p.use_count); enum {N = 3}; pthread_t t[N]; - csptr_base c[N]; + BaseRc c[N]; c_forrange (i, N) { - c[i] = csptr_base_clone(p); + c[i] = BaseRc_clone(p); pthread_create(&t[i], NULL, (void*(*)(void*))thr, &c[i]); } printf("Shared ownership between %d threads and released\n" "ownership from main:\n" " p.get() = %p, p.use_count() = %ld\n", N, (void*)p.get, *p.use_count); - csptr_base_reset(&p); + BaseRc_reset(&p); c_forrange (i, N) pthread_join(t[i], NULL); printf("All threads completed, the last one deleted Base\n"); diff --git a/examples/sptr_to_maps.c b/examples/sptr_to_maps.c index 64866ba3..0873d297 100644 --- a/examples/sptr_to_maps.c +++ b/examples/sptr_to_maps.c @@ -12,14 +12,14 @@ // no need for atomic ref. count in single thread: // no compare function available for csmap: #define i_opt c_no_atomic|c_no_cmp -#include +#include #define i_type Stack -#define i_val_ref Arc // define i_val_bind for csptr/cbox value (not i_val) +#define i_val_sptr Arc // define i_val_bind for carc/cbox value (not i_val) #include #define i_type List -#define i_val_ref Arc // as above +#define i_val_sptr Arc // as above #include int main() @@ -44,7 +44,7 @@ int main() {"Steve", 1979}, {"Rick", 1974}, {"Tracy", 2003} }); - // Share two Maps from the stack with the list using emplace (clone the csptr): + // Share two Maps from the stack with the list using emplace (clone the carc): List_push_back(&list, Arc_clone(stack.data[0])); List_push_back(&list, Arc_clone(stack.data[1])); -- cgit v1.2.3