mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] sandbox: libc_malloc: populate barebox errno on error
@ 2023-11-22 17:00 Ahmad Fatoum
  2023-11-22 17:00 ` [PATCH 2/2] dlmalloc: populate " Ahmad Fatoum
  2023-11-23  7:20 ` [PATCH 1/2] sandbox: libc_malloc: populate barebox " Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2023-11-22 17:00 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

TLSF already populates errno on errors, so do likewise for the
allocators that don't.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/sandbox/os/libc_malloc.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/arch/sandbox/os/libc_malloc.c b/arch/sandbox/os/libc_malloc.c
index 74e3e2680585..975c41b0ec49 100644
--- a/arch/sandbox/os/libc_malloc.c
+++ b/arch/sandbox/os/libc_malloc.c
@@ -6,18 +6,30 @@
 #include <stdlib.h>
 #include <malloc.h>
 
+#define BAREBOX_ENOMEM 12
+extern int barebox_errno;
+
 void barebox_malloc_stats(void)
 {
 }
 
 void *barebox_memalign(size_t alignment, size_t bytes)
 {
-	return memalign(alignment, bytes);
+	void *mem = memalign(alignment, bytes);
+	if (!mem)
+		barebox_errno = BAREBOX_ENOMEM;
+
+	return mem;
 }
 
 void *barebox_malloc(size_t size)
 {
-	return malloc(size);
+
+	void *mem = malloc(size);
+	if (!mem)
+		barebox_errno = BAREBOX_ENOMEM;
+
+	return mem;
 }
 
 void barebox_free(void *ptr)
@@ -27,10 +39,18 @@ void barebox_free(void *ptr)
 
 void *barebox_realloc(void *ptr, size_t size)
 {
-	return realloc(ptr, size);
+	void *mem = realloc(ptr, size);
+	if (!mem)
+		barebox_errno = BAREBOX_ENOMEM;
+
+	return mem;
 }
 
 void *barebox_calloc(size_t n, size_t elem_size)
 {
-	return calloc(n, elem_size);
+	void *mem = calloc(n, elem_size);
+	if (!mem)
+		barebox_errno = BAREBOX_ENOMEM;
+
+	return mem;
 }
-- 
2.39.2




^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] dlmalloc: populate errno on error
  2023-11-22 17:00 [PATCH 1/2] sandbox: libc_malloc: populate barebox errno on error Ahmad Fatoum
@ 2023-11-22 17:00 ` Ahmad Fatoum
  2023-11-23  7:20 ` [PATCH 1/2] sandbox: libc_malloc: populate barebox " Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2023-11-22 17:00 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

TLSF already populates errno on errors, so do likewise for the
allocators that don't.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/dlmalloc.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/common/dlmalloc.c b/common/dlmalloc.c
index c634fb1997a5..c41487d54b4a 100644
--- a/common/dlmalloc.c
+++ b/common/dlmalloc.c
@@ -1,5 +1,6 @@
 
 #include <config.h>
+#include <errno.h>
 #include <malloc.h>
 #include <string.h>
 #include <memory.h>
@@ -1167,8 +1168,10 @@ void *malloc(size_t bytes)
 
 	INTERNAL_SIZE_T nb;
 
-	if ((long) bytes < 0)
+	if ((long) bytes < 0) {
+		errno = ENOMEM;
 		return NULL;
+	}
 
 	nb = request2size(bytes); /* padded request size; */
 
@@ -1322,8 +1325,10 @@ void *malloc(size_t bytes)
 	if ((remainder_size = chunksize (top) - nb) < (long) MINSIZE) {
 		/* Try to extend */
 		malloc_extend_top(nb);
-		if ((remainder_size = chunksize(top) - nb) < (long) MINSIZE)
+		if ((remainder_size = chunksize(top) - nb) < (long) MINSIZE) {
+			errno = ENOMEM;
 			return NULL;	/* propagate failure */
+		}
 	}
 
 	victim = top;
@@ -1487,8 +1492,10 @@ void *realloc(void *oldmem, size_t bytes)
 	}
 #endif
 
-	if ((long)bytes < 0)
+	if ((long)bytes < 0) {
+		errno = ENOMEM;
 		return NULL;
+	}
 
 	/* realloc of null is supposed to be same as malloc */
 	if (!oldmem)
@@ -1654,8 +1661,10 @@ void *memalign(size_t alignment, size_t bytes)
 	mchunkptr remainder;	/* spare room at end to split off */
 	long remainder_size;	/* its size */
 
-	if ((long) bytes < 0)
+	if ((long) bytes < 0) {
+		errno = ENOMEM;
 		return NULL;
+	}
 
 	/* If need less alignment than we give anyway, just relay to malloc */
 
@@ -1737,8 +1746,10 @@ void *calloc(size_t n, size_t elem_size)
 	mchunkptr oldtop = top;
 	INTERNAL_SIZE_T oldtopsize = chunksize(top);
 
-	if ((long)n < 0)
+	if ((long)n < 0) {
+		errno = ENOMEM;
 		return NULL;
+	}
 
 	mem = malloc(sz);
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2] sandbox: libc_malloc: populate barebox errno on error
  2023-11-22 17:00 [PATCH 1/2] sandbox: libc_malloc: populate barebox errno on error Ahmad Fatoum
  2023-11-22 17:00 ` [PATCH 2/2] dlmalloc: populate " Ahmad Fatoum
@ 2023-11-23  7:20 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2023-11-23  7:20 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Wed, Nov 22, 2023 at 06:00:06PM +0100, Ahmad Fatoum wrote:
> TLSF already populates errno on errors, so do likewise for the
> allocators that don't.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  arch/sandbox/os/libc_malloc.c | 28 ++++++++++++++++++++++++----
>  1 file changed, 24 insertions(+), 4 deletions(-)

Applied, thanks

Sascha

> 
> diff --git a/arch/sandbox/os/libc_malloc.c b/arch/sandbox/os/libc_malloc.c
> index 74e3e2680585..975c41b0ec49 100644
> --- a/arch/sandbox/os/libc_malloc.c
> +++ b/arch/sandbox/os/libc_malloc.c
> @@ -6,18 +6,30 @@
>  #include <stdlib.h>
>  #include <malloc.h>
>  
> +#define BAREBOX_ENOMEM 12
> +extern int barebox_errno;
> +
>  void barebox_malloc_stats(void)
>  {
>  }
>  
>  void *barebox_memalign(size_t alignment, size_t bytes)
>  {
> -	return memalign(alignment, bytes);
> +	void *mem = memalign(alignment, bytes);
> +	if (!mem)
> +		barebox_errno = BAREBOX_ENOMEM;
> +
> +	return mem;
>  }
>  
>  void *barebox_malloc(size_t size)
>  {
> -	return malloc(size);
> +
> +	void *mem = malloc(size);
> +	if (!mem)
> +		barebox_errno = BAREBOX_ENOMEM;
> +
> +	return mem;
>  }
>  
>  void barebox_free(void *ptr)
> @@ -27,10 +39,18 @@ void barebox_free(void *ptr)
>  
>  void *barebox_realloc(void *ptr, size_t size)
>  {
> -	return realloc(ptr, size);
> +	void *mem = realloc(ptr, size);
> +	if (!mem)
> +		barebox_errno = BAREBOX_ENOMEM;
> +
> +	return mem;
>  }
>  
>  void *barebox_calloc(size_t n, size_t elem_size)
>  {
> -	return calloc(n, elem_size);
> +	void *mem = calloc(n, elem_size);
> +	if (!mem)
> +		barebox_errno = BAREBOX_ENOMEM;
> +
> +	return mem;
>  }
> -- 
> 2.39.2
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-11-23  7:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-22 17:00 [PATCH 1/2] sandbox: libc_malloc: populate barebox errno on error Ahmad Fatoum
2023-11-22 17:00 ` [PATCH 2/2] dlmalloc: populate " Ahmad Fatoum
2023-11-23  7:20 ` [PATCH 1/2] sandbox: libc_malloc: populate barebox " Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox