BRL-CAD
constantpool.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
3 All Rights Reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8 * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13 * Neither the name of Sony Pictures Imageworks nor the names of its
14  contributors may be used to endorse or promote products derived from
15  this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef LIBOPTICAL_CONSTANTPOOL_H
30 #define LIBOPTICAL_CONSTANTPOOL_H
31 
32 #include "common.h"
33 
34 #include <vector>
35 #include <list>
36 #include <boost/foreach.hpp>
37 
38 #include "OpenImageIO/thread.h"
39 #ifdef OIIO_NAMESPACE
40 namespace OIIO = OIIO_NAMESPACE;
41 using OIIO::mutex;
42 using OIIO::lock_guard;
43 #endif
44 
45 using namespace OSL;
46 using namespace OSL::pvt;
47 
48 
49 #ifdef OSL_NAMESPACE
50 namespace OSL_NAMESPACE {
51 #endif
52 
53 namespace OSL {
54 namespace pvt {
55 
56 
57 /// A ConstantPool<T> is a way to allocate room for a small number of
58 /// T's at a time, such that the memory allocated will NEVER change its
59 /// address or be deallocated until the entire ConstantPool is
60 /// destroyed. Allocating from the pool is completely thread-safe.
61 ///
62 /// It is implemented as a linked list of memory blocks. A request for
63 /// a new allocation tries to fit it in one of the allocated blocks, but
64 /// if it won't fit anywhere, it makes a new block and adds it to the
65 /// head of the list.
66 template<class T>
67 class ConstantPool {
68 public:
69  /// Allocate a new pool of T's. The quanta, if supplied, is the
70  /// number of T's to malloc at a time.
71  ConstantPool (size_t quanta = 1000000) : m_quanta(quanta), m_total(0) { }
72 
74 
75  /// Allocate space enough for n T's, and return a pointer to the
76  /// start of that space.
77  T * alloc (size_t n) {
78  lock_guard lock (m_mutex);
79  // Check each block in the block list to see if it has enough space
80  BOOST_FOREACH (block_t &block, m_block_list) {
81  size_t s = block.size();
82  if ((s+n) <= block.capacity()) {
83  // Enough space in this block. Use it.
84  block.resize (s+n);
85  return &block[s];
86  }
87  }
88  // If we got here, there were no mini-blocks in the list with enough
89  // space. Make a new one.
90  m_block_list.push_front (block_t());
91  block_t &block (m_block_list.front());
92  size_t s = std::max (m_quanta, n);
93  block.reserve (s);
94  m_total += s * sizeof(T);
95  block.resize (n);
96  return &block[0];
97  }
98 
99 private:
100  typedef std::vector<T> block_t; ///< Type of block
101  std::list<block_t> m_block_list; ///< List of memory blocks
102  size_t m_quanta; ///< How big each memory block is (in T's, not bytes)
103  size_t m_total; ///< Total memory allocated (bytes!)
104  mutex m_mutex; ///< Thread-safe lock
105 };
106 
107 
108 }; // namespace OSL::pvt
109 }; // namespace OSL
110 
111 #ifdef OSL_NAMESPACE
112 }; // end namespace OSL_NAMESPACE
113 using namespace OSL_NAMESPACE;
114 #endif
115 
116 #endif /* LIBOPTICAL_CONSTANTPOOL_H */
ConstantPool(size_t quanta=1000000)
Definition: constantpool.h:71
if lu s
Definition: nmg_mod.c:3860
Header file for the BRL-CAD common definitions.
T * alloc(size_t n)
Definition: constantpool.h:77