Lumiera  0.pre.03
»edit your freedom«
tracking-heap-block-provider.hpp
Go to the documentation of this file.
1 /*
2  TRACKING-HEAP-BLOCK-PROVIDER.hpp - plain heap allocating BufferProvider implementation for tests
3 
4  Copyright (C) Lumiera.org
5  2011, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
42 #ifndef STEAM_ENGINE_TRACKING_HEAP_BLOCK_PROVIDER_H
43 #define STEAM_ENGINE_TRACKING_HEAP_BLOCK_PROVIDER_H
44 
45 
46 #include "lib/error.hpp"
47 #include "lib/hash-value.h"
49 #include "lib/scoped-ptrvect.hpp"
50 
51 #include <unordered_map>
52 #include <memory>
53 
54 
55 namespace steam {
56 namespace engine {
57 
58  namespace error = lumiera::error;
59 
60  using lib::ScopedPtrVect;
61  using lib::HashVal;
62 
63  namespace diagn {
64 
65  using std::unique_ptr;
66 
67 
73  class Block
75  {
76  unique_ptr<char[]> storage_;
77 
78  bool was_released_;
79 
80  public:
81  explicit
82  Block(size_t bufferSize)
83  : storage_(bufferSize? new char[bufferSize] : NULL)
84  , was_released_(false)
85  { }
86 
87  bool
88  was_used() const
89  {
90  return bool(storage_);
91  }
92 
93  bool
94  was_closed() const
95  {
96  return was_released_;
97  }
98 
99  void*
100  accessMemory() const
101  {
102  REQUIRE (storage_, "Block was never prepared for use");
103  return storage_.get();
104  }
105 
106  void
107  markReleased()
108  {
109  was_released_ = true;
110  }
111  };
112 
113  class BlockPool;
114 
115  typedef std::unordered_map<HashVal,BlockPool> PoolTable;
116  }
117 
118 
129  : public BufferProvider
130  {
131  unique_ptr<diagn::PoolTable> pool_;
133 
134  public:
135  /* === BufferProvider interface === */
136 
137  virtual uint prepareBuffers (uint count, HashVal typeID);
138  virtual BuffHandle provideLockedBuffer (HashVal typeID);
139  virtual void mark_emitted (HashVal entryID, LocalKey const&);
140  virtual void detachBuffer (HashVal entryID, LocalKey const&);
141 
142  public:
144  virtual ~TrackingHeapBlockProvider();
145 
146  size_t emittedCnt() const;
147 
148  diagn::Block& access_emitted (uint bufferID);
149 
150  template<typename TY>
151  TY& accessAs (uint bufferID);
152 
153  private:
154  bool withinOutputSequence (uint bufferID) const;
155  diagn::BlockPool& getBlockPoolFor (HashVal typeID);
156  diagn::Block* locateBlock (HashVal typeID, void*);
157  diagn::Block* searchInOutSeqeuence (void* storage);
158  };
159 
160 
161 
170  template<typename TY>
171  TY&
173  {
174  if (!withinOutputSequence (bufferID))
175  throw error::Invalid ("Buffer with the given ID not yet emitted");
176 
177  diagn::Block& memoryBlock = access_emitted (bufferID);
178  TY* converted = std::launder (reinterpret_cast<TY*> (memoryBlock.accessMemory()));
179 
180  REQUIRE (converted);
181  return *converted;
182  }
183 
184 
185 
186 
187 }} // namespace steam::engine
188 #endif
Helper for implementing a diagnostic BufferProvider: A block of heap allocated storage, with the capability to store some additional tracking information.
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
Steam-Layer implementation namespace root.
Managing lifecycle for a collection of objects.
Simple vector based collection of pointers, managing lifecycle of the pointed-to objects.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
an opaque ID to be used by the BufferProvider implementation.
TY & accessAs(uint bufferID)
convenience shortcut: access the buffer with the given number, then try to convert the raw memory to ...
simple BufferProvider implementation with additional allocation tracking.
Lumiera error handling (C++ interface).
Hash value types and utilities.
Handle for a buffer for processing data, abstracting away the actual implementation.
Definition: buffhandle.hpp:115
size_t HashVal
a STL compatible hash value
Definition: hash-value.h:56
Interface: a facility providing and managing working buffers for media calculations.
Abstraction to represent buffer management and lifecycle within the render engine.