Search code examples
c++boostboost-pool

How to use boost::simple_segregated_storage?


I try to use the boost::simple_segregated_storage, but I can`t understand how to use it correctly. There are no any samples. I use it in next way:

boost::simple_segregated_storage<int> pStorage;

const int num_partitions = 100;
const int partition_sz = sizeof(int);
const int block_sz = partition_sz * num_partitions;
int block[block_sz] = {};

pStorage.segregate(block, block_sz, partition_sz);

int* pInt = (int*)pStorage.malloc(); // <-- Crashes here

But I received crash. What I do wrong and where mistake? How to use it in right way?


Solution

  • You should use pStorage.add_block(block, block_sz, partition_sz); instead of segregate(), as segregate() is just for segregating block into chunks(I assume you know the concept of block and chunk, if not, here is an illustration). add_block() segregates block and merge its free list into pStorage's free list. After add_block(), pStorage is not empty and you can allocate memory from it.