diff options
author | Enrico Loparco (eloparco) <eloparco@cisco.com> | 2021-04-03 00:03:57 +0200 |
---|---|---|
committer | Enrico Loparco (eloparco) <eloparco@cisco.com> | 2021-04-03 00:03:57 +0200 |
commit | 6ffbb5ed61733b8dbef39b1a9d437e899e9359d7 (patch) | |
tree | 3db1707fff8e11db5d2fb59c64cb4de92a57c721 | |
parent | 2fa1eaf5aecbd862e9eba897d989d2fd7adba71e (diff) |
[HICN-558] Fix bug in pool_put and add tests for several pool_get / pool_put
Signed-off-by: Enrico Loparco (eloparco) <eloparco@cisco.com>
Change-Id: Ie0ee6d17ab87586143ecf07e648d22b3af72bbcd
-rw-r--r-- | hicn-light/src/hicn/base/pool.c | 5 | ||||
-rw-r--r-- | hicn-light/src/hicn/base/test/test-pool.cc | 40 |
2 files changed, 40 insertions, 5 deletions
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-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); |