- 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:
@@ -14,8 +14,10 @@
|
|||||||
|
|
||||||
class DynamicBuffer {
|
class DynamicBuffer {
|
||||||
public:
|
public:
|
||||||
DynamicBuffer(size_t initialSize = 0);
|
DynamicBuffer(size_t initialSize = 1);
|
||||||
~DynamicBuffer();
|
~DynamicBuffer();
|
||||||
|
|
||||||
|
DynamicBuffer(const DynamicBuffer& buffer);
|
||||||
|
|
||||||
// InitCheck() should be called to guarantee the object initialization
|
// InitCheck() should be called to guarantee the object initialization
|
||||||
// didn't fail. If it does not return B_OK, the initialization failed.
|
// didn't fail. If it does not return B_OK, the initialization failed.
|
||||||
@@ -47,7 +49,7 @@ public:
|
|||||||
void PrintToStream();
|
void PrintToStream();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
status_t _GrowToFit(size_t size);
|
status_t _GrowToFit(size_t size, bool exact = false);
|
||||||
|
|
||||||
unsigned char* fBuffer;
|
unsigned char* fBuffer;
|
||||||
size_t fBufferSize;
|
size_t fBufferSize;
|
||||||
|
|||||||
@@ -8,9 +8,12 @@
|
|||||||
|
|
||||||
#include "DynamicBuffer.h"
|
#include "DynamicBuffer.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#include <Errors.h>
|
#include <Errors.h>
|
||||||
#include <SupportDefs.h>
|
#include <SupportDefs.h>
|
||||||
|
|
||||||
|
#include <new>
|
||||||
|
|
||||||
DynamicBuffer::DynamicBuffer(size_t initialSize) :
|
DynamicBuffer::DynamicBuffer(size_t initialSize) :
|
||||||
fBuffer(NULL),
|
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
|
status_t
|
||||||
DynamicBuffer::InitCheck() const
|
DynamicBuffer::InitCheck() const
|
||||||
{
|
{
|
||||||
@@ -114,12 +137,16 @@ DynamicBuffer::PrintToStream()
|
|||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
DynamicBuffer::_GrowToFit(size_t size)
|
DynamicBuffer::_GrowToFit(size_t size, bool exact)
|
||||||
{
|
{
|
||||||
if (size <= fBufferSize - fDataEnd)
|
if (size <= fBufferSize - fDataEnd)
|
||||||
return B_OK;
|
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];
|
unsigned char* newBuffer = new (std::nothrow) unsigned char[newSize];
|
||||||
if (newBuffer == NULL)
|
if (newBuffer == NULL)
|
||||||
|
|||||||
Reference in New Issue
Block a user