aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light
diff options
context:
space:
mode:
Diffstat (limited to 'hicn-light')
-rw-r--r--hicn-light/src/hicn/base/pool.c5
-rw-r--r--hicn-light/src/hicn/base/test/test-pool.cc40
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);