summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2018-11-09 10:55:04 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2018-11-09 10:55:04 +0100
commitac9c1beb361e3c71e0b9b59d38e5022f12c263c6 (patch)
tree0202322d5cf8af813e49fa92dc311d5483636045
parent98c31d3e3659566011dbce1c1d649d7c4ec021e1 (diff)
parent1e0d7e417acd745e5ddd9fb3b8dbb8691945f62f (diff)
downloadbarebox-ac9c1beb361e3c71e0b9b59d38e5022f12c263c6.tar.gz
Merge branch 'for-next/malloc'
-rw-r--r--common/Makefile4
-rw-r--r--common/calloc.c19
-rw-r--r--common/dummy_malloc.c22
-rw-r--r--common/tlsf_malloc.c33
-rw-r--r--lib/libfile.c6
5 files changed, 47 insertions, 37 deletions
diff --git a/common/Makefile b/common/Makefile
index 13920cc..861365b 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -34,8 +34,8 @@ obj-$(CONFIG_GLOBALVAR) += globalvar.o
obj-$(CONFIG_GREGORIAN_CALENDER) += date.o
obj-$(CONFIG_KALLSYMS) += kallsyms.o
obj-$(CONFIG_MALLOC_DLMALLOC) += dlmalloc.o
-obj-$(CONFIG_MALLOC_TLSF) += tlsf_malloc.o tlsf.o
-obj-$(CONFIG_MALLOC_DUMMY) += dummy_malloc.o
+obj-$(CONFIG_MALLOC_TLSF) += tlsf_malloc.o tlsf.o calloc.o
+obj-$(CONFIG_MALLOC_DUMMY) += dummy_malloc.o calloc.o
obj-$(CONFIG_MEMINFO) += meminfo.o
obj-$(CONFIG_MENU) += menu.o
obj-$(CONFIG_MODULES) += module.o
diff --git a/common/calloc.c b/common/calloc.c
new file mode 100644
index 0000000..2b933ec
--- /dev/null
+++ b/common/calloc.c
@@ -0,0 +1,19 @@
+#include <common.h>
+#include <malloc.h>
+
+/*
+ * calloc calls malloc, then zeroes out the allocated chunk.
+ */
+void *calloc(size_t n, size_t elem_size)
+{
+ size_t size = elem_size * n;
+ void *r = malloc(size);
+
+ if (!r)
+ return r;
+
+ memset(r, 0x0, size);
+
+ return r;
+}
+EXPORT_SYMBOL(calloc);
diff --git a/common/dummy_malloc.c b/common/dummy_malloc.c
index 641baa1..0120d9b 100644
--- a/common/dummy_malloc.c
+++ b/common/dummy_malloc.c
@@ -30,11 +30,14 @@ void malloc_stats(void)
void *memalign(size_t alignment, size_t bytes)
{
- unsigned long mem = (unsigned long)sbrk(bytes + alignment);
+ void *mem = sbrk(bytes + alignment);
- mem = (mem + alignment) & ~(alignment - 1);
+ if (!mem) {
+ errno = ENOMEM;
+ return NULL;
+ }
- return (void *)mem;
+ return PTR_ALIGN(mem, alignment);
}
void *malloc(size_t size)
@@ -50,16 +53,3 @@ void *realloc(void *ptr, size_t size)
{
BUG();
}
-
-void *calloc(size_t n, size_t elem_size)
-{
- size_t size = elem_size * n;
- void *r = malloc(size);
-
- if (!r)
- return r;
-
- memset(r, 0x0, size);
-
- return r;
-}
diff --git a/common/tlsf_malloc.c b/common/tlsf_malloc.c
index a3541d8..c8900fc 100644
--- a/common/tlsf_malloc.c
+++ b/common/tlsf_malloc.c
@@ -28,6 +28,7 @@ extern tlsf_pool tlsf_mem_pool;
void *malloc(size_t bytes)
{
+ void *mem;
/*
* tlsf_malloc returns NULL for zero bytes, we instead want
* to have a valid pointer.
@@ -35,25 +36,13 @@ void *malloc(size_t bytes)
if (!bytes)
bytes = 1;
- return tlsf_malloc(tlsf_mem_pool, bytes);
-}
-EXPORT_SYMBOL(malloc);
-
-/*
- * calloc calls malloc, then zeroes out the allocated chunk.
- */
-void *calloc(size_t n, size_t elem_size)
-{
- void *mem;
- size_t sz;
-
- sz = n * elem_size;
- mem = malloc(sz);
- memset(mem, 0, sz);
+ mem = tlsf_malloc(tlsf_mem_pool, bytes);
+ if (!mem)
+ errno = ENOMEM;
return mem;
}
-EXPORT_SYMBOL(calloc);
+EXPORT_SYMBOL(malloc);
void free(void *mem)
{
@@ -63,13 +52,21 @@ EXPORT_SYMBOL(free);
void *realloc(void *oldmem, size_t bytes)
{
- return tlsf_realloc(tlsf_mem_pool, oldmem, bytes);
+ void *mem = tlsf_realloc(tlsf_mem_pool, oldmem, bytes);
+ if (!mem)
+ errno = ENOMEM;
+
+ return mem;
}
EXPORT_SYMBOL(realloc);
void *memalign(size_t alignment, size_t bytes)
{
- return tlsf_memalign(tlsf_mem_pool, alignment, bytes);
+ void *mem = tlsf_memalign(tlsf_mem_pool, alignment, bytes);
+ if (!mem)
+ errno = ENOMEM;
+
+ return mem;
}
EXPORT_SYMBOL(memalign);
diff --git a/lib/libfile.c b/lib/libfile.c
index 39c85b2..8f2aed2 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -185,7 +185,11 @@ again:
goto again;
}
- buf = xzalloc(read_size + 1);
+ buf = calloc(read_size + 1, 1);
+ if (!buf) {
+ ret = -ENOMEM;
+ goto err_out;
+ }
fd = open(filename, O_RDONLY);
if (fd < 0) {