[haiku-commits] Re: haiku: hrev43455 - src/system/kernel/messaging

  • From: "Ingo Weinhold" <ingo_weinhold@xxxxxx>
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Sat, 10 Dec 2011 22:03:10 +0100

mmlr@xxxxxxxx wrote:
> hrev43455 adds 1 changeset to branch 'master'
> old head: 268ddbd76f963f8abca784bfa33f43d779181c72
> new head: d0aa07489c022f98519a79cbd5ceea5ac060d4d6
> 
> ----------------------------------------------------------------------------
> 
> d0aa074: Ensure proper alignment instead of just checking for it.
>  
>  * If there is an alignment requirement then better use memalign() to
>  make sure that it is met.

Was there an actual problem you noticed? As per specification the pointer 
returned by malloc() is aligned to be suitable for any type of object. The 
assumption of the KMessage code that this is at least a multiple of 4 is rather 
safe, I'd say.

>  * Since the BMessageAdapter possibly sets a buffer directly, make a
>  properly aligned copy of the buffer if it happens to be misaligned.

While that doesn't harm, it would be even better, if BMessageAdapter used an 
aligned buffer in the first place, so the copy is avoided.


In hrev43461:

diff --git a/src/system/kernel/messaging/KMessage.cpp 
b/src/system/kernel/messaging/KMessage.cpp
index d4dbf03..8f48d3d 100644
--- a/src/system/kernel/messaging/KMessage.cpp
+++ b/src/system/kernel/messaging/KMessage.cpp
@@ -6,8 +6,6 @@
 
 #include <util/KMessage.h>
 
-#include <malloc.h>
- // for memalign()
 #include <stdlib.h>
 #include <string.h>
 
@@ -610,8 +608,7 @@ KMessage::ReceiveFrom(port_id fromPort, bigtime_t timeout,
 return error;
 
 // allocate a buffer
- uint8* buffer = (uint8*)memalign(kMessageBufferAlignment,
- messageInfo->size);
+ uint8* buffer = (uint8*)malloc(_Align(messageInfo->size));
 if (!buffer)
 return B_NO_MEMORY;
 
@@ -830,7 +827,7 @@ KMessage::_InitFromBuffer(bool sizeFromBuffer)
 fBufferCapacity = size;
 }
 
- void* buffer = memalign(kMessageBufferAlignment, fBufferCapacity);
+ void* buffer = malloc(_Align(fBufferCapacity));
 if (buffer == NULL)
 return B_NO_MEMORY;
 
@@ -964,7 +961,7 @@ KMessage::_AllocateSpace(int32 size, bool alignAddress, 
bool alignSize,
 // reallocate if necessary
 if (fBuffer == &fHeader) {
 int32 newCapacity = _CapacityFor(newSize);
- void* newBuffer = memalign(kMessageBufferAlignment, newCapacity);
+ void* newBuffer = malloc(_Align(newCapacity));
 if (!newBuffer)
 return B_NO_MEMORY;
 fBuffer = newBuffer;

I don't quite get that. Why align the size passed to malloc()?

CU, Ingo

Other related posts: