00001 /* 00002 Copyright (C) 2003-2005 The Pentagram team 00003 00004 This program is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU General Public License 00006 as published by the Free Software Foundation; either version 2 00007 of the License, or (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program; if not, write to the Free Software 00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 */ 00018 #include "pent_include.h" 00019 00020 #include "SegmentedAllocator.h" 00021 00022 DEFINE_RUNTIME_CLASSTYPE_CODE(SegmentedAllocator,Allocator); 00023 00024 SegmentedAllocator::SegmentedAllocator(size_t nodeCapacity_, uint32 nodes_): Allocator(), nodes(nodes_) 00025 { 00026 pools.push_back(new SegmentedPool(nodeCapacity_, nodes_)); 00027 nodeCapacity = pools[0]->getNodeCapacity(); 00028 // pout << "Initial Pool Created: Nodes - " << nodes << ", Node Capacity - " 00029 // << nodeCapacity << std::endl; 00030 } 00031 00032 SegmentedAllocator::~SegmentedAllocator() 00033 { 00034 std::vector<SegmentedPool *>::iterator i; 00035 for (i = pools.begin(); i != pools.end(); ++i) 00036 { 00037 delete *i; 00038 } 00039 00040 pools.clear(); 00041 } 00042 00043 void * SegmentedAllocator::allocate(size_t size) 00044 { 00045 std::vector<SegmentedPool *>::iterator i; 00046 SegmentedPool * p; 00047 00048 if (size > nodeCapacity) 00049 return 0; 00050 00051 for (i = pools.begin(); i != pools.end(); ++i) 00052 { 00053 if (! (*i)->isFull()) 00054 return (*i)->allocate(size); 00055 } 00056 00057 // else we need a new pool 00058 p = new SegmentedPool(nodeCapacity, nodes); 00059 if (p) 00060 { 00061 // pout << "New Pool Created: Nodes - " << nodes << ", Node Capacity - " 00062 // << nodeCapacity << std::endl; 00063 00064 pools.push_back(p); 00065 return p->allocate(size); 00066 } 00067 00068 // fail 00069 return 0; 00070 } 00071 00072 Pool * SegmentedAllocator::findPool(void * ptr) 00073 { 00074 std::vector<SegmentedPool *>::iterator i; 00075 for (i = pools.begin(); i != pools.end(); ++i) 00076 { 00077 if ((*i)->inPool(ptr)) 00078 return *i; 00079 } 00080 return 0; 00081 } 00082 00083 void SegmentedAllocator::freeResources() 00084 { 00085 if (pools.empty()) 00086 return; 00087 00088 // Pop back only -- it should suffice. 00089 while(pools.back()->isEmpty()) 00090 { 00091 delete pools.back(); 00092 pools.pop_back(); 00093 00094 if (pools.empty()) 00095 return; 00096 } 00097 } 00098 00099 void SegmentedAllocator::printInfo() 00100 { 00101 std::vector<SegmentedPool *>::iterator it; 00102 int i = 0; 00103 00104 pout << "Pools: " << pools.size() << std::endl; 00105 for (it = pools.begin(); it != pools.end(); ++it) 00106 { 00107 pout << " Pool " << i++ << ":" << std::endl; 00108 (*it)->printInfo(); 00109 } 00110 }