Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions include/boost/pool/simple_segregated_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,19 @@ void * simple_segregated_storage<SizeType>::try_malloc_n(
void * & start, size_type n, const size_type partition_size)
{
void * iter = nextof(start);
if (n == 1)
{
void * next = nextof(iter);
if (next != static_cast<char *>(iter) + partition_size)
{
start = iter;
return 0;
}
else
{
return iter;
}
}
while (--n != 0)
{
void * next = nextof(iter);
Expand Down
28 changes: 28 additions & 0 deletions test/test_pool_alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,34 @@ void test_mem_usage()
// This will clean up the memory leak from (*B*)
}

{
pool_type pool(sizeof(int), 2);
void * ptr_0 = pool.malloc();
void * ptr_1 = pool.malloc();
void * ptr_2 = pool.malloc();
void * ptr_3 = pool.malloc();
pool.ordered_free(ptr_2);
pool.ordered_free(ptr_3);
BOOST_TEST(pool.release_memory());
pool.ordered_free(ptr_0);
pool.ordered_free(ptr_1);
BOOST_TEST(pool.release_memory());
}

{
pool_type pool(sizeof(int), 2);
void * ptr_0 = pool.malloc();
void * ptr_1 = pool.malloc();
void * ptr_2 = pool.malloc();
void * ptr_3 = pool.malloc();
pool.ordered_free(ptr_0);
pool.ordered_free(ptr_1);
BOOST_TEST(pool.release_memory());
pool.ordered_free(ptr_2);
pool.ordered_free(ptr_3);
BOOST_TEST(pool.release_memory());
}

BOOST_TEST(track_alloc::ok());
}

Expand Down
76 changes: 76 additions & 0 deletions test/test_simple_seg_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,82 @@ int main()
BOOST_TEST(nchunks == 3);
}

{
char* const pc = track_allocator::malloc(4 * partition_sz);
test_simp_seg_store tstore;
tstore.add_ordered_block(pc, 4 * partition_sz, partition_sz);

void* pvret = tstore.malloc_n(1, partition_sz);
BOOST_TEST(pvret == pc);

// There should still be two contiguous
// and one non-contiguous chunk left
std::size_t nchunks = 0;
while(!tstore.empty())
{
tstore.malloc();
++nchunks;
}
BOOST_TEST(nchunks == 3);
}

{
char* const pc = track_allocator::malloc(4 * partition_sz);
test_simp_seg_store tstore;
tstore.add_ordered_block(pc, 4 * partition_sz, partition_sz);

void* pvret = tstore.malloc_n(2, partition_sz);
BOOST_TEST(pvret == pc);

// There should still be two contiguous
// and one non-contiguous chunk left
std::size_t nchunks = 0;
while(!tstore.empty())
{
tstore.malloc();
++nchunks;
}
BOOST_TEST(nchunks == 2);
}

{
char* const pc = track_allocator::malloc(4 * partition_sz);
test_simp_seg_store tstore;
tstore.add_ordered_block(pc, 4 * partition_sz, partition_sz);

void* pvret = tstore.malloc_n(1, 2 * partition_sz);
BOOST_TEST(pvret == 0);

// There should still be two contiguous
// and one non-contiguous chunk left
std::size_t nchunks = 0;
while(!tstore.empty())
{
tstore.malloc();
++nchunks;
}
BOOST_TEST(nchunks == 4);
}

{
char* const pc = track_allocator::malloc(4 * partition_sz);
test_simp_seg_store tstore;
tstore.add_ordered_block(pc, 4 * partition_sz, partition_sz);

void* pvret = tstore.malloc_n(2, 2 * partition_sz);
BOOST_TEST(pvret == 0);

// There should still be two contiguous
// and one non-contiguous chunk left
std::size_t nchunks = 0;
while(!tstore.empty())
{
tstore.malloc();
++nchunks;
}
BOOST_TEST(nchunks == 4);
}

{
char* const pc = track_allocator::malloc(12 * partition_sz);
test_simp_seg_store tstore;
Expand Down