00001 /* _______ __ 00002 / ___/ / ___ __ _ / / ___ 00003 / /__/ _ \/ _ \/ ' \/ _ \/ _ \ 00004 \___/_//_/\___/_/_/_/_.__/\___/ 00005 */ 00006 // 00007 // This software is copyright (C) by the Lawrence Berkeley 00008 // National Laboratory. Permission is granted to reproduce 00009 // this software for non-commercial purposes provided that 00010 // this notice is left intact. 00011 // 00012 // It is acknowledged that the U.S. Government has rights to 00013 // this software under Contract DE-AC03-765F00098 between 00014 // the U.S. Department of Energy and the University of 00015 // California. 00016 // 00017 // This software is provided as a professional and academic 00018 // contribution for joint exchange. Thus it is experimental, 00019 // is provided ``as is'', with no warranties of any kind 00020 // whatsoever, no support, no promise of updates, or printed 00021 // documentation. By using this software, you acknowledge 00022 // that the Lawrence Berkeley National Laboratory and 00023 // Regents of the University of California shall have no 00024 // liability with respect to the infringement of other 00025 // copyrights by any part of this software. 00026 // 00027 00028 #ifndef POOL_H 00029 #define POOL_H 00030 00031 // Pool is a class to optimize memory allocation. It is specialized to 00032 // allocate fixed size chunks of memory specified by ptrSize in the 00033 // constructor. Its operation is analogous to malloc, not new. It 00034 // does not initialize the memory in any way. The constructor can 00035 // optionally specify an initial pool size, and memory alignment. The 00036 // pool size will grow as needed by calling ::new. The pool size never 00037 // shrinks. Memory will be reclaimed at ~Pool(). The units of poolSize 00038 // are number-of-ptrSize-chunks. The units of alignment are bytes. 00039 00040 #include <cstdlib> 00041 #include "Vector.H" 00042 #include <list> 00044 class Pool; 00045 typedef std::list<Pool*> PoolList; 00046 00047 class Pool { 00048 public: 00050 Pool(int ptrSize, const char* name = "unnamed", 00051 int poolSize = 100, int alignment = sizeof(int)); 00053 ~Pool(); 00055 void* getPtr(); 00057 void returnPtr(void* ptr); 00059 long memUsage() const; 00060 00061 00062 // undocumented function, call this at own risk. You must 00063 // be absolutely positive that you have returned all the 00064 // ptr's you've asked for, or you can have major seg faults. 00065 void clear(); 00066 00067 // not for public consumption. used in memory tracking code. 00068 static PoolList* poolList_; 00069 char name_[64]; 00070 00071 protected: 00072 private: 00074 Vector<char*> pool_; 00076 int ptrSize_; 00078 int poolSize_; 00080 int alignment_; 00082 void* next_; 00084 void* getMoreMemory(); 00086 Pool(const Pool& rhs); 00087 const Pool& operator=(const Pool& rhs); // Not implemented 00088 00089 }; 00090 00091 00092 #endif