KSquare Utilities
RasterBuffer.cpp
Go to the documentation of this file.
1 /* RasterBuffer.h -- Implements RasterBuffering allowing multiple threads to add and remove Raster
2  * instances without corrupting memory.
3  * Copyright (C) 2011-2014 Kurt Kramer
4  * For conditions of distribution and use, see copyright notice in KKB.h
5  */
6 #include "FirstIncludes.h"
7 #if defined(WIN32)
8 #include <windows.h>
9 #else
10 #include <fcntl.h>
11 #include <semaphore.h>
12 #endif
13 
14 #include <errno.h>
15 #include <stdlib.h>
16 #include <istream>
17 #include <iostream>
18 #include <queue>
19 #include <vector>
20 using namespace std;
21 
22 
23 #include "KKBaseTypes.h"
24 #include "KKException.h"
25 #include "GoalKeeper.h"
26 #include "OSservices.h"
27 using namespace KKB;
28 
29 
30 #include "Raster.h"
31 
32 #include "RasterBuffer.h"
33 using namespace KKB;
34 
35 
36 
37 
38 
40  kkint32 _maxNumOfBuffers
41  ):
42 
43  buffer (),
44  gateKeeper (NULL),
45  maxNumOfBuffers (_maxNumOfBuffers),
46  memoryConsumed (0),
47  name (_name),
48  rastersDropped (0)
49 {
50  GoalKeeper::Create ("RasterBuffer_" + name, gateKeeper);
51  memoryConsumed = sizeof (RasterBuffer) + gateKeeper->MemoryConsumedEstimated ();
52 }
53 
54 
55 
57 {
58  while (buffer.size () > 0)
59  {
60  RasterPtr r = buffer.front ();
61  buffer.pop ();
62  delete r;
63  r = NULL;
64  }
65 
66  GoalKeeper::Destroy (gateKeeper);
67  gateKeeper = NULL;
68 }
69 
70 
71 
72 void RasterBuffer::ThrowOutOldestOccupiedBuffer ()
73 {
74  gateKeeper->StartBlock ();
75 
76  if (buffer.size () > 0)
77  {
78  RasterPtr r = buffer.front ();
79  buffer.pop ();
80  memoryConsumed = memoryConsumed - r->MemoryConsumedEstimated ();
81  delete r;
82  r = NULL;
83  rastersDropped++;
84  }
85 
86  gateKeeper->EndBlock ();
87  return;
88 } /* ThrowOutOldestOccupiedBuffer */
89 
90 
91 
92 
94 {
95  return maxNumOfBuffers - (kkint32)buffer.size ();
96 }
97 
98 
100 {
101  return (kkint32)buffer.size ();
102 }
103 
104 
105 void RasterBuffer::AddRaster (RasterPtr raster)
106 {
107  if (raster == NULL)
108  {
109  KKStr errMsg;
110  errMsg << "RasterBuffer::AddRaster raster == NULL";
111  cerr << std::endl << std::endl << errMsg << std::endl << std::endl;
112  throw KKException (errMsg);
113  }
114 
115  gateKeeper->StartBlock ();
116 
117  while (buffer.size () >= (kkuint32)maxNumOfBuffers)
118  ThrowOutOldestOccupiedBuffer ();
119 
120  buffer.push (raster);
121  memoryConsumed = memoryConsumed + raster->MemoryConsumedEstimated ();
122 
123  gateKeeper->EndBlock ();
124 } /* AddFrame */
125 
126 
127 
129 {
130  RasterPtr result = NULL;
131 
132  gateKeeper->StartBlock ();
133 
134  if (buffer.size () > 0)
135  {
136  result = buffer.front ();
137  buffer.pop ();
138  memoryConsumed = memoryConsumed - result->MemoryConsumedEstimated ();
139  }
140 
141  gateKeeper->EndBlock ();
142  return result;
143 } /* GetNextRaster */
144 
145 
146 
148 {
149  kkint32 result = 0;
150  gateKeeper->StartBlock ();
151  result = memoryConsumed;
152  gateKeeper->EndBlock ();
153  return result;
154 } /* MemoryConsumedEstimated */
155 
156 
157 
159 {
160  RasterPtr result = NULL;
161  gateKeeper->StartBlock ();
162  if (buffer.size () > 0)
163  {
164  result = new Raster (*(buffer.back ()));
165  }
166  gateKeeper->EndBlock ();
167  return result;
168 }
__int32 kkint32
Definition: KKBaseTypes.h:88
RasterPtr GetCopyOfLastImage()
Returns a copy of the last Raster instance added to the queue; if buffer is empty will return NULL...
static void Destroy(volatile GoalKeeperPtr &_goalKeeperInstance)
Destroys an existing instance of GoalKeeper.
Definition: GoalKeeper.cpp:491
kkint32 MemoryConsumedEstimated()
Returns an estimate of the amount of memory consumed in bytes.
kkint32 MemoryConsumedEstimated() const
Definition: GoalKeeper.cpp:166
static void Create(const KKStr &_name, volatile GoalKeeperPtr &_newGoalKeeper)
Create a GoalKeeper object and avoid a race condition doing it.
Definition: GoalKeeper.cpp:346
RasterBuffer(const KKStr &_name, kkint32 _maxNumOfBuffers)
Constructor.
KKTHread * KKTHreadPtr
KKStr operator+(const char *left, const KKStr &right)
Definition: KKStr.cpp:3976
KKStr(const KKStr &str)
Copy Constructor.
Definition: KKStr.cpp:561
void StartBlock()
Initiates a Block as long as another thread has not already locked this object.
Definition: GoalKeeper.cpp:214
void EndBlock()
Ends the block and allows other threads to pass through StatBlock.
Definition: GoalKeeper.cpp:295
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
kkint32 NumAvailable() const
The number of entries that are left in the buffer before &#39;maxNumOfBuffers&#39; is reached.
RasterPtr GetNextRaster()
Removes from the buffer the oldest instance of &#39;Raster&#39; and returns it to caller; if buffer is empty ...
Will manage a buffer that will allow multiple threads to add and remove instances of &#39;Raster&#39; objects...
Definition: RasterBuffer.h:34
kkint32 MemoryConsumedEstimated() const
Definition: Raster.cpp:864
kkint32 NumPopulated() const
KKException(const KKStr &_exceptionStr)
Definition: KKException.cpp:45
void AddRaster(RasterPtr raster)
Adds &#39;raster&#39; to the end of the queue giving the queue ownership of the instance. ...