* 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
This commit is contained in:
Michael Lotz 2007-08-12 16:23:55 +00:00
parent 5a626bd963
commit ed8dc403a9

View File

@ -26,6 +26,7 @@
#include <image.h>
#include <errno.h>
#include <string.h>
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;
}