From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH] xfuncs: Be more informative when out of memory panic occurs
Date: Wed, 12 Apr 2017 12:19:27 +0200 [thread overview]
Message-ID: <20170412101927.27440-1-s.hauer@pengutronix.de> (raw)
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 <s.hauer@pengutronix.de>
---
lib/xfuncs.c | 32 ++++++++++++++++++++++++--------
1 file changed, 24 insertions(+), 8 deletions(-)
diff --git a/lib/xfuncs.c b/lib/xfuncs.c
index 1dc2ea92d8..1bcaa5e10e 100644
--- a/lib/xfuncs.c
+++ b/lib/xfuncs.c
@@ -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;
}
@@ -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;
}
@@ -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;
}
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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);
--
2.11.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
reply other threads:[~2017-04-12 10:19 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170412101927.27440-1-s.hauer@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox