summaryrefslogtreecommitdiff
path: root/libc/stdlib/malloc-930716/malloc.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2003-09-06 11:49:28 +0000
committerEric Andersen <andersen@codepoet.org>2003-09-06 11:49:28 +0000
commitcd34085f2c93441324f86172613a20cd899bddea (patch)
tree8a4a3dcc3b092904ea5f93ec49e600cc451d42bb /libc/stdlib/malloc-930716/malloc.c
parentb9c0292271ea5e4dc583137d6c74dda605d1ae04 (diff)
Fix errno values. Fix MALLOC_GLIBC_COMPAT handling in malloc/malloc.c,
which was reversed. Provide more consistancy between implementations. Handle it when people do stupid things like malloc(-1);
Diffstat (limited to 'libc/stdlib/malloc-930716/malloc.c')
-rw-r--r--libc/stdlib/malloc-930716/malloc.c22
1 files changed, 15 insertions, 7 deletions
diff --git a/libc/stdlib/malloc-930716/malloc.c b/libc/stdlib/malloc-930716/malloc.c
index dce32b90b..3e69f1140 100644
--- a/libc/stdlib/malloc-930716/malloc.c
+++ b/libc/stdlib/malloc-930716/malloc.c
@@ -15,6 +15,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <errno.h>
#include "malloc.h"
#ifdef __UCLIBC_HAS_THREADS__
@@ -161,19 +162,22 @@ void * __malloc_unlocked (size_t size)
struct list *next;
#if defined(__MALLOC_GLIBC_COMPAT__)
- if (size == 0)
+ if (unlikely(size == 0))
size++;
#else
/* Some programs will call malloc (0). Lets be strict and return NULL */
- if (size == 0)
- return NULL;
+ if (unlikely(size == 0))
+ goto oom;
#endif
+ /* Check if they are doing something dumb like malloc(-1) */
+ if (unlikely(((unsigned long)size > (unsigned long)(sizeof (struct list)*-2))))
+ goto oom;
- if (size < sizeof (struct list))
+ if (unlikely(size < sizeof (struct list)))
size = sizeof (struct list);
if (!initialized && !initialize()) {
- return NULL;
+ goto oom;
}
/* Determine the allocation policy based on the request size. */
@@ -204,7 +208,7 @@ void * __malloc_unlocked (size_t size)
and break it into fragments, returning the first. */
result = __malloc_unlocked(BLOCKSIZE);
if (!result) {
- return NULL;
+ goto oom;
}
++_fragblocks[log];
@@ -255,7 +259,7 @@ void * __malloc_unlocked (size_t size)
}
result = morecore(blocks * BLOCKSIZE);
if (!result) {
- return NULL;
+ goto oom;
}
block = BLOCK(result);
_heapinfo[block].busy.type = 0;
@@ -293,6 +297,10 @@ void * __malloc_unlocked (size_t size)
}
return result;
+
+oom:
+ __set_errno(ENOMEM);
+ return NULL;
}
/* Return memory to the heap. */