00001 /* 00002 * Copyright (C) Sergey P. Derevyago, 2008-2009. 00003 * 00004 * Permission to copy, use, modify, sell and distribute this software is granted 00005 * provided this copyright notice appears in all copies. 00006 * This software is provided "as is" without express or implied warranty, and 00007 * with no claim as to its suitability for any purpose. 00008 * 00009 */ 00010 00016 #ifndef __DERS__MEM_POOL_HPP__ 00017 #define __DERS__MEM_POOL_HPP__ 00018 00019 #include <ders/config.hpp> 00020 #include <new> 00021 #include <stddef.h> 00022 00023 namespace ders { // ::ders 00024 00028 class mem_pool { 00029 void* impl; 00030 00031 mem_pool(const mem_pool&); 00032 mem_pool& operator=(const mem_pool&); 00033 00034 public: 00035 mem_pool(size_t max_chunk_size=2*1024, size_t min_objs_in_chunk=4); 00036 ~mem_pool(); 00037 00038 void* allocate(size_t size); 00039 void deallocate(void* ptr, size_t size); 00040 }; 00041 00045 template<class T> 00046 class mp_newbuf { 00047 mem_pool& mp; 00048 void* ptr; 00049 00050 mp_newbuf(const mp_newbuf&); 00051 mp_newbuf& operator=(const mp_newbuf&); 00052 00053 public: 00054 explicit mp_newbuf(mem_pool& m) : mp(m), ptr(m.allocate(sizeof(T))) {} 00055 ~mp_newbuf() { if (ptr) mp.deallocate(ptr, sizeof(T)); } 00056 00057 mem_pool& pool() const { return mp; } 00058 void* get() const { return ptr; } 00059 T* rls(T* p) { ptr=0; return p; } 00060 }; 00061 00062 } // namespace ::ders 00063 00064 #endif // __DERS__MEM_POOL_HPP__ 00065