| 1 |
/*=========================================================================== |
| 2 |
FILE: |
| 3 |
SharedBuffer.h |
| 4 |
|
| 5 |
DESCRIPTION: |
| 6 |
Shareable buffer structures and affliated methods |
| 7 |
|
| 8 |
PUBLIC CLASSES AND METHODS: |
| 9 |
sSharedBuffer |
| 10 |
Simple struct to represent a reference counted shareable (no copy) |
| 11 |
buffer, as the basis for all buffer related classes |
| 12 |
|
| 13 |
Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 14 |
|
| 15 |
Redistribution and use in source and binary forms, with or without |
| 16 |
modification, are permitted provided that the following conditions are met: |
| 17 |
* Redistributions of source code must retain the above copyright |
| 18 |
notice, this list of conditions and the following disclaimer. |
| 19 |
* Redistributions in binary form must reproduce the above copyright |
| 20 |
notice, this list of conditions and the following disclaimer in the |
| 21 |
documentation and/or other materials provided with the distribution. |
| 22 |
* Neither the name of Code Aurora Forum nor |
| 23 |
the names of its contributors may be used to endorse or promote |
| 24 |
products derived from this software without specific prior written |
| 25 |
permission. |
| 26 |
|
| 27 |
|
| 28 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 29 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 30 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 31 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 32 |
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 33 |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 34 |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 35 |
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 36 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 37 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 38 |
POSSIBILITY OF SUCH DAMAGE. |
| 39 |
===========================================================================*/ |
| 40 |
|
| 41 |
//--------------------------------------------------------------------------- |
| 42 |
// Pragmas |
| 43 |
//--------------------------------------------------------------------------- |
| 44 |
#pragma once |
| 45 |
|
| 46 |
//--------------------------------------------------------------------------- |
| 47 |
// Forward Declarations |
| 48 |
//--------------------------------------------------------------------------- |
| 49 |
struct sProtocolBuffer; |
| 50 |
|
| 51 |
//--------------------------------------------------------------------------- |
| 52 |
// Definitions |
| 53 |
//--------------------------------------------------------------------------- |
| 54 |
|
| 55 |
// Maximum size of a shared buffer |
| 56 |
const ULONG MAX_SHARED_BUFFER_SIZE = 1024 * 16 + 256; |
| 57 |
|
| 58 |
//--------------------------------------------------------------------------- |
| 59 |
// Pragmas (pack structs) |
| 60 |
//--------------------------------------------------------------------------- |
| 61 |
#pragma pack( push, 1 ) |
| 62 |
|
| 63 |
/*=========================================================================*/ |
| 64 |
// Struct sSharedBuffer |
| 65 |
// |
| 66 |
// Simple struct to represent a reference counted shareable (no copy) |
| 67 |
// buffer, as the basis for all buffer related classes |
| 68 |
// |
| 69 |
// NOTE: Do *NOT* create instances of this structure on the stack, it |
| 70 |
// must be dynamically allocated in order to function correctly |
| 71 |
/*=========================================================================*/ |
| 72 |
struct sSharedBuffer |
| 73 |
{ |
| 74 |
public: |
| 75 |
// Constructor (copy passed in buffer) |
| 76 |
sSharedBuffer( |
| 77 |
const BYTE * pDataToCopy, |
| 78 |
ULONG dataLen, |
| 79 |
ULONG dataType ); |
| 80 |
|
| 81 |
// Constructor (assume ownership of passed in buffer) |
| 82 |
sSharedBuffer( |
| 83 |
ULONG dataLen, |
| 84 |
PBYTE pDataToOwn, |
| 85 |
ULONG dataType ); |
| 86 |
|
| 87 |
// Destructor |
| 88 |
virtual ~sSharedBuffer(); |
| 89 |
|
| 90 |
// Equality operator |
| 91 |
bool operator == ( const sSharedBuffer & ) const; |
| 92 |
|
| 93 |
// Inequality operator |
| 94 |
bool operator != ( const sSharedBuffer & ) const; |
| 95 |
|
| 96 |
// (Inline) Get buffer |
| 97 |
const BYTE * GetBuffer() const |
| 98 |
{ |
| 99 |
return mpData; |
| 100 |
}; |
| 101 |
|
| 102 |
// (Inline) Get buffer size |
| 103 |
ULONG GetSize() const |
| 104 |
{ |
| 105 |
return mSize; |
| 106 |
}; |
| 107 |
|
| 108 |
// (Inline) Get buffer type |
| 109 |
ULONG GetType() const |
| 110 |
{ |
| 111 |
return mType; |
| 112 |
}; |
| 113 |
|
| 114 |
// (Inline) Is this buffer valid? |
| 115 |
bool IsValid() const |
| 116 |
{ |
| 117 |
return (mpData != 0 && IsValidSize( mSize )); |
| 118 |
}; |
| 119 |
|
| 120 |
// (Inline) Get reference count |
| 121 |
ULONG GetRefCount() const |
| 122 |
{ |
| 123 |
return mRefCount; |
| 124 |
}; |
| 125 |
|
| 126 |
// (Static Inline) Is the passed in size within the allowable range |
| 127 |
// a shared buffer? |
| 128 |
static bool IsValidSize( ULONG sz ) |
| 129 |
{ |
| 130 |
return (sz > 0 && sz <= MAX_SHARED_BUFFER_SIZE); |
| 131 |
}; |
| 132 |
|
| 133 |
protected: |
| 134 |
// Add reference |
| 135 |
void AddRef(); |
| 136 |
|
| 137 |
// Release reference, delete if reference count zero |
| 138 |
void Release(); |
| 139 |
|
| 140 |
/* Data */ |
| 141 |
PBYTE mpData; |
| 142 |
|
| 143 |
/* Size of data */ |
| 144 |
ULONG mSize; |
| 145 |
|
| 146 |
/* Type of data */ |
| 147 |
ULONG mType; |
| 148 |
|
| 149 |
/* Reference count */ |
| 150 |
ULONG mRefCount; |
| 151 |
|
| 152 |
private: |
| 153 |
// Leave copy constructor and assignment operator unimplemented |
| 154 |
// to prevent unintentional and unauthorized copying of the object |
| 155 |
// (which would lead to bad reference counting) |
| 156 |
sSharedBuffer( const sSharedBuffer & ); |
| 157 |
sSharedBuffer & operator = ( const sSharedBuffer & ); |
| 158 |
|
| 159 |
friend struct sProtocolBuffer; |
| 160 |
}; |
| 161 |
|
| 162 |
//--------------------------------------------------------------------------- |
| 163 |
// Pragmas |
| 164 |
//--------------------------------------------------------------------------- |
| 165 |
#pragma pack( pop ) |
| 166 |
|