| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkObjectStore.txx.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:43 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
|
PURPOSE. See the above copyright notices for more information. |
| 15 |
|
|
| 16 |
|
=========================================================================*/ |
| 17 |
|
#ifndef __itkObjectStore_txx |
| 18 |
|
#define __itkObjectStore_txx |
| 19 |
|
|
| 20 |
|
#include "itkObjectStore.h" |
| 21 |
|
|
| 22 |
|
namespace itk { |
| 23 |
|
|
| 24 |
|
template <class TObjectType> |
| 25 |
|
ObjectStore<TObjectType> |
| 26 |
|
::ObjectStore() |
| 27 |
|
{ |
| 28 |
|
m_Size = 0; |
| 29 |
|
m_LinearGrowthSize = 1024; |
| 30 |
|
m_GrowthStrategy = EXPONENTIAL_GROWTH; |
| 31 |
|
} |
| 32 |
|
|
| 33 |
|
template <class TObjectType> |
| 34 |
|
ObjectStore<TObjectType> |
| 35 |
|
::~ObjectStore() |
| 36 |
|
{ |
| 37 |
|
this->Clear(); |
| 38 |
|
} |
| 39 |
|
|
| 40 |
|
template <class TObjectType> |
| 41 |
|
void |
| 42 |
|
ObjectStore<TObjectType> |
| 43 |
|
::Reserve(::size_t n) |
| 44 |
|
{ |
| 45 |
|
// No need to grow? Do nothing. |
| 46 |
|
if ( n <= m_Size ) return; |
| 47 |
|
|
| 48 |
|
// Need to grow. Allocate a new block of memory and copy that block's |
| 49 |
|
// pointers into the m_FreeList. Updates m_Size appropriately. |
| 50 |
|
MemoryBlock new_block( n - m_Size ); |
| 51 |
|
m_Store.push_back( new_block ); |
| 52 |
|
|
| 53 |
|
m_FreeList.reserve(n); |
| 54 |
|
for (ObjectType *ptr = new_block.Begin; |
| 55 |
|
ptr < new_block.Begin + new_block.Size; ptr++) |
| 56 |
|
{ m_FreeList.push_back(ptr); } |
| 57 |
|
m_Size += ( n - m_Size ); |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
template <class TObjectType> |
| 61 |
|
typename ObjectStore<TObjectType>::ObjectType * |
| 62 |
|
ObjectStore<TObjectType> |
| 63 |
|
::Borrow() |
| 64 |
|
{ |
| 65 |
|
ObjectType *p; |
| 66 |
|
if (m_FreeList.empty() ) // must allocate more memory |
| 67 |
|
{ |
| 68 |
|
this->Reserve( m_Size + this->GetGrowthSize() ); |
| 69 |
|
} |
| 70 |
|
p = m_FreeList.back(); |
| 71 |
|
m_FreeList.pop_back(); |
| 72 |
|
return p; |
| 73 |
|
|
| 74 |
|
//To do: This method will need to decrement counters in the memory blocks so |
| 75 |
|
//that blocks know when they can be deleted. |
| 76 |
|
} |
| 77 |
|
|
| 78 |
|
template <class TObjectType> |
| 79 |
|
void |
| 80 |
|
ObjectStore<TObjectType> |
| 81 |
|
::Return(ObjectType *p) |
| 82 |
|
{ |
| 83 |
|
// For speed, does no checking. |
| 84 |
|
m_FreeList.push_back(p); |
| 85 |
|
|
| 86 |
|
// if ( m_FreeList.size() != m_Size ) |
| 87 |
|
// { m_FreeList.push_back(p); } |
| 88 |
|
// else object has been used incorrectly |
| 89 |
|
|
| 90 |
|
// To do: |
| 91 |
|
// Eventually this method will have to increment counters in the memory |
| 92 |
|
// blocks so that blocks know when they can be deleted. This will also allow |
| 93 |
|
// for checking to see if p actually belongs to this store. |
| 94 |
|
// |
| 95 |
|
// Problems could easily result if a pointer is Returned more than |
| 96 |
|
// once. Only time-wise efficient way to deal with this would be a flag in |
| 97 |
|
// the allocated memory block for each object, sort of how malloc deals with |
| 98 |
|
// it? |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
|
| 102 |
EML |
|
| 103 |
|
template <class TObjectType> |
| 104 |
|
::size_t |
| 105 |
|
ObjectStore<TObjectType> |
| 106 |
|
::GetGrowthSize() |
| 107 |
|
{ |
| 108 |
|
switch (m_GrowthStrategy) |
| 109 |
|
{ |
| 110 |
|
case LINEAR_GROWTH: |
| 111 |
IND |
******return m_LinearGrowthSize; |
| 112 |
IND |
******break; |
| 113 |
|
case EXPONENTIAL_GROWTH: |
| 114 |
IND |
******if (m_Size == 0) return m_LinearGrowthSize; |
| 115 |
IND |
******else return m_Size; |
| 116 |
IND |
******break; |
| 117 |
|
default: |
| 118 |
IND |
******return m_LinearGrowthSize; |
| 119 |
|
} |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
template <class TObjectType> |
| 123 |
|
void |
| 124 |
|
ObjectStore<TObjectType> |
| 125 |
|
::Squeeze() |
| 126 |
|
{ |
| 127 |
|
// Not implemented yet. |
| 128 |
|
|
| 129 |
|
} |
| 130 |
|
|
| 131 |
|
template <class TObjectType> |
| 132 |
|
void |
| 133 |
|
ObjectStore<TObjectType> |
| 134 |
|
::Clear() |
| 135 |
|
{ |
| 136 |
|
// Clear the free list. |
| 137 |
|
m_FreeList.clear(); |
| 138 |
|
|
| 139 |
|
// Empty the MemoryBlock list and deallocate all memory blocks. |
| 140 |
|
while ( ! m_Store.empty() ) |
| 141 |
|
{ |
| 142 |
|
m_Store.back().Delete(); |
| 143 |
|
m_Store.pop_back(); |
| 144 |
|
} |
| 145 |
|
m_Size = 0; |
| 146 |
|
} |
| 147 |
|
|
| 148 |
|
template <class TObjectType> |
| 149 |
|
void |
| 150 |
|
ObjectStore<TObjectType> |
| 151 |
|
::PrintSelf(std::ostream& os, Indent indent) const |
| 152 |
|
{ |
| 153 |
|
Superclass::PrintSelf(os,indent); |
| 154 |
|
|
| 155 |
|
os << indent << "m_GrowthStrategy: " << m_GrowthStrategy << std::endl; |
| 156 |
|
os << indent << "m_Size: " << m_Size << std::endl; |
| 157 |
LEN |
os << indent << "m_LinearGrowthSize: " << static_cast<unsigned long>( m_LinearGrowthSize ) << std::endl; |
| 158 |
LEN |
os << indent << "Free list size: " << static_cast<unsigned long>( m_FreeList.size() ) << std::endl; |
| 159 |
LEN |
os << indent << "Free list capacity: " << static_cast<unsigned long>( m_FreeList.capacity() ) << std::endl; |
| 160 |
LEN |
os << indent << "Number of blocks in store: " << static_cast<unsigned long>( m_Store.size() ) << std::endl; |
| 161 |
|
} |
| 162 |
|
|
| 163 |
|
|
| 164 |
EML |
|
| 165 |
EML |
|
| 166 |
|
} // end namespace itk |
| 167 |
|
|
| 168 |
|
#endif |
| 169 |
|
|