Skip to content

Commit

Permalink
fix: revert type casting
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Dec 22, 2023
1 parent e6e8798 commit 342fcce
Showing 1 changed file with 53 additions and 54 deletions.
107 changes: 53 additions & 54 deletions include/zenoh-pico/collections/pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,60 +41,59 @@
#endif // __cplusplus

/*------------------ Internal Array Macros ------------------*/
#define _Z_POINTER_DEFINE(name, type) \
typedef struct { \
type##_t *ptr; \
_z_atomic(unsigned int) * _cnt; \
} name##_sptr_t; \
static inline name##_sptr_t name##_sptr_new(type##_t val) { \
name##_sptr_t p; \
p.ptr = (type##_t *)z_malloc(sizeof(type##_t)); \
if (p.ptr != NULL) { \
p._cnt = (_z_atomic(unsigned int) *)z_malloc(sizeof(_z_atomic(unsigned int) *)); \
if (p._cnt != NULL) { \
*p.ptr = val; \
_z_atomic_store_explicit((volatile unsigned int *)p._cnt, 1, _z_memory_order_relaxed); \
} else { \
z_free(p.ptr); \
} \
} \
return p; \
} \
static inline name##_sptr_t name##_sptr_clone(name##_sptr_t *p) { \
name##_sptr_t c; \
c._cnt = p->_cnt; \
c.ptr = p->ptr; \
_z_atomic_fetch_add_explicit((volatile unsigned int *)p->_cnt, 1, _z_memory_order_relaxed); \
return c; \
} \
static inline name##_sptr_t *name##_sptr_clone_as_ptr(name##_sptr_t *p) { \
name##_sptr_t *c = (name##_sptr_t *)z_malloc(sizeof(name##_sptr_t)); \
if (c != NULL) { \
c->_cnt = p->_cnt; \
c->ptr = p->ptr; \
_z_atomic_fetch_add_explicit((volatile unsigned int *)p->_cnt, 1, _z_memory_order_relaxed); \
} \
return c; \
} \
static inline _Bool name##_sptr_eq(const name##_sptr_t *left, const name##_sptr_t *right) { \
return (left->ptr == right->ptr); \
} \
static inline _Bool name##_sptr_drop(name##_sptr_t *p) { \
_Bool dropped = false; \
if (p->_cnt != NULL) { \
unsigned int c = \
_z_atomic_fetch_sub_explicit((volatile unsigned int *)p->_cnt, 1, _z_memory_order_release); \
dropped = c == 1; \
if (dropped == true) { \
atomic_thread_fence(_z_memory_order_acquire); \
if (p->ptr != NULL) { \
type##_clear(p->ptr); \
z_free(p->ptr); \
z_free((void *)p->_cnt); \
} \
} \
} \
return dropped; \
#define _Z_POINTER_DEFINE(name, type) \
typedef struct { \
type##_t *ptr; \
_z_atomic(unsigned int) * _cnt; \
} name##_sptr_t; \
static inline name##_sptr_t name##_sptr_new(type##_t val) { \
name##_sptr_t p; \
p.ptr = (type##_t *)z_malloc(sizeof(type##_t)); \
if (p.ptr != NULL) { \
p._cnt = (_z_atomic(unsigned int) *)z_malloc(sizeof(_z_atomic(unsigned int) *)); \
if (p._cnt != NULL) { \
*p.ptr = val; \
_z_atomic_store_explicit(p._cnt, 1, _z_memory_order_relaxed); \
} else { \
z_free(p.ptr); \
} \
} \
return p; \
} \
static inline name##_sptr_t name##_sptr_clone(name##_sptr_t *p) { \
name##_sptr_t c; \
c._cnt = p->_cnt; \
c.ptr = p->ptr; \
_z_atomic_fetch_add_explicit(p->_cnt, 1, _z_memory_order_relaxed); \
return c; \
} \
static inline name##_sptr_t *name##_sptr_clone_as_ptr(name##_sptr_t *p) { \
name##_sptr_t *c = (name##_sptr_t *)z_malloc(sizeof(name##_sptr_t)); \
if (c != NULL) { \
c->_cnt = p->_cnt; \
c->ptr = p->ptr; \
_z_atomic_fetch_add_explicit(p->_cnt, 1, _z_memory_order_relaxed); \
} \
return c; \
} \
static inline _Bool name##_sptr_eq(const name##_sptr_t *left, const name##_sptr_t *right) { \
return (left->ptr == right->ptr); \
} \
static inline _Bool name##_sptr_drop(name##_sptr_t *p) { \
_Bool dropped = false; \
if (p->_cnt != NULL) { \
unsigned int c = _z_atomic_fetch_sub_explicit(p->_cnt, 1, _z_memory_order_release); \
dropped = c == 1; \
if (dropped == true) { \
atomic_thread_fence(_z_memory_order_acquire); \
if (p->ptr != NULL) { \
type##_clear(p->ptr); \
z_free(p->ptr); \
z_free((void *)p->_cnt); \
} \
} \
} \
return dropped; \
}
#else
/*------------------ Internal Array Macros ------------------*/
Expand Down

0 comments on commit 342fcce

Please sign in to comment.