- Got it to actually compile.

- Default buffer size is now set to 1 instead of 0, which would cause the
  construction without a given size to fail.
- Added copy constructor.
- Changed _GrowToFit() to have a boolean parameter to indicate if we want
  to resize the buffer to an exact size. Used by the copy constructor. 



git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28924 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Bruno G. Albuquerque 2009-01-17 15:56:40 +00:00
parent 4e034d2ab3
commit a12096e5c7
2 changed files with 34 additions and 5 deletions

View File

@ -14,8 +14,10 @@
class DynamicBuffer {
public:
DynamicBuffer(size_t initialSize = 0);
DynamicBuffer(size_t initialSize = 1);
~DynamicBuffer();
DynamicBuffer(const DynamicBuffer& buffer);
// InitCheck() should be called to guarantee the object initialization
// didn't fail. If it does not return B_OK, the initialization failed.
@ -47,7 +49,7 @@ public:
void PrintToStream();
private:
status_t _GrowToFit(size_t size);
status_t _GrowToFit(size_t size, bool exact = false);
unsigned char* fBuffer;
size_t fBufferSize;

View File

@ -8,9 +8,12 @@
#include "DynamicBuffer.h"
#include <stdio.h>
#include <Errors.h>
#include <SupportDefs.h>
#include <new>
DynamicBuffer::DynamicBuffer(size_t initialSize) :
fBuffer(NULL),
@ -38,6 +41,26 @@ DynamicBuffer::~DynamicBuffer()
}
DynamicBuffer::DynamicBuffer(const DynamicBuffer& buffer) :
fBuffer(NULL),
fBufferSize(0),
fDataStart(0),
fDataEnd(0),
fInit(B_NO_INIT)
{
fInit = buffer.fInit;
if (fInit == B_OK) {
status_t result = _GrowToFit(buffer.fBufferSize, true);
if (result == B_OK) {
memcpy(fBuffer, buffer.fBuffer, fBufferSize);
fDataStart = buffer.fDataStart;
fDataEnd = buffer.fDataEnd;
} else
fInit = result;
}
}
status_t
DynamicBuffer::InitCheck() const
{
@ -114,12 +137,16 @@ DynamicBuffer::PrintToStream()
status_t
DynamicBuffer::_GrowToFit(size_t size)
DynamicBuffer::_GrowToFit(size_t size, bool exact)
{
if (size <= fBufferSize - fDataEnd)
return B_OK;
size_t newSize = (fBufferSize + size) * 2;
size_t newSize;
if (!exact)
newSize = (fBufferSize + size) * 2;
else
newSize = size;
unsigned char* newBuffer = new (std::nothrow) unsigned char[newSize];
if (newBuffer == NULL)