Skip to content

Commit

Permalink
xfuncs: Be more informative when out of memory panic occurs
Browse files Browse the repository at this point in the history
When one of the xfuncs panics we can be a bit more informative. We
can at least print the amount of bytes we wanted to allocate and
how much memory we have left.

Signed-off-by: Sascha Hauer <[email protected]>
  • Loading branch information
saschahauer committed Apr 28, 2017
1 parent bd3b279 commit 1ce56a2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
3 changes: 0 additions & 3 deletions common/dlmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1753,7 +1753,6 @@ void *calloc(size_t n, size_t elem_size)

/* Utility to update current_mallinfo for malloc_stats and mallinfo() */

#ifdef CONFIG_CMD_MEMINFO
static void malloc_update_mallinfo(void)
{
int i;
Expand Down Expand Up @@ -1821,8 +1820,6 @@ void malloc_stats(void)
#endif
}

#endif /* CONFIG_CMD_MEMINFO */

/*
History:
Expand Down
4 changes: 4 additions & 0 deletions common/dummy_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#include <common.h>
#include <malloc.h>

void malloc_stats(void)
{
}

void *memalign(size_t alignment, size_t bytes)
{
unsigned long mem = (unsigned long)sbrk(bytes + alignment);
Expand Down
32 changes: 24 additions & 8 deletions lib/xfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,30 @@
* GNU General Public License for more details.
*
*/
#define pr_fmt(fmt) "xfuncs: " fmt

#include <common.h>
#include <malloc.h>
#include <module.h>
#include <wchar.h>

static void __noreturn enomem_panic(size_t size)
{
pr_emerg("out of memory\n");
if (size)
pr_emerg("Unable to allocate %d bytes\n", size);

malloc_stats();

panic("out of memory");
}

void *xmalloc(size_t size)
{
void *p = NULL;

if (!(p = malloc(size)))
panic("ERROR: out of memory\n");
enomem_panic(size);

return p;
}
Expand All @@ -40,7 +52,7 @@ void *xrealloc(void *ptr, size_t size)
void *p = NULL;

if (!(p = realloc(ptr, size)))
panic("ERROR: out of memory\n");
enomem_panic(size);

return p;
}
Expand All @@ -63,7 +75,7 @@ char *xstrdup(const char *s)

p = strdup(s);
if (!p)
panic("ERROR: out of memory\n");
enomem_panic(strlen(s) + 1);

return p;
}
Expand Down Expand Up @@ -95,7 +107,8 @@ void* xmemalign(size_t alignment, size_t bytes)
{
void *p = memalign(alignment, bytes);
if (!p)
panic("ERROR: out of memory\n");
enomem_panic(bytes);

return p;
}
EXPORT_SYMBOL(xmemalign);
Expand All @@ -116,7 +129,7 @@ char *xvasprintf(const char *fmt, va_list ap)

p = bvasprintf(fmt, ap);
if (!p)
panic("ERROR: out of memory\n");
enomem_panic(0);
return p;
}
EXPORT_SYMBOL(xvasprintf);
Expand All @@ -139,7 +152,8 @@ wchar_t *xstrdup_wchar(const wchar_t *s)
wchar_t *p = strdup_wchar(s);

if (!p)
panic("ERROR: out of memory\n");
enomem_panic((wcslen(s) + 1) * sizeof(wchar_t));

return p;
}
EXPORT_SYMBOL(xstrdup_wchar);
Expand All @@ -149,7 +163,8 @@ wchar_t *xstrdup_char_to_wchar(const char *s)
wchar_t *p = strdup_char_to_wchar(s);

if (!p)
panic("ERROR: out of memory\n");
enomem_panic((strlen(s) + 1) * sizeof(wchar_t));

return p;
}
EXPORT_SYMBOL(xstrdup_char_to_wchar);
Expand All @@ -159,7 +174,8 @@ char *xstrdup_wchar_to_char(const wchar_t *s)
char *p = strdup_wchar_to_char(s);

if (!p)
panic("ERROR: out of memory\n");
enomem_panic((wcslen(s) + 1) * sizeof(wchar_t));

return p;
}
EXPORT_SYMBOL(xstrdup_wchar_to_char);

0 comments on commit 1ce56a2

Please sign in to comment.