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)
5  2011, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
33 #ifndef STEAM_ENGINE_TRACKING_HEAP_BLOCK_PROVIDER_H
34 #define STEAM_ENGINE_TRACKING_HEAP_BLOCK_PROVIDER_H
35 
36 
37 #include "lib/error.hpp"
38 #include "lib/hash-value.h"
40 #include "lib/scoped-ptrvect.hpp"
41 
42 #include <unordered_map>
43 #include <memory>
44 
45 
46 namespace steam {
47 namespace engine {
48 
49  namespace error = lumiera::error;
50 
51  using lib::ScopedPtrVect;
52  using lib::HashVal;
53 
54  namespace diagn {
55 
56  using std::unique_ptr;
57 
58 
64  class Block
66  {
67  unique_ptr<char[]> storage_;
68 
69  bool was_released_;
70 
71  public:
72  explicit
73  Block(size_t bufferSize)
74  : storage_(bufferSize? new char[bufferSize] : NULL)
75  , was_released_(false)
76  { }
77 
78  bool
79  was_used() const
80  {
81  return bool(storage_);
82  }
83 
84  bool
85  was_closed() const
86  {
87  return was_released_;
88  }
89 
90  void*
91  accessMemory() const
92  {
93  REQUIRE (storage_, "Block was never prepared for use");
94  return storage_.get();
95  }
96 
97  void
98  markReleased()
99  {
100  was_released_ = true;
101  }
102  };
103 
104  class BlockPool;
105 
106  typedef std::unordered_map<HashVal,BlockPool> PoolTable;
107  }
108 
109 
120  : public BufferProvider
121  {
122  unique_ptr<diagn::PoolTable> pool_;
124 
125  public:
126  /* === BufferProvider interface === */
127 
128  virtual uint prepareBuffers (uint count, HashVal typeID) override;
129  virtual BuffHandle provideLockedBuffer (HashVal typeID) override;
130  virtual void mark_emitted (HashVal, LocalTag const&) override;
131  virtual void detachBuffer (HashVal, LocalTag const&, Buff&) override;
132 
133  public:
135  virtual ~TrackingHeapBlockProvider();
136 
137  size_t emittedCnt() const;
138 
139  diagn::Block& access_emitted (uint bufferID);
140 
141  template<typename TY>
142  TY& accessAs (uint bufferID);
143 
144  private:
145  bool withinOutputSequence (uint bufferID) const;
146  diagn::BlockPool& getBlockPoolFor (HashVal typeID);
147  diagn::Block* locateBlock (HashVal typeID, void*);
148  diagn::Block* searchInOutSeqeuence (void* storage);
149  };
150 
151 
152 
161  template<typename TY>
162  TY&
164  {
165  if (!withinOutputSequence (bufferID))
166  throw error::Invalid ("Buffer with the given ID not yet emitted");
167 
168  diagn::Block& memoryBlock = access_emitted (bufferID);
169  TY* converted = std::launder (reinterpret_cast<TY*> (memoryBlock.accessMemory()));
170 
171  REQUIRE (converted);
172  return *converted;
173  }
174 
175 
176 
177 
178 }} // namespace steam::engine
179 #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:37
an opaque mark to be used by the BufferProvider implementation.
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:190
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:111
size_t HashVal
a STL compatible hash value
Definition: hash-value.h:52
Interface: a facility providing and managing working buffers for media calculations.
placeholder type for the contents of a data buffer.
Definition: streamtype.hpp:112
Abstraction to represent buffer management and lifecycle within the render engine.