Skip to content

Instantly share code, notes, and snippets.

@xiongjia
Last active September 14, 2017 07:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiongjia/5c222ad60e7a4bb2d0f3 to your computer and use it in GitHub Desktop.
Save xiongjia/5c222ad60e7a4bb2d0f3 to your computer and use it in GitHub Desktop.
A simple sample of Boost Pool #devsample #boost
/**
* A simple sample of Boost Pool
*/
#include <iostream>
#include <vector>
#include <boost/pool/pool.hpp>
#include <boost/pool/object_pool.hpp>
#include <boost/pool/singleton_pool.hpp>
#include <boost/pool/pool_alloc.hpp>
#include <boost/foreach.hpp>
#include <boost/array.hpp>
/* Memory allocator */
class TestAllocator
{
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
static char * malloc(const size_type bytes)
{
std::cout << "Alloc (" << bytes << ")" << std::endl;
return reinterpret_cast<char *>(std::malloc(bytes));
}
static void free(char * const block)
{
std::cout << "Free block" << std::endl;
std::free(block);
}
};
static void mem_pool_tests(void)
{
/* the test data */
struct TestData
{
int num;
};
/* pool tests */
boost::pool<TestAllocator> mpool(sizeof(struct TestData));
boost::array<struct TestData*, 128> allData = { NULL };
/* mpool.malloc */
for (std::size_t idx = 0; idx < allData.size(); ++idx)
{
allData[idx] = reinterpret_cast<struct TestData*>(mpool.malloc());
(allData[idx])->num = idx;
}
/* print all data and free these memory */
std::cout << "All Pool Data: ";
BOOST_FOREACH(struct TestData *item, allData)
{
std::cout << "[" << item->num << "] ";
mpool.free(item);
}
std::cout << std::endl;
/* To allocate it again.
* The mpool should find the 'free' items and needn't the real malloc()
*/
for (std::size_t idx = 0; idx < allData.size(); ++idx)
{
allData[idx] = reinterpret_cast<struct TestData*>(mpool.malloc());
(allData[idx])->num = idx;
}
/* free the unused chunk all items still available */
mpool.release_memory();
std::cout << "All Pool Data: ";
BOOST_FOREACH(struct TestData *item, allData)
{
std::cout << "[" << item->num << "] ";
}
std::cout << std::endl;
/* purge all memory */
mpool.purge_memory();
}
static void obj_pool_tests(void)
{
/* the test object */
class TestObj
{
private:
int m_num;
public:
TestObj(void)
: m_num(0)
{
std::cout << "Create TestObj" << std::endl;
}
~TestObj(void)
{
std::cout << "Delete TestObj, Num: " << m_num << std::endl;
}
void Set(int num)
{
m_num = num;
}
void Print(void)
{
std::cout << "TestObj(" << m_num << ")" << std::endl;
}
};
boost::object_pool<TestObj, TestAllocator> mpool;
boost::array<TestObj*, 32> allData = { NULL };
for (std::size_t idx = 0; idx < allData.size(); ++idx)
{
allData[idx] = mpool.malloc();
allData[idx]->Set(idx);
}
/* print all data and free these objects */
std::cout << "All Pool Data: " << std::endl;
BOOST_FOREACH(TestObj *item, allData)
{
item->Print();
mpool.destroy(item);
}
std::cout << std::endl;
}
static void singleton_pool(void)
{
/* the test data */
struct TestData
{
int num;
};
struct MPoolTag {};
typedef boost::singleton_pool<MPoolTag, sizeof(struct TestData), TestAllocator> mm_pool;
boost::array<struct TestData*, 128> allData = { NULL };
/* mpool.malloc */
for (std::size_t idx = 0; idx < allData.size(); ++idx)
{
allData[idx] = reinterpret_cast<struct TestData*>(mm_pool::malloc());
(allData[idx])->num = idx;
}
mm_pool::purge_memory();
}
int main(int argc, char **argv)
{
/* memory pool */
mem_pool_tests();
/* object pool */
obj_pool_tests();
/* singleton pool */
singleton_pool();
return 0;
}
# CMake build script
cmake_minimum_required(VERSION 2.8)
# project name & version
project(ProgPoolTest)
# common settings (Boost libraries)
if (MSVC)
# Enable the static libraries on Windows
foreach (flag_var
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
# update to the static version of the run time library
string(REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endforeach()
set(CMAKE_C_STANDARD_LIBRARIES "")
set(CMAKE_CXX_STANDARD_LIBRARIES "")
endif()
# enable boost static flag
unset(Boost_LIBRARIES)
set(Boost_USE_STATIC ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
# boost components
find_package(Boost REQUIRED system thread date_time chrono)
include_directories("${PROJECT_SOURCE_DIR}"
"${Boost_INCLUDE_DIRS}")
set(progpool_dbg_libs
"${Boost_SYSTEM_LIBRARY_DEBUG}"
"${Boost_DATE_TIME_LIBRARY_DEBUG}"
"${Boost_CHRONO_LIBRARY_DEBUG}"
"${Boost_THREAD_LIBRARY_DEBUG}")
set(proglpool_opt_libs
"${Boost_SYSTEM_LIBRARY_RELEASE}"
"${Boost_DATE_TIME_LIBRARY_RELEASE}"
"${Boost_CHRONO_LIBRARY_RELEASE}"
"${Boost_THREAD_LIBRARY_RELEASE}")
add_executable(ProgPool
"${PROJECT_SOURCE_DIR}/0_main.cxx")
target_link_libraries(ProgPool)
target_link_libraries(ProgPool
debug "${progpool_dbg_libs}"
optimized "${proglpool_opt_libs}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment