Clean up the mess added in hrev43461 and solve it differently.

* Define a MEMALIGN macro that is either just defined to malloc() or
  to the actual memalign() depending on where KMessage is used. We only
  use memalign() inside the kernel and libroot.
* Add a comment to the macro explaining that this allows the use of
  special heap implementations that might return unaligned buffers for
  debugging purposes.
This commit is contained in:
Michael Lotz
2011-12-10 23:20:18 +01:00
parent 828294f3df
commit 44c3f5c188
+17 -3
View File
@@ -36,6 +36,19 @@
#endif
#if !defined(HAIKU_TARGET_PLATFORM_HAIKU) || defined(_BOOT_MODE) \
|| defined(_LOADER_MODE)
# define MEMALIGN(alignment, size) malloc(size)
// Built as part of a build tool or the boot or runtime loader.
#else
# include <malloc.h>
# define MEMALIGN(alignment, size) memalign(alignment, size)
// Built as part of the kernel or userland. Using memalign allows use of
// special heap implementations that might otherwise return unaligned
// buffers for debugging purposes.
#endif
static const int32 kMessageReallocChunkSize = 64;
static const size_t kMessageBufferAlignment = 4;
@@ -608,7 +621,8 @@ KMessage::ReceiveFrom(port_id fromPort, bigtime_t timeout,
return error;
// allocate a buffer
uint8* buffer = (uint8*)malloc(_Align(messageInfo->size));
uint8* buffer = (uint8*)MEMALIGN(kMessageBufferAlignment,
messageInfo->size);
if (!buffer)
return B_NO_MEMORY;
@@ -827,7 +841,7 @@ KMessage::_InitFromBuffer(bool sizeFromBuffer)
fBufferCapacity = size;
}
void* buffer = malloc(_Align(fBufferCapacity));
void* buffer = MEMALIGN(kMessageBufferAlignment, fBufferCapacity);
if (buffer == NULL)
return B_NO_MEMORY;
@@ -961,7 +975,7 @@ KMessage::_AllocateSpace(int32 size, bool alignAddress, bool alignSize,
// reallocate if necessary
if (fBuffer == &fHeader) {
int32 newCapacity = _CapacityFor(newSize);
void* newBuffer = malloc(_Align(newCapacity));
void* newBuffer = MEMALIGN(kMessageBufferAlignment, newCapacity);
if (!newBuffer)
return B_NO_MEMORY;
fBuffer = newBuffer;