From ed8dc403a9f2849f4c2c281b0d94a6edf6ad8dc7 Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Sun, 12 Aug 2007 16:23:55 +0000 Subject: [PATCH] * Changed realloc() again to leave the old block intact (obviously...) * Check for failed allocations and set errno correspondingly in malloc(), calloc(), memalign() and realloc() git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@21911 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/system/libroot/posix/malloc/wrapper.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/system/libroot/posix/malloc/wrapper.cpp b/src/system/libroot/posix/malloc/wrapper.cpp index 64cf5fdaa6..ca2702dc20 100644 --- a/src/system/libroot/posix/malloc/wrapper.cpp +++ b/src/system/libroot/posix/malloc/wrapper.cpp @@ -26,6 +26,7 @@ #include +#include #include using namespace BPrivate; @@ -210,9 +211,15 @@ malloc(size_t size) static processHeap *pHeap = getAllocator(); void *addr = pHeap->getHeap(pHeap->getHeapIndex()).malloc(size); + if (addr == NULL) { + errno = B_NO_MEMORY; + return NULL; + } + #if HEAP_LEAK_CHECK add_address(addr, size); #endif + return addr; } @@ -222,6 +229,10 @@ calloc(size_t nelem, size_t elsize) { static processHeap *pHeap = getAllocator(); void *ptr = pHeap->getHeap(pHeap->getHeapIndex()).malloc(nelem * elsize); + if (ptr == NULL) { + errno = B_NO_MEMORY; + return NULL; + } #if HEAP_LEAK_CHECK add_address(ptr, nelem * elsize); @@ -250,6 +261,11 @@ memalign(size_t alignment, size_t size) { static processHeap *pHeap = getAllocator(); void *addr = pHeap->getHeap(pHeap->getHeapIndex()).memalign(alignment, size); + if (addr == NULL) { + errno = B_NO_MEMORY; + return NULL; + } + #if HEAP_LEAK_CHECK add_address(addr, size); #endif @@ -286,8 +302,8 @@ realloc(void *ptr, size_t size) // Allocate a new block of size sz. void *buffer = malloc(size); if (buffer == NULL) { - // Allocation failed, free old block and return - free(ptr); + // Allocation failed, leave old block and return + errno = B_NO_MEMORY; return NULL; }