KWStyle - itkObjectStore.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkObjectStore.h.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_h
18 #define __itkObjectStore_h
19
20 #include "itkObjectFactory.h"
21 #include "itkObject.h"
22 #include <vector>
23
24 namespace itk {
25
26 /** \class ObjectStore
27  * \brief A specialized memory management object for allocating and destroying
28  * contiguous blocks of objects.
29  *
30  * ObjectStore implements a dynamically sizeable free memory store, from which
31  * instantiated objects can be borrowed and returned without always invoking
32  * calls to new/delete.  This type of memory management is useful in situations
33  * where calls to new/delete may be expensive, such as a multithreaded
34  * environment to avoid the heap contention problem.
35  *
36  * ObjectStore is designed to grow dynamically.  Shrinking is a more difficult
37  * problem and is only done if it will not invalidate any pointers that have
38  * been "lent" to a calling application.
39  *
40  * This implementation uses a very simple, list-based scheme to manage
41  * pointers that have been borrowed and returned.  Memory overhead incurred is
42  * one pointer per object allocated.  Because of this overhead, ObjectStore is 
43  * not efficient for use with small objects such as native types.
44  *
45  * Important notes on thread-safety: This object is thread-safe in the same
46  * sense that STL defines thread-safety: simultaneous operations on distinct
47  * containers are safe.  It is the user's responsibility to apply appropriate
48  * mutex locks if the same container is used across multiple threads. One (or
49  * more) ObjectStore's can be safely be created for each thread -- and may even
50  * be more efficient in terms of memory use than sharing a single ObjectStore
51  * across threads. Calls to \em{new} and \em{delete} have been placed in
52  * critical sections in case a compiler's implementation of new/delete is not
53  * thread-safe.
54  * 
55  * Warnings:  For efficiency reasons, the ObjectStore does not guard against the
56  * same pointer being Returned() more than once.  Doing this could result in
57  * serious problems.
58  */
59 template < class TObjectType >
60 class ITK_EXPORT ObjectStore : public Object
61 {
62 public:
63 IND ***/** Standard typedefs. */
64   typedef ObjectStore Self;
65 TDA   typedef Object  Superclass;
66 TDA   typedef SmartPointer<Self>  Pointer;
67 TDA   typedef SmartPointer<const Self>  ConstPointer;
68
69   /** Method for creation through the object factory. */
70   itkNewMacro(Self);
71
72   /** Run-time type information (and related methods). */
73   itkTypeMacro(ObjectStore, Object);
74
75   /** Type of the objects in storage. */
76   typedef TObjectType ObjectType;
77
78   /** Type of list for storing pointers to free memory.*/
79   typedef std::vector<ObjectType *> FreeListType;
80
81   /** Type of memory allocation strategy */
82   typedef enum {LINEAR_GROWTH = 0, EXPONENTIAL_GROWTH = 1} GrowthStrategyType;
83     
84   /** Borrow a pointer to an object from the memory store. */
85   ObjectType *Borrow();
86
87   /** Return a pointer to the memory store for reuse. WARNING: The ObjectStore
88    *  assumes a pointer is returned exactly once after each time it has been
89    *  borrowed. */
90   void Return(ObjectType *p);
91
92   /** Returns the size of the container.  This is not the number of objects
93    *  available, but the total number of objects allocated. */
94   itkGetMacro(Size, ::size_t);
95
96   /** Ensures that there are at least n elements allocated in the storage
97    *  container.  Will not shrink the container, but may enlarge the
98    *   container. */
99   void Reserve(::size_t n);
100
101   /** Attempts to free memory that is not in use and shrink the size of the
102    *  container.  Not guaranteed to do anything. */
103   void Squeeze();
104   
105   /** Frees all memory in the container */
106   void Clear();
107
108   /** Set/Get the linear growth size */
109   itkSetMacro(LinearGrowthSize, ::size_t);
110   itkGetMacro(LinearGrowthSize, ::size_t);
111
112   /** Set/Get the growth strategy. */
113   itkSetMacro(GrowthStrategy, GrowthStrategyType);
114   itkGetMacro(GrowthStrategy, GrowthStrategyType);
115
116   /** Set growth strategy to exponential */
117   void SetGrowthStrategyToExponential()
118 IND **{ this->SetGrowthStrategy(EXPONENTIAL_GROWTH); }
119
120   /** Set growth strategy to linear */
121   void SetGrowthStrategyToLinear()
122 IND **{ this->SetGrowthStrategy(LINEAR_GROWTH); }
123   
124 protected:
125   ObjectStore();
126   ~ObjectStore();
127   virtual void PrintSelf(std::ostream& os, Indent indent) const;
128
129   /** Returns a new size to grow. */
130   ::size_t GetGrowthSize();
131   
132   struct MemoryBlock
133 IND **{
134     MemoryBlock(): Size(0), Begin(0) {}
135
136     MemoryBlock(::size_t n) : Size(n)
137 IND ****{ Begin = new ObjectType[n];  }
138
139     ~MemoryBlock()  {  } // Purposely does *not* free memory
140
141     void Delete()
142 IND ****{ if (Begin !=0) delete[] Begin; }
143     
144     ObjectType *Begin;
145     ::size_t Size;
146 IND **};
147   
148 private:
149   ObjectStore(const Self&);    //purposely not implemented
150   void operator=(const Self&); //purposely not implemented
151
152   GrowthStrategyType m_GrowthStrategy;
153   
154   ::size_t m_Size;
155   ::size_t m_LinearGrowthSize;
156   
157   /** Pointers to objects available for borrowing. */
158   FreeListType  m_FreeList;
159
160   /** A list of MemoryBlocks that have been allocated. */
161   std::vector<MemoryBlock>   m_Store;
162
163 };
164
165   
166 // end namespace itk
167
168 #ifndef ITK_MANUAL_INSTANTIATION
169 #include "itkObjectStore.txx"
170 #endif
171
172 #endif
173

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:37PM
© Kitware Inc.