摘要:
class MemoryChunk{private: MemoryChunk* next;//指向下个内存块 void * mem;//指向可用的内存 size_t chunkSize;//该内存块的大小 size_t bytesAlreadyAllocated;//已经分配的字节数public: MemoryChunk(MemoryChunk *nextChunk ,size_t chunkSize); ~MemoryChunk(); inline void* alloc(size_t size); inline void free (void... 阅读全文
摘要:
#include templateclass MemoryPool{public: MemoryPool(size_t size =EXPANSION_SIZE); ~MemoryPool(); //从空闲列表中分配T元素 inline void * alloc(size_t size); inline void free(void * someElement);private: MemoryPool* next; enum{EXPANSION_SIZE=2}; //将空闲元素添加至空闲列表 void expanTheFreeList(in... 阅读全文
摘要:
#include class NextOnFreeList{public: NextOnFreeList *next;};class Rational {public: Rational(int a = 0, int b = 1) : n(a), d(b) {} inline void * operator new (size_t size); //over load new and delete operator inline void operator delete (void *doomed, size_t size); static void newMemPool() {ex... 阅读全文
摘要:
1 单例模式下构建出来的对象本质上仍然是一个全局变量,因此全局变量可以完成同样的功能。2 需要考虑线程安全,避免new出2个对象。class SingleTon{private: int i_; SingleTon(int x) : i_(x) {} SingleTon &operator = (SingleTon &); SingleTon(const SingleTon &); //not allow copy and assign ~SingleTon(){}; static SingleTon *st_; static boost... 阅读全文