From dfd5242e7f6ce0a7af67e5d3e7ff2927c7acdcff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Thu, 16 Aug 2007 10:04:44 +0000 Subject: [PATCH] Even though Korli beat me, I've updated the posix_memalign() function to be more standards compliant: * it now checks the alignment is a multiple of 4 (needs to be changed for 64 bit architectures) * it no longer sets errno. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@21975 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/posix/stdlib.h | 8 +++--- src/system/libroot/posix/malloc/wrapper.cpp | 28 ++++++++++++++------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/headers/posix/stdlib.h b/headers/posix/stdlib.h index 05f96b6c14..7db19888ea 100644 --- a/headers/posix/stdlib.h +++ b/headers/posix/stdlib.h @@ -1,5 +1,5 @@ /* - * Copyright 2002-2006, Haiku Inc. All Rights Reserved. + * Copyright 2002-2007, Haiku Inc. All Rights Reserved. * Distributed under the terms of the MIT License. */ #ifndef _STDLIB_H_ @@ -45,11 +45,11 @@ extern "C" { #endif /* memory allocation (see malloc.h for additional defines & prototypes) */ -extern void *calloc(size_t nmemb, size_t size); +extern void *calloc(size_t numElements, size_t size); extern void free(void *pointer); extern void *malloc(size_t size); -extern void *realloc(void * ptr, size_t size); -extern int posix_memalign(void **memptr, size_t alignment, size_t size); +extern int posix_memalign(void **_pointer, size_t alignment, size_t size); +extern void *realloc(void *oldPointer, size_t newSize); /* process termination */ extern void abort(void); diff --git a/src/system/libroot/posix/malloc/wrapper.cpp b/src/system/libroot/posix/malloc/wrapper.cpp index 7ef6393faf..a926e1f563 100644 --- a/src/system/libroot/posix/malloc/wrapper.cpp +++ b/src/system/libroot/posix/malloc/wrapper.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2002-2006, Haiku Inc. + * Copyright 2002-2007, Haiku Inc. * Distributed under the terms of the MIT License. */ @@ -260,7 +260,8 @@ extern "C" void * memalign(size_t alignment, size_t size) { static processHeap *pHeap = getAllocator(); - void *addr = pHeap->getHeap(pHeap->getHeapIndex()).memalign(alignment, size); + void *addr = pHeap->getHeap(pHeap->getHeapIndex()).memalign(alignment, + size); if (addr == NULL) { errno = B_NO_MEMORY; return NULL; @@ -275,14 +276,23 @@ memalign(size_t alignment, size_t size) extern "C" int -posix_memalign(void **memptr, size_t alignment, size_t size) +posix_memalign(void **_pointer, size_t alignment, size_t size) { - if (!memptr) - return EINVAL; - *memptr = memalign(alignment, size); - if (memptr == NULL) - return ENOMEM; - return 0; + if ((alignment & 3) != 0 || _pointer == NULL) + return B_BAD_VALUE; + + static processHeap *pHeap = getAllocator(); + void *pointer = pHeap->getHeap(pHeap->getHeapIndex()).memalign(alignment, + size); + if (pointer == NULL) + return NULL; + +#if HEAP_LEAK_CHECK + add_address(pointer, size); +#endif + + *_pointer = pointer; + return 0; }