Debian Codegraph @

gobi-api/Gobi_2012-06-18-1054/Core/SharedBuffer.cpp

raw
1 /*===========================================================================
2 FILE:
3 SharedBuffer.cpp
4
5 DESCRIPTION:
6 Shareable protocol structures and affliated methods
7
8 PUBLIC CLASSES AND METHODS:
9
10 sSharedBuffer
11 Simple struct to represent a reference counted shareable (no copy)
12 buffer, as the basis for all buffer related classes
13
14 Copyright (c) 2012, Code Aurora Forum. All rights reserved.
15
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions are met:
18 * Redistributions of source code must retain the above copyright
19 notice, this list of conditions and the following disclaimer.
20 * Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23 * Neither the name of Code Aurora Forum nor
24 the names of its contributors may be used to endorse or promote
25 products derived from this software without specific prior written
26 permission.
27
28
29 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
33 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 POSSIBILITY OF SUCH DAMAGE.
40 ===========================================================================*/
41
42 //---------------------------------------------------------------------------
43 // Include Files
44 //---------------------------------------------------------------------------
45 #include "StdAfx.h"
46 #include "SharedBuffer.h"
47
48 //---------------------------------------------------------------------------
49 // Definitions
50 //---------------------------------------------------------------------------
51
52 // Synchronization object
53 struct sSharedBufferSync
54 {
55 public:
56 // Constructor
57 sSharedBufferSync()
58 : mbInitialized( false )
59 {
60 int nRet = pthread_mutex_init( &mSyncSection, NULL );
61 if (nRet != 0)
62 {
63 TRACE( "SharedBuffer: Unable to init sync mutex."
64 " Error %d: %s\n",
65 nRet,
66 strerror( nRet ) );
67 return;
68 }
69
70 mbInitialized = true;
71 };
72
73 // Destructor
74 ~sSharedBufferSync()
75 {
76 mbInitialized = false;
77 int nRet = pthread_mutex_destroy( &mSyncSection );
78 if (nRet != 0)
79 {
80 TRACE( "SharedBuffer: Unable to destroy sync mutex."
81 " Error %d: %s\n",
82 nRet,
83 strerror( nRet ) );
84 }
85
86 };
87
88 // Lock sync object
89 void Lock()
90 {
91 if (mbInitialized == true)
92 {
93 int nRet = pthread_mutex_lock( &mSyncSection );
94 if (nRet != 0)
95 {
96 TRACE( "SharedBuffer: Unable to lock sync mutex."
97 " Error %d: %s\n",
98 nRet,
99 strerror( nRet ) );
100 return;
101 }
102
103 }
104 };
105
106 // Unlock sync object
107 void Unlock()
108 {
109 if (mbInitialized == true)
110 {
111 int nRet = pthread_mutex_unlock( &mSyncSection );
112 if (nRet != 0)
113 {
114 TRACE( "SharedBuffer: Unable to unlock sync mutex."
115 " Error %d: %s\n",
116 nRet,
117 strerror( nRet ) );
118 return;
119 }
120
121 }
122 };
123
124 protected:
125 /* DIAG buffer critical section */
126 pthread_mutex_t mSyncSection;
127
128 /* Has this object been initialized? */
129 bool mbInitialized;
130 };
131
132 // Global (across all shared buffers) reference count guard
133 sSharedBufferSync gRefCount;
134
135 /*=========================================================================*/
136 // sSharedBuffer Methods
137 /*=========================================================================*/
138
139 /*===========================================================================
140 METHOD:
141 sSharedBuffer (Public Method)
142
143 DESCRIPTION:
144 Constructor (copy passed in buffer)
145
146 PARAMETERS:
147 pDataToCopy [ I ] - The data buffer to copy (should be non-zero)
148 dataLen [ I ] - The length of the above buffer (should be > 1)
149 dataType [ I ] - Type of data (not used internal to class)
150
151 RETURN VALUE:
152 None
153 ===========================================================================*/
154 sSharedBuffer::sSharedBuffer(
155 const BYTE * pDataToCopy,
156 ULONG dataLen,
157 ULONG dataType )
158 : mpData( 0 ),
159 mSize( 0 ),
160 mType( dataType ),
161 mRefCount( 0 )
162 {
163 // Length not too small/not too big?
164 if (IsValidSize( dataLen ) == true)
165 {
166 // Yes, data actually exists?
167 if (pDataToCopy != 0)
168 {
169 // Yes, try to allocate memory
170 mpData = new BYTE[dataLen];
171 if (mpData != 0)
172 {
173 // Now copy into our allocation
174 memcpy( (PVOID)mpData,
175 (LPCVOID)pDataToCopy,
176 (SIZE_T)dataLen );
177
178 // Now set the size, we do this last so that our double
179 // deletion logic is only applied if we had an allocation
180 // in the first place
181 mSize = dataLen;
182 }
183 }
184 }
185 }
186
187 /*===========================================================================
188 METHOD:
189 sSharedBuffer (Public Method)
190
191 DESCRIPTION:
192 Constructor (assume ownership of passed in buffer)
193
194 PARAMETERS:
195 dataLen [ I ] - The length of the above buffer (should be > 1)
196 pDataToOwn [ I ] - The data buffer to assume ownership of (should
197 be non-zero)
198
199 dataType [ I ] - Type of data (not used internal to class)
200
201 NOTE: The order is intentionally reversed from the previous constructor
202 to avoid any cases of mistaken identity (copy versus assume ownership)
203
204 RETURN VALUE:
205 None
206 ===========================================================================*/
207 sSharedBuffer::sSharedBuffer(
208 ULONG dataLen,
209 PBYTE pDataToOwn,
210 ULONG dataType )
211 : mpData( 0 ),
212 mSize( 0 ),
213 mType( dataType ),
214 mRefCount( 0 )
215 {
216 // Data actually exists?
217 if (pDataToOwn != 0)
218 {
219 // Yes, length not too small/not too big?
220 if (IsValidSize( dataLen ) == true)
221 {
222 // Yes, assume ownership of the passed in buffer
223 mpData = pDataToOwn;
224 mSize = dataLen;
225 }
226 else
227 {
228 // This data buffer is not acceptable to us, but we have assumed
229 // ownership of the memory which we will now free
230 delete [] pDataToOwn;
231 }
232 }
233 }
234
235 /*===========================================================================
236 METHOD:
237 ~sSharedBuffer (Public Method)
238
239 DESCRIPTION:
240 Destructor
241
242 RETURN VALUE:
243 None
244 ===========================================================================*/
245 sSharedBuffer::~sSharedBuffer()
246 {
247 ASSERT( mRefCount == 0 );
248
249 // Buffer data to free?
250 if (mpData != 0)
251 {
252 // Yes, zero first byte for caution and then delete it
253 mpData[0] = 0;
254 delete [] mpData;
255
256 // Even more caution, zero out pointer
257 mpData = 0;
258 }
259 else if (mSize != 0)
260 {
261 ASSERT( (PVOID)("Double deletion detected in ~sSharedBuffer") == 0 );
262 }
263 }
264
265 /*===========================================================================
266 METHOD:
267 operator == (Public Method)
268
269 DESCRIPTION:
270 Equality operator
271
272 RETURN VALUE:
273 bool
274 ===========================================================================*/
275 bool sSharedBuffer::operator == ( const sSharedBuffer & refBuf ) const
276 {
277 // Assume they are not equal
278 bool bEq = false;
279
280 // The buffers must be the same
281 if (mpData == refBuf.mpData)
282 {
283 if (mSize == refBuf.mSize)
284 {
285 if (mRefCount == refBuf.mRefCount)
286 {
287 if (mType == refBuf.mType)
288 {
289 // The shared buffers are the same
290 bEq = true;
291 }
292 }
293 else
294 {
295 // Very odd - the buffers are the same, but not the ref count?!?
296 ASSERT( 0 );
297 }
298 }
299 else
300 {
301 // Very odd - the buffers are the same, but not the size?!?
302 ASSERT( 0 );
303 }
304 }
305
306 return bEq;
307 }
308
309 /*===========================================================================
310 METHOD:
311 operator != (Public Method)
312
313 DESCRIPTION:
314 Inequality operator
315
316 RETURN VALUE:
317 bool
318 ===========================================================================*/
319 bool sSharedBuffer::operator != ( const sSharedBuffer & refBuf ) const
320 {
321 if (*this == refBuf)
322 {
323 return false;
324 }
325
326 return true;
327 }
328
329 /*===========================================================================
330 METHOD:
331 AddRef (Internal Method)
332
333 DESCRIPTION:
334 Increment reference count
335
336 RETURN VALUE:
337 None
338 ===========================================================================*/
339 void sSharedBuffer::AddRef()
340 {
341 gRefCount.Lock();
342 mRefCount++;
343 gRefCount.Unlock();
344 }
345
346 /*===========================================================================
347 METHOD:
348 Release (Internal Method)
349
350 DESCRIPTION:
351 Release reference, delete if reference count zero
352
353 RETURN VALUE:
354 None
355 ===========================================================================*/
356 void sSharedBuffer::Release()
357 {
358 gRefCount.Lock();
359
360 ASSERT( mRefCount != 0 );
361
362 // Decrement reference count
363 if (mRefCount > 0)
364 {
365 mRefCount--;
366 }
367
368 // ... and delete if reference count now 0
369 if (mRefCount == 0)
370 {
371 delete this;
372 }
373
374 gRefCount.Unlock();
375 }