SpatialOps
MemoryPool.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 The University of Utah
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 
27 #ifndef UT_MemoryPool_h
28 #define UT_MemoryPool_h
29 
30 #include <stack>
31 #include <map>
32 
33 #include <spatialops/structured/MemoryTypes.h>
34 
35 namespace SpatialOps {
36 
37 template<typename T>
38 class Pool{
39 
40  struct FieldQueue{
41  size_t accessCounter;
42  typedef std::stack<T*> Queue;
43  Queue queue;
44  FieldQueue(){ accessCounter=0; }
45  };
46 
47  typedef std::map<size_t,FieldQueue> FQSizeMap;
48  typedef std::map<T*,size_t> FieldSizeMap;
49 
50  static bool destroyed_;
51  bool pinned_;
52  size_t accessCounter_;
53 
54  FQSizeMap cpufqm_, gpufqm_;
55  FieldSizeMap fsm_;
56  size_t cpuhighWater_, gpuhighWater_;
57  Pool();
58  ~Pool();
59  Pool(const Pool&);
60  Pool& operator=(const Pool&);
61 
62  static Pool& self();
63 
64  const unsigned short int deviceIndex_;
65 
66  public:
67 
79  static T* get( const short int deviceLocation, const size_t n );
80 
87  static void put( const short int deviceLocation, T* t );
88 
92  static size_t active();
93 
97  static size_t total();
98 
103  static size_t flush_unused( const size_t n );
104 
105 };
106 
107 template<typename T> bool Pool<T>::destroyed_ = false;
108 
120 template<typename T>
122  public:
129  inline PoolAutoPtr(T* ptr, short int activeIndex)
130  : ptr(ptr), activeIndex(activeIndex)
131  {}
132 
136  inline T* get() { return ptr; }
137  inline const T* get() const { return ptr; }
138 
142  inline operator T*() { return ptr; }
143  inline operator const T*() const { return ptr; }
144 
148  inline T& operator*() { return *ptr; }
149  inline const T& operator*() const { return *ptr; }
150 
154  inline T* operator->() { return ptr; }
155  inline const T* operator->() const { return ptr; }
156 
160  inline ~PoolAutoPtr() { if(ptr != NULL) Pool<T>::put( activeIndex, ptr ); }
161 
162  private:
163  T* ptr;
164  const short int activeIndex;
165 };
166 
167 
168 } //namespace SpatialOps
169 
170 
171 #endif
static size_t flush_unused(const size_t n)
remove fields that have not been used for more than n calls to the pool
Definition: MemoryPool.cpp:275
T * operator->()
allow pointer like interaction
Definition: MemoryPool.h:154
static void put(const short int deviceLocation, T *t)
Return the requested block of memory to the pool. This should only be done for memory requested from ...
Definition: MemoryPool.cpp:217
takes ownership over memory allocated from the pool and puts the memory back into the pool when it is...
Definition: MemoryPool.h:121
PoolAutoPtr(T *ptr, short int activeIndex)
PoolAutoPtr constructor which takes ownership over ptr.
Definition: MemoryPool.h:129
T & operator*()
return reference to managed memory
Definition: MemoryPool.h:148
static size_t total()
Definition: MemoryPool.cpp:268
~PoolAutoPtr()
put managed memory back into other pool on destruction
Definition: MemoryPool.h:160
static size_t active()
Definition: MemoryPool.cpp:256