2007-07-05 16:02:19 +00:00
|
|
|
/*
|
|
|
|
* xfuncs.c - safe malloc funcions
|
|
|
|
*
|
|
|
|
* based on busybox
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
|
|
|
|
*
|
|
|
|
* See file CREDITS for list of people who contributed to this
|
|
|
|
* project.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
2007-07-05 16:01:39 +00:00
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <malloc.h>
|
2007-10-04 10:33:20 +00:00
|
|
|
#include <module.h>
|
2015-07-17 19:22:35 +00:00
|
|
|
#include <wchar.h>
|
2007-07-05 16:01:39 +00:00
|
|
|
|
|
|
|
void *xmalloc(size_t size)
|
|
|
|
{
|
|
|
|
void *p = NULL;
|
|
|
|
|
|
|
|
if (!(p = malloc(size)))
|
|
|
|
panic("ERROR: out of memory\n");
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
2007-10-04 10:33:20 +00:00
|
|
|
EXPORT_SYMBOL(xmalloc);
|
2007-07-05 16:01:39 +00:00
|
|
|
|
|
|
|
void *xrealloc(void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
void *p = NULL;
|
|
|
|
|
|
|
|
if (!(p = realloc(ptr, size)))
|
|
|
|
panic("ERROR: out of memory\n");
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
2007-10-04 10:33:20 +00:00
|
|
|
EXPORT_SYMBOL(xrealloc);
|
2007-07-05 16:01:42 +00:00
|
|
|
|
|
|
|
void *xzalloc(size_t size)
|
|
|
|
{
|
|
|
|
void *ptr = xmalloc(size);
|
|
|
|
memset(ptr, 0, size);
|
|
|
|
return ptr;
|
|
|
|
}
|
2007-10-04 10:33:20 +00:00
|
|
|
EXPORT_SYMBOL(xzalloc);
|
|
|
|
|
2009-09-25 10:16:30 +00:00
|
|
|
char *xstrdup(const char *s)
|
|
|
|
{
|
|
|
|
char *p = strdup(s);
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
panic("ERROR: out of memory\n");
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xstrdup);
|
|
|
|
|
2015-06-17 20:47:01 +00:00
|
|
|
char *xstrndup(const char *s, size_t n)
|
|
|
|
{
|
|
|
|
int m;
|
|
|
|
char *t;
|
|
|
|
|
|
|
|
/* We can just xmalloc(n+1) and strncpy into it, */
|
|
|
|
/* but think about xstrndup("abc", 10000) wastage! */
|
|
|
|
m = n;
|
|
|
|
t = (char*) s;
|
|
|
|
while (m) {
|
|
|
|
if (!*t) break;
|
|
|
|
m--;
|
|
|
|
t++;
|
|
|
|
}
|
|
|
|
n -= m;
|
|
|
|
t = xmalloc(n + 1);
|
|
|
|
t[n] = '\0';
|
|
|
|
|
|
|
|
return memcpy(t, s, n);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xstrndup);
|
|
|
|
|
2011-01-06 15:23:01 +00:00
|
|
|
void* xmemalign(size_t alignment, size_t bytes)
|
|
|
|
{
|
|
|
|
void *p = memalign(alignment, bytes);
|
|
|
|
if (!p)
|
|
|
|
panic("ERROR: out of memory\n");
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xmemalign);
|
2013-11-25 14:37:33 +00:00
|
|
|
|
|
|
|
void *xmemdup(const void *orig, size_t size)
|
|
|
|
{
|
|
|
|
void *buf = xmalloc(size);
|
|
|
|
|
|
|
|
memcpy(buf, orig, size);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xmemdup);
|
2015-07-17 19:22:34 +00:00
|
|
|
|
|
|
|
char *xvasprintf(const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = vasprintf(fmt, ap);
|
|
|
|
if (!p)
|
|
|
|
panic("ERROR: out of memory\n");
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xvasprintf);
|
|
|
|
|
|
|
|
char *xasprintf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
p = xvasprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xasprintf);
|
2015-07-17 19:22:35 +00:00
|
|
|
|
|
|
|
wchar_t *xstrdup_wchar(const wchar_t *s)
|
|
|
|
{
|
|
|
|
wchar_t *p = strdup_wchar(s);
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
panic("ERROR: out of memory\n");
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xstrdup_wchar);
|
|
|
|
|
|
|
|
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");
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xstrdup_char_to_wchar);
|
|
|
|
|
|
|
|
char *xstrdup_wchar_to_char(const wchar_t *s)
|
|
|
|
{
|
|
|
|
char *p = strdup_wchar_to_char(s);
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
panic("ERROR: out of memory\n");
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xstrdup_wchar_to_char);
|