1 /*****************************************************************
3 | Neptune - Data Buffer
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
8 | Redistribution and use in source and binary forms, with or without
9 | modification, are permitted provided that the following conditions are met:
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 | * Neither the name of Axiomatic Systems nor the
16 | names of its contributors may be used to endorse or promote products
17 | derived from this software without specific prior written permission.
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 ****************************************************************/
32 /*----------------------------------------------------------------------
34 +---------------------------------------------------------------------*/
35 #include "NptDataBuffer.h"
37 #include "NptResults.h"
39 /*----------------------------------------------------------------------
40 | NPT_DataBuffer::NPT_DataBuffer
41 +---------------------------------------------------------------------*/
42 NPT_DataBuffer::NPT_DataBuffer() :
43 m_BufferIsLocal(true),
50 /*----------------------------------------------------------------------
51 | NPT_DataBuffer::NPT_DataBuffer
52 +---------------------------------------------------------------------*/
53 NPT_DataBuffer::NPT_DataBuffer(NPT_Size bufferSize
) :
54 m_BufferIsLocal(true),
55 m_Buffer(bufferSize
?new NPT_Byte
[bufferSize
]:NULL
),
56 m_BufferSize(bufferSize
),
61 /*----------------------------------------------------------------------
62 | NPT_DataBuffer::NPT_DataBuffer
63 +---------------------------------------------------------------------*/
64 NPT_DataBuffer::NPT_DataBuffer(const void* data
, NPT_Size data_size
, bool copy
) :
65 m_BufferIsLocal(copy
),
66 m_Buffer(copy
?(data_size
?new NPT_Byte
[data_size
]:NULL
):reinterpret_cast<NPT_Byte
*>(const_cast<void*>(data
))),
67 m_BufferSize(data_size
),
70 if (copy
&& data_size
) NPT_CopyMemory(m_Buffer
, data
, data_size
);
73 /*----------------------------------------------------------------------
74 | NPT_DataBuffer::NPT_DataBuffer
75 +---------------------------------------------------------------------*/
76 NPT_DataBuffer::NPT_DataBuffer(const NPT_DataBuffer
& other
) :
77 m_BufferIsLocal(true),
79 m_BufferSize(other
.m_DataSize
),
80 m_DataSize(other
.m_DataSize
)
83 m_Buffer
= new NPT_Byte
[m_BufferSize
];
84 NPT_CopyMemory(m_Buffer
, other
.m_Buffer
, m_BufferSize
);
88 /*----------------------------------------------------------------------
89 | NPT_DataBuffer::~NPT_DataBuffer
90 +---------------------------------------------------------------------*/
91 NPT_DataBuffer::~NPT_DataBuffer()
96 /*----------------------------------------------------------------------
97 | NPT_DataBuffer::Clear
98 +---------------------------------------------------------------------*/
100 NPT_DataBuffer::Clear()
102 if (m_BufferIsLocal
) {
112 /*----------------------------------------------------------------------
113 | NPT_DataBuffer::operator=
114 +---------------------------------------------------------------------*/
116 NPT_DataBuffer::operator=(const NPT_DataBuffer
& copy
)
118 // do nothing if we're assigning to ourselves
122 m_BufferIsLocal
= true;
123 m_BufferSize
= copy
.m_BufferSize
;
124 m_DataSize
= copy
.m_DataSize
;
127 m_Buffer
= new NPT_Byte
[m_BufferSize
];
128 NPT_CopyMemory(m_Buffer
, copy
.m_Buffer
, m_BufferSize
);
134 /*----------------------------------------------------------------------
135 | NPT_DataBuffer::operator==
136 +---------------------------------------------------------------------*/
138 NPT_DataBuffer::operator==(const NPT_DataBuffer
& other
) const
140 // check that the sizes match
141 if (m_DataSize
!= other
.m_DataSize
) return false;
143 return NPT_MemoryEqual(m_Buffer
,
148 /*----------------------------------------------------------------------
149 | NPT_DataBuffer::SetBuffer
150 +---------------------------------------------------------------------*/
152 NPT_DataBuffer::SetBuffer(NPT_Byte
* buffer
, NPT_Size buffer_size
)
156 // we're now using an external buffer
157 m_BufferIsLocal
= false;
159 m_BufferSize
= buffer_size
;
165 /*----------------------------------------------------------------------
166 | NPT_DataBuffer::SetBufferSize
167 +---------------------------------------------------------------------*/
169 NPT_DataBuffer::SetBufferSize(NPT_Size buffer_size
)
171 if (m_BufferIsLocal
) {
172 return ReallocateBuffer(buffer_size
);
174 return NPT_ERROR_NOT_SUPPORTED
; // you cannot change the
175 // buffer management mode
179 /*----------------------------------------------------------------------
180 | NPT_DataBuffer::Reserve
181 +---------------------------------------------------------------------*/
183 NPT_DataBuffer::Reserve(NPT_Size size
)
185 if (size
<= m_BufferSize
) return NPT_SUCCESS
;
187 // try doubling the buffer to accomodate for the new size
188 NPT_Size new_size
= m_BufferSize
*2;
189 if (new_size
< size
) new_size
= size
;
190 return SetBufferSize(new_size
);
193 /*----------------------------------------------------------------------
194 | NPT_DataBuffer::SetDataSize
195 +---------------------------------------------------------------------*/
197 NPT_DataBuffer::SetDataSize(NPT_Size size
)
199 if (size
> m_BufferSize
) {
200 // the buffer is too small, we need to reallocate it
201 if (m_BufferIsLocal
) {
202 NPT_CHECK(ReallocateBuffer(size
));
204 // we cannot reallocate an external buffer
205 return NPT_ERROR_NOT_SUPPORTED
;
212 /*----------------------------------------------------------------------
213 | NPT_DataBuffer::SetData
214 +---------------------------------------------------------------------*/
216 NPT_DataBuffer::SetData(const NPT_Byte
* data
, NPT_Size size
)
218 if (size
> m_BufferSize
) {
219 if (m_BufferIsLocal
) {
220 NPT_CHECK(ReallocateBuffer(size
));
222 return NPT_ERROR_INVALID_STATE
;
225 if (data
) NPT_CopyMemory(m_Buffer
, data
, size
);
231 /*----------------------------------------------------------------------
232 | NPT_DataBuffer::ReallocateBuffer
233 +---------------------------------------------------------------------*/
235 NPT_DataBuffer::ReallocateBuffer(NPT_Size size
)
237 // check that the existing data fits
238 if (m_DataSize
> size
) return NPT_ERROR_INVALID_PARAMETERS
;
240 // allocate a new buffer only if size is not zero
241 if (!size
) return NPT_ERROR_INVALID_PARAMETERS
;
243 NPT_Byte
* newBuffer
= new NPT_Byte
[size
];
245 // copy the contents of the previous buffer, if any
246 if (m_Buffer
&& m_DataSize
) {
247 NPT_CopyMemory(newBuffer
, m_Buffer
, m_DataSize
);
250 // destroy the previous buffer
253 // use the new buffer
254 m_Buffer
= newBuffer
;