* 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:
@@ -26,6 +26,7 @@
|
|||||||
|
|
||||||
#include <image.h>
|
#include <image.h>
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
using namespace BPrivate;
|
using namespace BPrivate;
|
||||||
@@ -210,9 +211,15 @@ malloc(size_t size)
|
|||||||
static processHeap *pHeap = getAllocator();
|
static processHeap *pHeap = getAllocator();
|
||||||
|
|
||||||
void *addr = pHeap->getHeap(pHeap->getHeapIndex()).malloc(size);
|
void *addr = pHeap->getHeap(pHeap->getHeapIndex()).malloc(size);
|
||||||
|
if (addr == NULL) {
|
||||||
|
errno = B_NO_MEMORY;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
#if HEAP_LEAK_CHECK
|
#if HEAP_LEAK_CHECK
|
||||||
add_address(addr, size);
|
add_address(addr, size);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return addr;
|
return addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,6 +229,10 @@ calloc(size_t nelem, size_t elsize)
|
|||||||
{
|
{
|
||||||
static processHeap *pHeap = getAllocator();
|
static processHeap *pHeap = getAllocator();
|
||||||
void *ptr = pHeap->getHeap(pHeap->getHeapIndex()).malloc(nelem * elsize);
|
void *ptr = pHeap->getHeap(pHeap->getHeapIndex()).malloc(nelem * elsize);
|
||||||
|
if (ptr == NULL) {
|
||||||
|
errno = B_NO_MEMORY;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
#if HEAP_LEAK_CHECK
|
#if HEAP_LEAK_CHECK
|
||||||
add_address(ptr, nelem * elsize);
|
add_address(ptr, nelem * elsize);
|
||||||
@@ -250,6 +261,11 @@ memalign(size_t alignment, size_t size)
|
|||||||
{
|
{
|
||||||
static processHeap *pHeap = getAllocator();
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
#if HEAP_LEAK_CHECK
|
#if HEAP_LEAK_CHECK
|
||||||
add_address(addr, size);
|
add_address(addr, size);
|
||||||
#endif
|
#endif
|
||||||
@@ -286,8 +302,8 @@ realloc(void *ptr, size_t size)
|
|||||||
// Allocate a new block of size sz.
|
// Allocate a new block of size sz.
|
||||||
void *buffer = malloc(size);
|
void *buffer = malloc(size);
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
// Allocation failed, free old block and return
|
// Allocation failed, leave old block and return
|
||||||
free(ptr);
|
errno = B_NO_MEMORY;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user