From 44c3f5c18843a98ba881efb7e0d71145db705b31 Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Sat, 10 Dec 2011 23:13:01 +0100 Subject: [PATCH] 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. --- src/system/kernel/messaging/KMessage.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/system/kernel/messaging/KMessage.cpp b/src/system/kernel/messaging/KMessage.cpp index 8f48d3d93a..12127d9780 100644 --- a/src/system/kernel/messaging/KMessage.cpp +++ b/src/system/kernel/messaging/KMessage.cpp @@ -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 +# 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;