diff options
Diffstat (limited to 'hicn-light/src/hicn/base')
-rw-r--r-- | hicn-light/src/hicn/base/bitmap.h | 29 | ||||
-rw-r--r-- | hicn-light/src/hicn/base/loop.c | 9 | ||||
-rw-r--r-- | hicn-light/src/hicn/base/loop.h | 10 | ||||
-rw-r--r-- | hicn-light/src/hicn/base/pool.c | 5 | ||||
-rw-r--r-- | hicn-light/src/hicn/base/test/test-loop.cc | 4 | ||||
-rw-r--r-- | hicn-light/src/hicn/base/test/test-pool.cc | 40 | ||||
-rw-r--r-- | hicn-light/src/hicn/base/vector.h | 5 |
7 files changed, 83 insertions, 19 deletions
diff --git a/hicn-light/src/hicn/base/bitmap.h b/hicn-light/src/hicn/base/bitmap.h index 1159806c8..46d473ff8 100644 --- a/hicn-light/src/hicn/base/bitmap.h +++ b/hicn-light/src/hicn/base/bitmap.h @@ -66,6 +66,13 @@ bitmap_ensure_pos(bitmap_t ** bitmap, off_t pos) } /** + * @brief Returns the allocated size of a bitmap. + * + * @see listener_table_get_by_id + */ +#define bitmap_get_alloc_size(bitmap) vector_get_alloc_size(bitmap) + +/** * @brief Retrieve the state of the i-th bit in the bitmap. * * @param[in] bitmap The bitmap to access. @@ -76,6 +83,7 @@ int bitmap_get(const bitmap_t * bitmap, off_t i) { size_t offset = i / BITMAP_WIDTH(bitmap); + assert(offset < bitmap_get_alloc_size(bitmap)); size_t pos = i % BITMAP_WIDTH(bitmap); size_t shift = BITMAP_WIDTH(bitmap) - pos - 1; return (bitmap[offset] >> shift) & 1; @@ -100,16 +108,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; } @@ -186,8 +205,4 @@ END: #define bitmap_free(bitmap) vector_free(bitmap) -#ifdef WITH_TESTS -#define bitmap_get_alloc_size(bitmap) vector_get_alloc_size(bitmap) -#endif /* WITH_TESTS */ - #endif /* UTIL_BITMAP_H */ 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/pool.c b/hicn-light/src/hicn/base/pool.c index a7c50963d..662c1233c 100644 --- a/hicn-light/src/hicn/base/pool.c +++ b/hicn-light/src/hicn/base/pool.c @@ -154,7 +154,8 @@ _pool_put(void ** pool_ptr, void ** elt, size_t elt_size) pool_hdr_t * ph = pool_hdr(*pool_ptr); uint64_t l = vector_len(ph->free_indices); vector_ensure_pos(ph->free_indices, l); - ph->free_indices[l] = *elt - *pool_ptr; + off_t freed_id = (*elt - *pool_ptr) / elt_size; + ph->free_indices[l] = freed_id; vector_len(ph->free_indices)++; - bitmap_set(ph->free_bitmap, l); + bitmap_set(ph->free_bitmap, freed_id); } 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_); diff --git a/hicn-light/src/hicn/base/test/test-pool.cc b/hicn-light/src/hicn/base/test/test-pool.cc index e5df191ea..fd0d5988b 100644 --- a/hicn-light/src/hicn/base/test/test-pool.cc +++ b/hicn-light/src/hicn/base/test/test-pool.cc @@ -140,7 +140,6 @@ TEST_F(PoolTest, PoolAllocation) pool_free(pool); } -// XXX todo : check state after several get and put TEST_F(PoolTest, PoolPut) { pool_init(pool, DEFAULT_SIZE, 0); @@ -148,9 +147,7 @@ TEST_F(PoolTest, PoolPut) int* elt; pool_get(pool, elt); *elt = 10; - printf("2\n"); pool_put(pool, elt); - printf("3\n"); pool_free(pool); } @@ -169,6 +166,43 @@ TEST_F(PoolTest, PoolGetForceBitmapRealloc) pool_free(pool); } +TEST_F(PoolTest, PoolGetAfterReleasing) +{ + int *elt1 = NULL, *elt2 = NULL, *tmp = NULL; + pool_init(pool, DEFAULT_SIZE, 0); + + // If two elements are requested... + off_t id1 = pool_get(pool, elt1); + pool_get(pool, tmp); + + // ...and the first one is released... + pool_put(pool, elt1); + + // ...requesting a new one should return + // the first one (that was freed) + off_t id2 = pool_get(pool, elt2); + EXPECT_EQ(id1, id2); + EXPECT_EQ(elt1, elt2); + + pool_free(pool); +} + +TEST_F(PoolTest, PoolGetMultipleElementsAfterReleasing) +{ + const int N = 2; + int *elts[N]; + pool_init(pool, N, 0); + + for (int i = 0; i < N; i++) + pool_get(pool, elts[i]); + for (int i = 0; i < N; i++) + pool_put(pool, elts[i]); + for (int i = 0; i < N; i++) + pool_get(pool, elts[i]); + + pool_free(pool); +} + int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); diff --git a/hicn-light/src/hicn/base/vector.h b/hicn-light/src/hicn/base/vector.h index b369086fc..fd8401e6d 100644 --- a/hicn-light/src/hicn/base/vector.h +++ b/hicn-light/src/hicn/base/vector.h @@ -251,8 +251,9 @@ do { \ */ #define vector_len(vector) vector_hdr(vector)->cur_size -#ifdef WITH_TESTS +/** + * @brief Returns the allocated size of a vector. + */ #define vector_get_alloc_size(vector) vector_hdr(vector)->alloc_size -#endif /* WITH_TESTS */ #endif /* UTIL_VECTOR_H */ |