16struct cstl_shared_ptr_data
28 atomic_size_t hard, soft;
46 void *
const ptr = malloc(sz);
58 if (up->clr.func != NULL) {
59 up->clr.func(ptr, up->clr.priv);
71 struct cstl_shared_ptr_data * data;
73 data = malloc(
sizeof(*data));
75 atomic_init(&data->ref.hard, 1);
76 atomic_init(&data->ref.soft, 1);
77 atomic_flag_clear(&data->ref.lock);
94 const struct cstl_shared_ptr_data *
const data =
98 count = atomic_load(&data->ref.soft);
105 const struct cstl_shared_ptr_data *
const data =
116 struct cstl_shared_ptr_data * data;
123 atomic_fetch_add(&data->ref.hard, 1);
124 atomic_fetch_add(&data->ref.soft, 1);
130 struct cstl_shared_ptr_data *
const data =
134 if (atomic_fetch_sub(&data->ref.hard, 1) == 1) {
149 struct cstl_shared_ptr_data * data;
156 atomic_fetch_add(&data->ref.soft, 1);
163 struct cstl_shared_ptr_data * data;
184 while (atomic_flag_test_and_set(&data->ref.lock)) {
199 if (atomic_fetch_add(&data->ref.hard, 1) > 0) {
204 atomic_fetch_add(&data->ref.soft, 1);
207 atomic_fetch_sub(&data->ref.hard, 1);
211 atomic_flag_clear(&data->ref.lock);
217 struct cstl_shared_ptr_data *
const data =
223 if (atomic_fetch_sub(&data->ref.soft, 1) == 1) {
231#include "internal/check.h"
249static void dtor_memclr(
void *
const mem,
void *
const len)
251 memset(mem, 0xa5, (uintptr_t)len);
274 ck_assert_uint_eq((uintptr_t)dtor, (uintptr_t)dtor_memclr);
275 ck_assert_ptr_eq(priv, (
void *)1024);
333Suite * memory_suite(
void)
335 Suite *
const s = suite_create(
"memory");
339 tc = tcase_create(
"memory");
340 tcase_add_test(tc, guarded);
341 tcase_add_test(tc, unique);
342 tcase_add_test(tc, shared);
343 tcase_add_test(tc, weak);
344 suite_add_tcase(s, tc);
void cstl_xtor_func_t(void *obj, void *priv)
Type for functions called to construct, clear, or destroy an object.
static void cstl_guarded_ptr_set(struct cstl_guarded_ptr *const gp, void *const ptr)
Initialize a guarded pointer object to a specific pointer value.
#define DECLARE_CSTL_GUARDED_PTR(NAME)
Declare and initialize a guarded pointer.
static void cstl_guarded_ptr_copy(struct cstl_guarded_ptr *const dst, const struct cstl_guarded_ptr *const src)
Copy the cstl_guarded_ptr object to a new location.
static const void * cstl_guarded_ptr_get_const(const struct cstl_guarded_ptr *const gp)
Retrieve the stored pointer value.
static void * cstl_guarded_ptr_get(struct cstl_guarded_ptr *const gp)
Retrieve the stored pointer value.
void cstl_shared_ptr_reset(cstl_shared_ptr_t *const sp)
Stop managing the underlying memory via this object.
bool cstl_shared_ptr_unique(const cstl_shared_ptr_t *const sp)
Determine if a shared pointer uniquely owns the underlying memory.
static void * cstl_shared_ptr_get(cstl_shared_ptr_t *const sp)
Get a pointer to the memory managed by the object.
void cstl_shared_ptr_share(const cstl_shared_ptr_t *const e, cstl_shared_ptr_t *const n)
Create a new shared pointer object to manage the underlying memory.
void cstl_shared_ptr_alloc(cstl_shared_ptr_t *const sp, const size_t sz, cstl_xtor_func_t *const clr)
Dynamically allocated memory to be shared via the object.
#define DECLARE_CSTL_SHARED_PTR(NAME)
Compile-time declaration and initialization of a shared pointer.
const void * cstl_shared_ptr_get_const(const cstl_shared_ptr_t *const sp)
Get a pointer to the memory managed by the object.
static void cstl_unique_ptr_init(cstl_unique_ptr_t *const up)
Initialize a unique pointer.
static void * cstl_unique_ptr_get(cstl_unique_ptr_t *const up)
Get the pointer managed by the unique pointer object.
void cstl_unique_ptr_reset(cstl_unique_ptr_t *const up)
Free the memory managed by a unique pointer.
void cstl_unique_ptr_alloc(cstl_unique_ptr_t *const up, const size_t sz, cstl_xtor_func_t *const clr, void *const priv)
Dynamically allocate memory to be managed by the unique pointer.
static void * cstl_unique_ptr_release(cstl_unique_ptr_t *const up, cstl_xtor_func_t **const clr, void **priv)
Stop a unique pointer object from managing a pointer.
static const void * cstl_unique_ptr_get_const(const cstl_unique_ptr_t *const up)
Get the pointer managed by the unique pointer object.
#define DECLARE_CSTL_UNIQUE_PTR(NAME)
Declare and initialize a unique pointer.
#define DECLARE_CSTL_WEAK_PTR(NAME)
Compile-time declaration and initialization of a weak pointer.
void cstl_weak_ptr_reset(cstl_weak_ptr_t *const wp)
Drop the reference to the underlying managed memory.
void cstl_weak_ptr_from(cstl_weak_ptr_t *const wp, const cstl_shared_ptr_t *const sp)
Create a weak pointer from a shared pointer.
void cstl_weak_ptr_lock(const cstl_weak_ptr_t *const wp, cstl_shared_ptr_t *const sp)
Convert a weak pointer to a shared pointer.
The shared pointer object.
A pointer that has a single "owner".