diff --git a/include/zenoh-pico/collections/lru_cache.h b/include/zenoh-pico/collections/lru_cache.h index 9fb783b8e..c0f5b967a 100644 --- a/include/zenoh-pico/collections/lru_cache.h +++ b/include/zenoh-pico/collections/lru_cache.h @@ -18,6 +18,7 @@ #include #include +#include "zenoh-pico/collections/element.h" #include "zenoh-pico/utils/result.h" #ifdef __cplusplus @@ -45,8 +46,8 @@ typedef struct _z_lru_cache_t { _z_lru_cache_t _z_lru_cache_init(size_t capacity); void *_z_lru_cache_get(_z_lru_cache_t *cache, void *value, _z_lru_val_cmp_f compare); z_result_t _z_lru_cache_insert(_z_lru_cache_t *cache, void *value, size_t value_size, _z_lru_val_cmp_f compare); -void _z_lru_cache_clear(_z_lru_cache_t *cache); -void _z_lru_cache_delete(_z_lru_cache_t *cache); +void _z_lru_cache_clear(_z_lru_cache_t *cache, z_element_clear_f clear); +void _z_lru_cache_delete(_z_lru_cache_t *cache, z_element_clear_f clear); #define _Z_LRU_CACHE_DEFINE(name, type, compare_f) \ typedef _z_lru_cache_t name##_lru_cache_t; \ @@ -57,8 +58,12 @@ void _z_lru_cache_delete(_z_lru_cache_t *cache); static inline z_result_t name##_lru_cache_insert(name##_lru_cache_t *cache, type *val) { \ return _z_lru_cache_insert(cache, (void *)val, sizeof(type), compare_f); \ } \ - static inline void name##_lru_cache_clear(name##_lru_cache_t *cache) { _z_lru_cache_clear(cache); } \ - static inline void name##_lru_cache_delete(name##_lru_cache_t *cache) { _z_lru_cache_delete(cache); } + static inline void name##_lru_cache_clear(name##_lru_cache_t *cache) { \ + _z_lru_cache_clear(cache, name##_elem_clear); \ + } \ + static inline void name##_lru_cache_delete(name##_lru_cache_t *cache) { \ + _z_lru_cache_delete(cache, name##_elem_clear); \ + } #ifdef __cplusplus } diff --git a/include/zenoh-pico/collections/vec.h b/include/zenoh-pico/collections/vec.h index a9277d423..17cc759df 100644 --- a/include/zenoh-pico/collections/vec.h +++ b/include/zenoh-pico/collections/vec.h @@ -85,12 +85,12 @@ typedef struct { } _z_svec_t; static inline _z_svec_t _z_svec_null(void) { return (_z_svec_t){0}; } -static inline _z_svec_t _z_svec_alias(const _z_svec_t *src) { +static inline _z_svec_t _z_svec_alias(const _z_svec_t *src, bool ownership) { _z_svec_t ret; ret._capacity = src->_capacity; ret._len = src->_len; ret._val = src->_val; - ret._aliased = true; + ret._aliased = !ownership; return ret; } static inline _z_svec_t _z_svec_alias_element(void *element) { @@ -151,7 +151,12 @@ void _z_svec_release(_z_svec_t *v); static inline z_result_t name##_svec_copy(name##_svec_t *dst, const name##_svec_t *src, bool use_elem_f) { \ return _z_svec_copy(dst, src, name##_elem_copy, sizeof(type), use_elem_f); \ } \ - static inline name##_svec_t name##_svec_alias(const name##_svec_t *v) { return _z_svec_alias(v); } \ + static inline name##_svec_t name##_svec_alias(const name##_svec_t *v) { return _z_svec_alias(v, false); } \ + static inline name##_svec_t name##_svec_transfer(name##_svec_t *v) { \ + name##_svec_t ret = _z_svec_alias(v, true); \ + v->_aliased = true; \ + return ret; \ + } \ static inline name##_svec_t name##_svec_alias_element(type *e) { return _z_svec_alias_element((void *)e); } \ static inline void name##_svec_move(name##_svec_t *dst, name##_svec_t *src) { _z_svec_move(dst, src); } \ static inline void name##_svec_reset(name##_svec_t *v) { _z_svec_reset(v, name##_elem_clear, sizeof(type)); } \ diff --git a/include/zenoh-pico/session/queryable.h b/include/zenoh-pico/session/queryable.h index 019ce6e1e..a9cdf4899 100644 --- a/include/zenoh-pico/session/queryable.h +++ b/include/zenoh-pico/session/queryable.h @@ -42,12 +42,15 @@ typedef struct { void _z_queryable_cache_invalidate(_z_session_t *zn); int _z_queryable_cache_data_compare(const void *first, const void *second); +void _z_queryable_cache_data_clear(_z_queryable_cache_data_t *val); #if Z_FEATURE_QUERYABLE == 1 #define _Z_QUERYABLE_COMPLETE_DEFAULT false #define _Z_QUERYABLE_DISTANCE_DEFAULT 0 #if Z_FEATURE_RX_CACHE == 1 +_Z_ELEM_DEFINE(_z_queryable, _z_queryable_cache_data_t, _z_noop_size, _z_queryable_cache_data_clear, _z_noop_copy, + _z_noop_move) _Z_LRU_CACHE_DEFINE(_z_queryable, _z_queryable_cache_data_t, _z_queryable_cache_data_compare) #endif diff --git a/include/zenoh-pico/session/subscription.h b/include/zenoh-pico/session/subscription.h index 07c037992..06daf70c8 100644 --- a/include/zenoh-pico/session/subscription.h +++ b/include/zenoh-pico/session/subscription.h @@ -45,6 +45,7 @@ typedef struct { void _z_subscription_cache_invalidate(_z_session_t *zn); int _z_subscription_cache_data_compare(const void *first, const void *second); +void _z_subscription_cache_data_clear(_z_subscription_cache_data_t *val); /*------------------ Subscription ------------------*/ z_result_t _z_trigger_subscriptions_put(_z_session_t *zn, _z_keyexpr_t *keyexpr, _z_bytes_t *payload, @@ -63,6 +64,8 @@ z_result_t _z_trigger_liveliness_subscriptions_undeclare(_z_session_t *zn, _z_ke #if Z_FEATURE_SUBSCRIPTION == 1 #if Z_FEATURE_RX_CACHE == 1 +_Z_ELEM_DEFINE(_z_subscription, _z_subscription_cache_data_t, _z_noop_size, _z_subscription_cache_data_clear, + _z_noop_copy, _z_noop_move) _Z_LRU_CACHE_DEFINE(_z_subscription, _z_subscription_cache_data_t, _z_subscription_cache_data_compare) #endif diff --git a/src/collections/lru_cache.c b/src/collections/lru_cache.c index c306c3d6d..2bb9f184b 100644 --- a/src/collections/lru_cache.c +++ b/src/collections/lru_cache.c @@ -102,6 +102,18 @@ static void _z_lru_cache_update_list(_z_lru_cache_t *cache, _z_lru_cache_node_t _z_lru_cache_insert_list_node(cache, node); } +static void _z_lru_cache_clear_list(_z_lru_cache_t *cache, z_element_clear_f clear) { + _z_lru_cache_node_data_t *node = cache->head; + while (node != NULL) { + _z_lru_cache_node_t *tmp = node; + _z_lru_cache_node_data_t *node_data = _z_lru_cache_node_data(node); + void *node_value = _z_lru_cache_node_value(node); + node = node_data->next; + clear(node_value); + z_free(tmp); + } +} + // Sorted list function static _z_lru_cache_node_t *_z_lru_cache_search_slist(_z_lru_cache_t *cache, void *value, _z_lru_val_cmp_f compare, size_t *idx) { @@ -223,32 +235,21 @@ z_result_t _z_lru_cache_insert(_z_lru_cache_t *cache, void *value, size_t value_ return _Z_RES_OK; } -void _z_lru_cache_clear(_z_lru_cache_t *cache) { +void _z_lru_cache_clear(_z_lru_cache_t *cache, z_element_clear_f clear) { // Reset slist if (cache->slist != NULL) { memset(cache->slist, 0, cache->capacity * sizeof(void *)); } - // Remove nodes - _z_lru_cache_node_data_t *node = cache->head; - while (node != NULL) { - _z_lru_cache_node_t *tmp = node; - _z_lru_cache_node_data_t *node_data = _z_lru_cache_node_data(node); - node = node_data->next; - z_free(tmp); - } + // Clear list + _z_lru_cache_clear_list(cache, clear); + // Reset cacge cache->len = 0; cache->head = NULL; cache->tail = NULL; } -void _z_lru_cache_delete(_z_lru_cache_t *cache) { - _z_lru_cache_node_data_t *node = cache->head; +void _z_lru_cache_delete(_z_lru_cache_t *cache, z_element_clear_f clear) { + _z_lru_cache_clear(cache, clear); z_free(cache->slist); - // Parse list - while (node != NULL) { - _z_lru_cache_node_t *tmp = node; - _z_lru_cache_node_data_t *node_data = _z_lru_cache_node_data(node); - node = node_data->next; - z_free(tmp); - } + cache->slist = NULL; } diff --git a/src/protocol/definitions/transport.c b/src/protocol/definitions/transport.c index 7bdfd9d85..3cb0d57ae 100644 --- a/src/protocol/definitions/transport.c +++ b/src/protocol/definitions/transport.c @@ -43,6 +43,8 @@ void _z_t_msg_keep_alive_clear(_z_t_msg_keep_alive_t *msg) { (void)(msg); } void _z_t_msg_frame_clear(_z_t_msg_frame_t *msg) { if (!msg->_messages._aliased) { _z_network_message_svec_clear(&msg->_messages); + } else { + _z_network_message_svec_reset(&msg->_messages); } } diff --git a/src/session/queryable.c b/src/session/queryable.c index 1244bd83f..e825e2333 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -51,6 +51,12 @@ int _z_queryable_cache_data_compare(const void *first, const void *second) { _z_queryable_cache_data_t *second_data = (_z_queryable_cache_data_t *)second; return _z_keyexpr_compare(&first_data->ke_in, &second_data->ke_in); } + +void _z_queryable_cache_data_clear(_z_queryable_cache_data_t *val) { + _z_queryable_infos_svec_clear(&val->infos); + _z_keyexpr_clear(&val->ke_in); + _z_keyexpr_clear(&val->ke_out); +} #endif // Z_FEATURE_RX_CACHE == 1 bool _z_session_queryable_eq(const _z_session_queryable_t *one, const _z_session_queryable_t *two) { @@ -177,7 +183,7 @@ static z_result_t _z_session_queryable_get_infos(_z_session_t *zn, _z_queryable_ #if Z_FEATURE_RX_CACHE == 1 // Update cache _z_queryable_cache_data_t cache_storage = { - .infos = _z_queryable_infos_svec_alias(&infos->infos), + .infos = _z_queryable_infos_svec_transfer(&infos->infos), .ke_in = _z_keyexpr_duplicate(&infos->ke_in), .ke_out = _z_keyexpr_duplicate(&infos->ke_out), .qle_nb = infos->qle_nb, diff --git a/src/session/rx.c b/src/session/rx.c index da0c1ddef..6e677ce10 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -89,12 +89,15 @@ z_result_t _z_handle_network_message(_z_session_rc_t *zsrc, _z_zenoh_message_t * _z_interest_process_declare_final(zn, decl->_interest_id); } break; } + _z_n_msg_declare_clear(&msg->_body._declare); } break; + case _Z_N_PUSH: { _Z_DEBUG("Handling _Z_N_PUSH"); _z_n_msg_push_t *push = &msg->_body._push; ret = _z_trigger_push(zn, push, msg->_reliability); } break; + case _Z_N_REQUEST: { _Z_DEBUG("Handling _Z_N_REQUEST"); _z_n_msg_request_t *req = &msg->_body._request; @@ -132,6 +135,7 @@ z_result_t _z_handle_network_message(_z_session_rc_t *zsrc, _z_zenoh_message_t * } break; } } break; + case _Z_N_RESPONSE: { _Z_DEBUG("Handling _Z_N_RESPONSE"); _z_n_msg_response_t *response = &msg->_body._response; @@ -146,9 +150,11 @@ z_result_t _z_handle_network_message(_z_session_rc_t *zsrc, _z_zenoh_message_t * } break; } } break; + case _Z_N_RESPONSE_FINAL: { _Z_DEBUG("Handling _Z_N_RESPONSE_FINAL"); ret = _z_trigger_reply_final(zn, &msg->_body._response_final); + _z_n_msg_response_final_clear(&msg->_body._response_final); } break; case _Z_N_INTEREST: { @@ -162,8 +168,13 @@ z_result_t _z_handle_network_message(_z_session_rc_t *zsrc, _z_zenoh_message_t * } else { _z_interest_process_interest_final(zn, interest->_interest._id); } - } + _z_n_msg_interest_clear(&msg->_body._interest); + } break; + + default: + _Z_ERROR("Unknown network message ID"); + _z_n_msg_clear(msg); + break; } - _z_msg_clear(msg); return ret; } diff --git a/src/session/subscription.c b/src/session/subscription.c index 9cdffd6b6..10aa79b36 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -52,6 +52,12 @@ int _z_subscription_cache_data_compare(const void *first, const void *second) { _z_subscription_cache_data_t *second_data = (_z_subscription_cache_data_t *)second; return _z_keyexpr_compare(&first_data->ke_in, &second_data->ke_in); } + +void _z_subscription_cache_data_clear(_z_subscription_cache_data_t *val) { + _z_subscription_infos_svec_clear(&val->infos); + _z_keyexpr_clear(&val->ke_in); + _z_keyexpr_clear(&val->ke_out); +} #endif // Z_FEATURE_RX_CACHE == 1 bool _z_subscription_eq(const _z_subscription_t *other, const _z_subscription_t *this_) { @@ -221,7 +227,7 @@ static z_result_t _z_subscription_get_infos(_z_session_t *zn, _z_subscriber_kind #if Z_FEATURE_RX_CACHE == 1 // Update cache, takes ownership of the data _z_subscription_cache_data_t cache_storage = { - .infos = _z_subscription_infos_svec_alias(&infos->infos), + .infos = _z_subscription_infos_svec_transfer(&infos->infos), .ke_in = _z_keyexpr_duplicate(&infos->ke_in), .ke_out = _z_keyexpr_duplicate(&infos->ke_out), .sub_nb = infos->sub_nb, diff --git a/src/transport/multicast/read.c b/src/transport/multicast/read.c index bea0b82f3..cb521b258 100644 --- a/src/transport/multicast/read.c +++ b/src/transport/multicast/read.c @@ -112,9 +112,7 @@ void *_zp_multicast_read_task(void *ztm_arg) { if (ret == _Z_RES_OK) { ret = _z_multicast_handle_transport_message(ztm, &t_msg, &addr); - if (ret == _Z_RES_OK) { - _z_t_msg_clear(&t_msg); - } else { + if (ret != _Z_RES_OK) { _Z_ERROR("Dropping message due to processing error: %d", ret); continue; } diff --git a/src/transport/multicast/rx.c b/src/transport/multicast/rx.c index fb0205b0f..ea67ac9ec 100644 --- a/src/transport/multicast/rx.c +++ b/src/transport/multicast/rx.c @@ -126,6 +126,7 @@ z_result_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _Z_DEBUG("Received _Z_FRAME message"); if (entry == NULL) { _Z_INFO("Dropping _Z_FRAME from unknown peer"); + _z_t_msg_frame_clear(&t_msg->_body._frame); break; } // Note that we receive data from peer @@ -145,6 +146,7 @@ z_result_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_wbuf_clear(&entry->_dbuf_reliable); #endif _Z_INFO("Reliable message dropped because it is out of order"); + _z_t_msg_frame_clear(&t_msg->_body._frame); break; } } else { @@ -158,6 +160,7 @@ z_result_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_wbuf_clear(&entry->_dbuf_best_effort); #endif _Z_INFO("Best effort message dropped because it is out of order"); + _z_t_msg_frame_clear(&t_msg->_body._frame); break; } } @@ -181,6 +184,7 @@ z_result_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, #if Z_FEATURE_FRAGMENTATION == 1 if (entry == NULL) { _Z_INFO("Dropping Z_FRAGMENT from unknown peer"); + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } // Note that we receive data from the peer @@ -207,6 +211,7 @@ z_result_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_wbuf_clear(&entry->_dbuf_reliable); entry->_state_reliable = _Z_DBUF_STATE_NULL; _Z_INFO("Reliable message dropped because it is out of order"); + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } } else { @@ -223,6 +228,7 @@ z_result_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_wbuf_clear(&entry->_dbuf_best_effort); entry->_state_best_effort = _Z_DBUF_STATE_NULL; _Z_INFO("Best effort message dropped because it is out of order"); + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } } @@ -230,6 +236,7 @@ z_result_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_wbuf_clear(dbuf); *dbuf_state = _Z_DBUF_STATE_NULL; _Z_INFO("Defragmentation buffer dropped because non-consecutive fragments received"); + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } // Handle fragment markers @@ -238,10 +245,12 @@ z_result_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_wbuf_reset(dbuf); } else if (_z_wbuf_len(dbuf) == 0) { _Z_INFO("First fragment received without the first marker"); + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } if (t_msg->_body._fragment.drop) { _z_wbuf_reset(dbuf); + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } } @@ -251,6 +260,7 @@ z_result_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, if (_z_wbuf_capacity(dbuf) != Z_FRAG_MAX_SIZE) { _Z_ERROR("Not enough memory to allocate peer defragmentation buffer"); ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } *dbuf_state = _Z_DBUF_STATE_INIT; @@ -273,6 +283,7 @@ z_result_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _Z_INFO("Fragment dropped because defragmentation buffer has overflown"); _z_wbuf_clear(dbuf); *dbuf_state = _Z_DBUF_STATE_NULL; + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } // Convert the defragmentation buffer into a decoding buffer @@ -282,6 +293,7 @@ z_result_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_wbuf_clear(dbuf); *dbuf_state = _Z_DBUF_STATE_NULL; ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } // Decode message @@ -305,32 +317,37 @@ z_result_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, #else _Z_INFO("Fragment dropped because fragmentation feature is deactivated"); #endif + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } case _Z_MID_T_KEEP_ALIVE: { _Z_DEBUG("Received _Z_KEEP_ALIVE message"); if (entry == NULL) { + _z_t_msg_keep_alive_clear(&t_msg->_body._keep_alive); break; } entry->_received = true; - + _z_t_msg_keep_alive_clear(&t_msg->_body._keep_alive); break; } case _Z_MID_T_INIT: { // Do nothing, multicast transports are not expected to handle INIT messages + _z_t_msg_init_clear(&t_msg->_body._init); break; } case _Z_MID_T_OPEN: { // Do nothing, multicast transports are not expected to handle OPEN messages + _z_t_msg_open_clear(&t_msg->_body._open); break; } case _Z_MID_T_JOIN: { _Z_DEBUG("Received _Z_JOIN message"); if (t_msg->_body._join._version != Z_PROTO_VERSION) { + _z_t_msg_join_clear(&t_msg->_body._join); break; } @@ -383,6 +400,7 @@ z_result_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, (t_msg->_body._join._batch_size != Z_BATCH_MULTICAST_SIZE)) { _z_transport_peer_entry_list_drop_filter(ztm->_peers, _z_transport_peer_entry_eq, entry); // TODO: cleanup here should also be done on mappings/subs/etc... + _z_t_msg_join_clear(&t_msg->_body._join); break; } @@ -393,6 +411,7 @@ z_result_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, // Update lease time (set as ms during) entry->_lease = t_msg->_body._join._lease; } + _z_t_msg_join_clear(&t_msg->_body._join); break; } @@ -400,15 +419,17 @@ z_result_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _Z_INFO("Closing session as requested by the remote peer"); if (entry == NULL) { + _z_t_msg_close_clear(&t_msg->_body._close); break; } ztm->_peers = _z_transport_peer_entry_list_drop_filter(ztm->_peers, _z_transport_peer_entry_eq, entry); - + _z_t_msg_close_clear(&t_msg->_body._close); break; } default: { _Z_ERROR("Unknown session message ID"); + _z_t_msg_clear(t_msg); break; } } diff --git a/src/transport/unicast/read.c b/src/transport/unicast/read.c index 5851f3146..1888ac181 100644 --- a/src/transport/unicast/read.c +++ b/src/transport/unicast/read.c @@ -107,9 +107,7 @@ void *_zp_unicast_read_task(void *ztu_arg) { if (ret == _Z_RES_OK) { ret = _z_unicast_handle_transport_message(ztu, &t_msg); - if (ret == _Z_RES_OK) { - _z_t_msg_clear(&t_msg); - } else { + if (ret != _Z_RES_OK) { if (ret != _Z_ERR_CONNECTION_CLOSED) { _Z_ERROR("Connection closed due to message processing error: %d", ret); } diff --git a/src/transport/unicast/rx.c b/src/transport/unicast/rx.c index 3f374f5af..cb8401de1 100644 --- a/src/transport/unicast/rx.c +++ b/src/transport/unicast/rx.c @@ -110,6 +110,7 @@ z_result_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_t ztu->_state_reliable = _Z_DBUF_STATE_NULL; #endif _Z_INFO("Reliable message dropped because it is out of order"); + _z_t_msg_frame_clear(&t_msg->_body._frame); break; } } else { @@ -122,6 +123,7 @@ z_result_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_t ztu->_state_best_effort = _Z_DBUF_STATE_NULL; #endif _Z_INFO("Best effort message dropped because it is out of order"); + _z_t_msg_frame_clear(&t_msg->_body._frame); break; } } @@ -161,6 +163,7 @@ z_result_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_t _z_wbuf_clear(&ztu->_dbuf_reliable); ztu->_state_reliable = _Z_DBUF_STATE_NULL; _Z_INFO("Reliable message dropped because it is out of order"); + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } } else { @@ -176,6 +179,7 @@ z_result_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_t _z_wbuf_clear(&ztu->_dbuf_best_effort); ztu->_state_best_effort = _Z_DBUF_STATE_NULL; _Z_INFO("Best effort message dropped because it is out of order"); + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } } @@ -184,6 +188,7 @@ z_result_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_t _z_wbuf_clear(dbuf); *dbuf_state = _Z_DBUF_STATE_NULL; _Z_INFO("Defragmentation buffer dropped because non-consecutive fragments received"); + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } // Handle fragment markers @@ -192,10 +197,12 @@ z_result_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_t _z_wbuf_reset(dbuf); } else if (_z_wbuf_len(dbuf) == 0) { _Z_INFO("First fragment received without the start marker"); + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } if (t_msg->_body._fragment.drop) { _z_wbuf_reset(dbuf); + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } } @@ -205,6 +212,7 @@ z_result_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_t if (_z_wbuf_capacity(dbuf) != Z_FRAG_MAX_SIZE) { _Z_ERROR("Not enough memory to allocate transport defragmentation buffer"); ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } *dbuf_state = _Z_DBUF_STATE_INIT; @@ -227,6 +235,7 @@ z_result_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_t _Z_INFO("Fragment dropped because defragmentation buffer has overflown"); _z_wbuf_clear(dbuf); *dbuf_state = _Z_DBUF_STATE_NULL; + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } // Convert the defragmentation buffer into a decoding buffer @@ -236,6 +245,7 @@ z_result_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_t _z_wbuf_clear(dbuf); *dbuf_state = _Z_DBUF_STATE_NULL; ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } // Decode message @@ -257,32 +267,38 @@ z_result_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_t #else _Z_INFO("Fragment dropped because fragmentation feature is deactivated"); #endif + _z_t_msg_fragment_clear(&t_msg->_body._fragment); break; } case _Z_MID_T_KEEP_ALIVE: { _Z_DEBUG("Received Z_KEEP_ALIVE message"); + _z_t_msg_keep_alive_clear(&t_msg->_body._keep_alive); break; } case _Z_MID_T_INIT: { // Do nothing, zenoh clients are not expected to handle accept messages on established sessions + _z_t_msg_init_clear(&t_msg->_body._init); break; } case _Z_MID_T_OPEN: { // Do nothing, zenoh clients are not expected to handle accept messages on established sessions + _z_t_msg_open_clear(&t_msg->_body._open); break; } case _Z_MID_T_CLOSE: { _Z_INFO("Closing session as requested by the remote peer"); ret = _Z_ERR_CONNECTION_CLOSED; + _z_t_msg_close_clear(&t_msg->_body._close); break; } default: { - _Z_ERROR("Unknown session message ID"); + _Z_ERROR("Unknown transport message ID"); + _z_t_msg_clear(t_msg); break; } } diff --git a/tests/z_lru_cache_test.c b/tests/z_lru_cache_test.c index 329ec185a..2dbff54e7 100644 --- a/tests/z_lru_cache_test.c +++ b/tests/z_lru_cache_test.c @@ -41,6 +41,8 @@ int _dummy_compare(const void *first, const void *second) { return -1; } +static inline void _dummy_elem_clear(void *e) { _z_noop_clear((_dummy_t *)e); } + _Z_LRU_CACHE_DEFINE(_dummy, _dummy_t, _dummy_compare) void test_lru_init(void) {