Skip to content

Commit a067109

Browse files
committed
feat: use elem_clone to avoid duplicating channel code
1 parent dedef05 commit a067109

File tree

1 file changed

+6
-106
lines changed

1 file changed

+6
-106
lines changed

include/zenoh-pico/api/handlers.h

+6-106
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#define _Z_CHANNEL_DEFINE_IMPL(handler_type, handler_name, handler_new_f_name, callback_type, callback_new_f, \
2828
collection_type, collection_new_f, collection_free_f, collection_push_f, \
2929
collection_pull_f, collection_try_pull_f, elem_owned_type, elem_loaned_type, \
30-
elem_copy_f, elem_drop_f) \
30+
elem_clone_f, elem_drop_f) \
3131
typedef struct { \
3232
collection_type *collection; \
3333
} handler_type; \
@@ -50,17 +50,8 @@
5050
_Z_ERROR("Out of memory"); \
5151
return; \
5252
} \
53-
if (elem == NULL) { \
54-
internal_elem->_val = NULL; \
55-
} else { \
56-
internal_elem->_val = (elem_loaned_type *)z_malloc(sizeof(elem_loaned_type)); \
57-
if (internal_elem->_val == NULL) { \
58-
z_free(internal_elem); \
59-
_Z_ERROR("Out of memory"); \
60-
return; \
61-
} \
62-
elem_copy_f(internal_elem->_val, (elem_loaned_type *)elem); \
63-
} \
53+
elem_clone_f(internal_elem, elem); \
54+
\
6455
int8_t ret = collection_push_f(internal_elem, context, _z_##handler_name##_elem_free); \
6556
if (ret != _Z_RES_OK) { \
6657
_Z_ERROR("%s failed: %i", #collection_push_f, ret); \
@@ -118,100 +109,9 @@
118109
/* collection_try_pull_f */ _z_##kind_name##_mt_try_pull, \
119110
/* elem_owned_type */ z_owned_##item_name##_t, \
120111
/* elem_loaned_type */ z_loaned_##item_name##_t, \
121-
/* elem_copy_f */ _z_##item_name##_copy, \
112+
/* elem_clone_f */ z_##item_name##_clone, \
122113
/* elem_drop_f */ z_##item_name##_drop)
123114

124-
#define _Z_CHANNEL_RC_DEFINE_IMPL(handler_type, handler_name, handler_new_f_name, callback_type, callback_new_f, \
125-
collection_type, collection_new_f, collection_free_f, collection_push_f, \
126-
collection_pull_f, collection_try_pull_f, elem_owned_type, elem_loaned_type, \
127-
elem_copy_f, elem_drop_f) \
128-
typedef struct { \
129-
collection_type *collection; \
130-
} handler_type; \
131-
\
132-
_Z_OWNED_TYPE_PTR(handler_type, handler_name) \
133-
_Z_LOANED_TYPE(handler_type, handler_name) \
134-
\
135-
static inline void _z_##handler_name##_elem_free(void **elem) { \
136-
elem_drop_f((elem_owned_type *)*elem); \
137-
z_free(*elem); \
138-
*elem = NULL; \
139-
} \
140-
static inline void _z_##handler_name##_elem_move(void *dst, void *src) { \
141-
memcpy(dst, src, sizeof(elem_owned_type)); \
142-
z_free(src); \
143-
} \
144-
static inline void _z_##handler_name##_send(const elem_loaned_type *elem, void *context) { \
145-
elem_owned_type *internal_elem = (elem_owned_type *)z_malloc(sizeof(elem_owned_type)); \
146-
if (internal_elem == NULL) { \
147-
_Z_ERROR("Out of memory"); \
148-
return; \
149-
} \
150-
if (elem == NULL) { \
151-
internal_elem->_rc.in = NULL; \
152-
} else { \
153-
elem_copy_f(&internal_elem->_rc, elem); \
154-
} \
155-
int8_t ret = collection_push_f(internal_elem, context, _z_##handler_name##_elem_free); \
156-
if (ret != _Z_RES_OK) { \
157-
_Z_ERROR("%s failed: %i", #collection_push_f, ret); \
158-
} \
159-
} \
160-
static inline void z_##handler_name##_recv(const z_loaned_##handler_name##_t *handler, elem_owned_type *elem) { \
161-
int8_t ret = collection_pull_f(elem, (collection_type *)handler->collection, _z_##handler_name##_elem_move); \
162-
if (ret != _Z_RES_OK) { \
163-
_Z_ERROR("%s failed: %i", #collection_pull_f, ret); \
164-
} \
165-
} \
166-
static inline void z_##handler_name##_try_recv(const z_loaned_##handler_name##_t *handler, \
167-
elem_owned_type *elem) { \
168-
int8_t ret = \
169-
collection_try_pull_f(elem, (collection_type *)handler->collection, _z_##handler_name##_elem_move); \
170-
if (ret != _Z_RES_OK) { \
171-
_Z_ERROR("%s failed: %i", #collection_try_pull_f, ret); \
172-
} \
173-
} \
174-
\
175-
static inline void _z_##handler_name##_free(handler_type **handler) { \
176-
handler_type *ptr = *handler; \
177-
if (ptr != NULL) { \
178-
collection_free_f(ptr->collection, _z_##handler_name##_elem_free); \
179-
z_free(ptr); \
180-
*handler = NULL; \
181-
} \
182-
} \
183-
static inline void _z_##handler_name##_copy(void *dst, const void *src) { \
184-
(void)(dst); \
185-
(void)(src); \
186-
} \
187-
\
188-
_Z_OWNED_FUNCTIONS_PTR_IMPL(handler_type, handler_name, _z_##handler_name##_copy, _z_##handler_name##_free) \
189-
\
190-
static inline int8_t handler_new_f_name(callback_type *callback, z_owned_##handler_name##_t *handler, \
191-
size_t capacity) { \
192-
handler->_val = (handler_type *)z_malloc(sizeof(handler_type)); \
193-
handler->_val->collection = collection_new_f(capacity); \
194-
callback_new_f(callback, _z_##handler_name##_send, NULL, handler->_val->collection); \
195-
return _Z_RES_OK; \
196-
}
197-
198-
#define _Z_CHANNEL_RC_DEFINE(item_name, kind_name) \
199-
_Z_CHANNEL_RC_DEFINE_IMPL(/* handler_type */ _z_##kind_name##_handler_##item_name##_t, \
200-
/* handler_name */ kind_name##_handler_##item_name, \
201-
/* handler_new_f_name */ z_##kind_name##_channel_##item_name##_new, \
202-
/* callback_type */ z_owned_closure_##item_name##_t, \
203-
/* callback_new_f */ z_closure_##item_name, \
204-
/* collection_type */ _z_##kind_name##_mt_t, \
205-
/* collection_new_f */ _z_##kind_name##_mt_new, \
206-
/* collection_free_f */ _z_##kind_name##_mt_free, \
207-
/* collection_push_f */ _z_##kind_name##_mt_push, \
208-
/* collection_pull_f */ _z_##kind_name##_mt_pull, \
209-
/* collection_try_pull_f */ _z_##kind_name##_mt_try_pull, \
210-
/* elem_owned_type */ z_owned_##item_name##_t, \
211-
/* elem_loaned_type */ z_loaned_##item_name##_t, \
212-
/* elem_copy_f */ _z_##item_name##_rc_copy, \
213-
/* elem_drop_f */ z_##item_name##_drop)
214-
215115
#define _Z_CHANNEL_DEFINE_DUMMY(item_name, kind_name) \
216116
typedef struct { \
217117
uint8_t _foo; \
@@ -239,12 +139,12 @@ _Z_CHANNEL_DEFINE(sample, fifo)
239139
// This macro defines:
240140
// z_ring_channel_query_new()
241141
// z_owned_ring_handler_query_t/z_loaned_ring_handler_query_t
242-
_Z_CHANNEL_RC_DEFINE(query, ring)
142+
_Z_CHANNEL_DEFINE(query, ring)
243143

244144
// This macro defines:
245145
// z_fifo_channel_query_new()
246146
// z_owned_fifo_handler_query_t/z_loaned_fifo_handler_query_t
247-
_Z_CHANNEL_RC_DEFINE(query, fifo)
147+
_Z_CHANNEL_DEFINE(query, fifo)
248148
#else // Z_FEATURE_QUERYABLE
249149
_Z_CHANNEL_DEFINE_DUMMY(query, ring)
250150
_Z_CHANNEL_DEFINE_DUMMY(query, fifo)

0 commit comments

Comments
 (0)