00001 #include "ShPool.hpp"
00002
00003 #ifdef SH_USE_MEMORY_POOL
00004
00005 #include "ShDebug.hpp"
00006
00007 namespace SH {
00008
00009 ShPool::ShPool(std::size_t element_size, std::size_t block_size)
00010 : m_element_size(element_size),
00011 m_block_size(block_size),
00012 m_next(0)
00013 {
00014 }
00015
00016 void* ShPool::alloc()
00017 {
00018 if (!m_next) {
00019
00020 char* block = new char[m_block_size * m_element_size];
00021 void* next = 0;
00022 for (std::size_t i = 0; i < m_block_size; i++) {
00023 *((void**)block) = next;
00024 next = (void*)block;
00025 block += m_element_size;
00026 }
00027 m_next = next;
00028 }
00029 void* r = m_next;
00030 m_next = *((void**)m_next);
00031 return r;
00032 }
00033
00034 void ShPool::free(void* ptr)
00035 {
00036 *((void**)ptr) = m_next;
00037 m_next = ptr;
00038 }
00039
00040 }
00041
00042 #endif // SH_USE_MEMORY_POOL