summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2001-10-23 06:01:43 (GMT)
committerH. Peter Anvin <hpa@zytor.com>2001-10-23 06:01:43 (GMT)
commitfbb530110cb5127532ed6ff1081f84abf574c43d (patch)
treefd8ab0a521e7b90729f9e2b28e9bdd796d32e219
parentd15e7744e7ab4cb7fa0ebdd8e8d652674dd95afa (diff)
downloadlpsm-fbb530110cb5127532ed6ff1081f84abf574c43d.zip
lpsm-fbb530110cb5127532ed6ff1081f84abf574c43d.tar.gz
lpsm-fbb530110cb5127532ed6ff1081f84abf574c43d.tar.bz2
lpsm-fbb530110cb5127532ed6ff1081f84abf574c43d.tar.xz
Be a bit more correct: set errno to ENOMEM if we fail to allocate
memory.
-rw-r--r--free.c10
-rw-r--r--malloc.c6
-rw-r--r--realloc.c13
-rw-r--r--zalloc.c4
4 files changed, 24 insertions, 9 deletions
diff --git a/free.c b/free.c
index 627e53f..f38f1db 100644
--- a/free.c
+++ b/free.c
@@ -148,8 +148,10 @@ void lpsm_free_slab(void *ptr)
void lpsm_free(void *ptr)
{
/* Buddy allocations are ALWAYS page-aligned, SLAB allocations never */
- if ( (uintptr_t)ptr & (BUDDY_SIZE_MIN-1) )
- lpsm_free_slab(ptr);
- else
- lpsm_free_buddy(ptr);
+ if ( ptr ) {
+ if ( (uintptr_t)ptr & (BUDDY_SIZE_MIN-1) )
+ lpsm_free_slab(ptr);
+ else
+ lpsm_free_buddy(ptr);
+ }
}
diff --git a/malloc.c b/malloc.c
index e2ae112..d565ebc 100644
--- a/malloc.c
+++ b/malloc.c
@@ -22,6 +22,7 @@
#include <inttypes.h>
#include <limits.h>
#include <string.h>
+#include <errno.h>
#include "lpsm.h"
#include "internals.h"
@@ -190,8 +191,10 @@ void *lpsm_malloc_buddy(size_t size)
order++;
xbit = lpsm_malloc_buddy_xbit(order);
- if ( !xbit )
+ if ( !xbit ) {
+ errno = ENOMEM;
return NULL;
+ }
obit = 1 << (AH->arena_size_lg2 - order);
@@ -268,6 +271,7 @@ void *lpsm_malloc_slab(size_t size)
if ( !sh ) {
/* Empty free list, need a new page */
if ( !(sh = lpsm_make_new_slab(si,index)) )
+ errno = ENOMEM;
return NULL; /* Unavailable to allocate new slab */
}
diff --git a/realloc.c b/realloc.c
index 3dce36e..27b2f1e 100644
--- a/realloc.c
+++ b/realloc.c
@@ -23,6 +23,7 @@
#include <inttypes.h>
#include <limits.h>
#include <string.h>
+#include <errno.h>
#include "lpsm.h"
#include "internals.h"
@@ -43,8 +44,10 @@ static void *lpsm_realloc_buddy(void *ptr, size_t new_size)
if ( order > AH->arena_size_lg2 ) {
/* Need to extend the arena immediately... */
- if ( lpsm_extend_arena(order+1) )
+ if ( lpsm_extend_arena(order+1) ) {
+ errno = ENOMEM;
return NULL;
+ }
}
rorder = AH->arena_size_lg2 - order;
@@ -94,8 +97,10 @@ static void *lpsm_realloc_buddy(void *ptr, size_t new_size)
/* Careful. If lpsm_malloc_buddy resizes the arena we need to
adjust the rorder and xbit values afterwards. */
nptr = lpsm_malloc_buddy(new_size);
- if ( !nptr )
+ if ( !nptr ) {
+ errno = ENOMEM;
return NULL; /* realloc failed */
+ }
memcpy(nptr, ptr, current_size);
@@ -162,8 +167,10 @@ static void *lpsm_realloc_slab(void *ptr, size_t new_size)
if ( new_size > si->size ) {
/* This also covers the possibility of new_size > slab_max */
void *nptr = lpsm_malloc(new_size);
- if ( !nptr )
+ if ( !nptr ) {
+ errno = ENOMEM;
return NULL;
+ }
memcpy(nptr, ptr, si->size);
lpsm_free_slab(ptr);
return nptr;
diff --git a/zalloc.c b/zalloc.c
index b6e5195..f972b16 100644
--- a/zalloc.c
+++ b/zalloc.c
@@ -22,6 +22,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
+#include <errno.h>
#include <sys/mman.h>
#include "lpsm.h"
#include "internals.h"
@@ -38,7 +39,8 @@ void *lpsm_zalloc(size_t size)
xbit = lpsm_malloc_buddy_xbit(size);
if ( !xbit ) {
- ptr = NULL;
+ errno = ENOMEM;
+ return NULL;
} else {
rorder = AH->arena_size_lg2 - order;
obit = 1 << rorder;