X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FPacket.cc;h=6923e7cb06e5d4a507ec1d8cbe2064e784503a4b;hp=8862f29bc9ba918ef23fb4608d539681fbb4f90a;hb=264bdbb09bc86797f1f80d151ac408cb780b9355;hpb=19d555013569026c9e68784ea560cd2e5a21bc5e diff --git a/src/Moof/Packet.cc b/src/Moof/Packet.cc index 8862f29..6923e7c 100644 --- a/src/Moof/Packet.cc +++ b/src/Moof/Packet.cc @@ -103,9 +103,7 @@ Packet::Packet(size_t size) : mBoolR(0), mBoolW(0), mBoolNumR(0), - mBoolNumW(0) -{ -} + mBoolNumW(0) {} Packet::Packet(const char* data, size_t size) : mBuffer((char*)malloc(size)), @@ -117,7 +115,36 @@ Packet::Packet(const char* data, size_t size) : mBoolNumR(0), mBoolNumW(0) { - memcpy(mBuffer, data, size); + if (mBuffer) memcpy(mBuffer, data, size); +} + + +Packet::Packet(const Packet& copy) : + mBuffer((char*)malloc(copy.mSize)), + mSize(copy.mSize), + mR(copy.mR), + mW(copy.mW), + mBoolR(copy.mBoolR), + mBoolW(copy.mBoolW), + mBoolNumR(copy.mBoolNumR), + mBoolNumW(copy.mBoolNumW) +{ + if (mBuffer) memcpy(mBuffer, copy.mBuffer, mSize); +} + +Packet& Packet::operator=(const Packet& copy) +{ + free(mBuffer); + + mBuffer = (char*)malloc(copy.mSize); + mSize = copy.mSize; + mR = copy.mR; + mW = copy.mW; + mBoolR = copy.mBoolR; + mBoolW = copy.mBoolW; + mBoolNumR = copy.mBoolNumR; + mBoolNumW = copy.mBoolNumW; + return *this; } @@ -216,14 +243,16 @@ size_t Packet::write(const void* bytes, size_t size) size_t nBytes = std::min(size, mSize - mW); if (!mBuffer || nBytes < size) { - int numPages = 1 + size / PAGE_SIZE; - int newSize = mSize + numPages * PAGE_SIZE; + int nPages = 1 + size / PAGE_SIZE; + int newSize = mSize + nPages * PAGE_SIZE; char* newBuffer = (char*)realloc(mBuffer, newSize); if (newBuffer) { mBuffer = newBuffer; mSize = newSize; + nBytes = size; } + if (!mBuffer) return 0; } memcpy(&mBuffer[mW], bytes, nBytes); mW += nBytes; @@ -322,5 +351,16 @@ size_t Packet::read(void* bytes, size_t size) } +void Packet::clear() +{ + mR = 0; + mW = 0; + mBoolR = 0; + mBoolW = 0; + mBoolNumR = 0; + mBoolNumW = 0; +} + + } // namespace Mf