* Added flag KMESSAGE_CLONE_BUFFER, which will cause buffer passed to SetTo()

to be cloned.
* Added "flags" parameter to the SetTo(const void*,...) version.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34155 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold 2009-11-20 13:52:23 +00:00
parent aea141f906
commit 0d1fab522b
2 changed files with 38 additions and 7 deletions

View File

@ -29,7 +29,8 @@ public:
enum { enum {
KMESSAGE_OWNS_BUFFER = 0x01, KMESSAGE_OWNS_BUFFER = 0x01,
KMESSAGE_INIT_FROM_BUFFER = 0x02, KMESSAGE_INIT_FROM_BUFFER = 0x02,
KMESSAGE_READ_ONLY = 0x04, KMESSAGE_CLONE_BUFFER = 0x04,
KMESSAGE_READ_ONLY = 0x08,
KMESSAGE_FLAG_MASK = 0x07, KMESSAGE_FLAG_MASK = 0x07,
}; };
@ -42,8 +43,10 @@ public:
status_t SetTo(uint32 what, uint32 flags = 0); status_t SetTo(uint32 what, uint32 flags = 0);
status_t SetTo(void* buffer, int32 bufferSize, status_t SetTo(void* buffer, int32 bufferSize,
uint32 what, uint32 flags = 0); uint32 what, uint32 flags = 0);
status_t SetTo(const void* buffer, status_t SetTo(const void* buffer, int32 bufferSize = -1,
int32 bufferSize = -1); uint32 flags = 0);
// KMESSAGE_INIT_FROM_BUFFER and
// KMESSAGE_READ_ONLY are implied
void Unset(); void Unset();
void SetWhat(uint32 what); void SetWhat(uint32 what);

View File

@ -177,8 +177,16 @@ KMessage::SetTo(void* buffer, int32 bufferSize, uint32 what, uint32 flags)
return B_BAD_VALUE; return B_BAD_VALUE;
// if read-only, we need to init from the buffer, too // if read-only, we need to init from the buffer, too
if (flags & KMESSAGE_READ_ONLY && !(flags & KMESSAGE_INIT_FROM_BUFFER)) if ((flags & KMESSAGE_READ_ONLY) != 0
&& (flags & KMESSAGE_INIT_FROM_BUFFER) == 0) {
return B_BAD_VALUE; return B_BAD_VALUE;
}
// if not initializing from the given buffer, cloning it doesn't make sense
if ((flags & KMESSAGE_INIT_FROM_BUFFER) == 0
&& (flags & KMESSAGE_CLONE_BUFFER) != 0) {
return B_BAD_VALUE;
}
fBuffer = buffer; fBuffer = buffer;
fBufferCapacity = bufferSize; fBufferCapacity = bufferSize;
@ -198,10 +206,10 @@ KMessage::SetTo(void* buffer, int32 bufferSize, uint32 what, uint32 flags)
status_t status_t
KMessage::SetTo(const void* buffer, int32 bufferSize) KMessage::SetTo(const void* buffer, int32 bufferSize, uint32 flags)
{ {
return SetTo(const_cast<void*>(buffer), bufferSize, 0, return SetTo(const_cast<void*>(buffer), bufferSize, 0,
KMESSAGE_INIT_FROM_BUFFER | KMESSAGE_READ_ONLY); KMESSAGE_INIT_FROM_BUFFER | KMESSAGE_READ_ONLY | flags);
} }
@ -805,7 +813,27 @@ KMessage::_AddFieldData(KMessageField* field, const void* data,
status_t status_t
KMessage::_InitFromBuffer(bool sizeFromBuffer) KMessage::_InitFromBuffer(bool sizeFromBuffer)
{ {
if (!fBuffer || _Align(fBuffer) != fBuffer) if (fBuffer == NULL)
return B_BAD_DATA;
// clone the buffer, if requested
if ((fFlags & KMESSAGE_CLONE_BUFFER) != 0) {
if (sizeFromBuffer) {
int32 size = fBufferCapacity;
memcpy(&size, &_Header()->size, 4);
fBufferCapacity = size;
}
void* buffer = malloc(fBufferCapacity);
if (buffer == NULL)
return B_NO_MEMORY;
memcpy(buffer, fBuffer, fBufferCapacity);
fBuffer = buffer;
fFlags &= ~(uint32)(KMESSAGE_READ_ONLY | KMESSAGE_CLONE_BUFFER);
}
if (_Align(fBuffer) != fBuffer)
return B_BAD_DATA; return B_BAD_DATA;
Header* header = _Header(); Header* header = _Header();