From 1c37763975af0a6c89a9089aa606155227e67d41 Mon Sep 17 00:00:00 2001 From: "Enrico Loparco (eloparco)" Date: Mon, 5 Apr 2021 12:10:45 +0200 Subject: [HICN-555, HICN-556] Add loop utility function and fix memory bug in bitmap A helper function is added to the loop class to allow loop exit after empty event queue. This function is used when running tests. Signed-off-by: Enrico Loparco (eloparco) Change-Id: I7bbab32a7d7f53365439f0fba2b9a030d34ac823 --- hicn-light/src/hicn/base/bitmap.h | 17 ++++++++++++++--- hicn-light/src/hicn/base/loop.c | 9 +++++++-- hicn-light/src/hicn/base/loop.h | 10 +++++++++- hicn-light/src/hicn/base/test/test-loop.cc | 4 ++-- 4 files changed, 32 insertions(+), 8 deletions(-) (limited to 'hicn-light') diff --git a/hicn-light/src/hicn/base/bitmap.h b/hicn-light/src/hicn/base/bitmap.h index 1159806c8..908b28d87 100644 --- a/hicn-light/src/hicn/base/bitmap.h +++ b/hicn-light/src/hicn/base/bitmap.h @@ -100,16 +100,27 @@ bitmap_get(const bitmap_t * bitmap, off_t i) * * @return bool */ +#define bitmap_set(bitmap, i) \ + _bitmap_set((bitmap_t**)&bitmap, i) + +/* + * @brief Returns whether the i-th bit is unset (equal to 0) in a bitmap (helper). + * + * @param[in] bitmap The bitmap to access. + * @param[in] i The bit position. + * + * @return bool + */ static inline int -bitmap_set(bitmap_t * bitmap, off_t i) +_bitmap_set(bitmap_t ** bitmap, off_t i) { - if (bitmap_ensure_pos(&bitmap, i) < 0) + if (bitmap_ensure_pos(bitmap, i) < 0) return -1; size_t offset = i / BITMAP_WIDTH(bitmap); size_t pos = i % BITMAP_WIDTH(bitmap); size_t shift = BITMAP_WIDTH(bitmap) - pos - 1; - bitmap[offset] |= 1ul << shift; + (*bitmap)[offset] |= 1ul << shift; return 0; } diff --git a/hicn-light/src/hicn/base/loop.c b/hicn-light/src/hicn/base/loop.c index 8588b538d..d27e81f5b 100644 --- a/hicn-light/src/hicn/base/loop.c +++ b/hicn-light/src/hicn/base/loop.c @@ -97,12 +97,17 @@ void loop_free(loop_t *loop) { free(loop); } -int loop_dispatch(loop_t *loop) +int _loop_dispatch(loop_t *loop, int flags) { - event_base_loop(loop->event_base, EVLOOP_NO_EXIT_ON_EMPTY); + event_base_loop(loop->event_base, flags); return 0; } +int loop_dispatch(loop_t *loop) +{ + return _loop_dispatch(loop, EVLOOP_NO_EXIT_ON_EMPTY); +} + int loop_undispatch(loop_t *loop) { return 0; } void loop_break(loop_t *loop) { event_base_loopbreak(loop->event_base); } diff --git a/hicn-light/src/hicn/base/loop.h b/hicn-light/src/hicn/base/loop.h index 697021175..544d73cd7 100644 --- a/hicn-light/src/hicn/base/loop.h +++ b/hicn-light/src/hicn/base/loop.h @@ -52,7 +52,15 @@ loop_t *loop_create(); void loop_free(loop_t *loop); /** - * \brief Runs the loop instance to process events + * \brief Runs the loop instance to process events (helper). + * \param [in] loop - Pointer to the loop instance + * \param [in] flags - Loop mode: EVLOOP_ONCE, EVLOOP_NONBLOCK or EVLOOP_NO_EXIT_ON_EMPTY + * \return 0 if successful, -1 otherwise + */ +int _loop_dispatch(loop_t *loop, int flags); + +/** + * \brief Runs the loop instance to process events. * \param [in] loop - Pointer to the loop instance * \return 0 if successful, -1 otherwise */ diff --git a/hicn-light/src/hicn/base/test/test-loop.cc b/hicn-light/src/hicn/base/test/test-loop.cc index c86ccd5e6..d50b2472a 100644 --- a/hicn-light/src/hicn/base/test/test-loop.cc +++ b/hicn-light/src/hicn/base/test/test-loop.cc @@ -229,7 +229,7 @@ TEST_F(LoopTest, TimerCreateAndCancel) ret = loop_timer_register(timer2, timer_tick_ / 2); EXPECT_TRUE(ret >= 0); - loop_dispatch(loop_); + _loop_dispatch(loop_, 0); loop_undispatch(loop_); @@ -266,7 +266,7 @@ TEST_F(LoopTest, LoopDispatch) EXPECT_TRUE(ret >= 0); // Start event dispatching - loop_dispatch(loop_); + _loop_dispatch(loop_, 0); // Stop dispatching loop_undispatch(loop_); -- cgit 1.2.3-korg