2010-10-15 12:03:25 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Davidlohr Bueso <dave@gnu.org>
|
|
|
|
*
|
|
|
|
* This file may be redistributed under the terms of the
|
|
|
|
* GNU Lesser General Public License.
|
|
|
|
*
|
2011-02-17 06:09:07 -06:00
|
|
|
* General memory allocation wrappers for malloc, realloc, calloc and strdup
|
2010-10-15 12:03:25 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef UTIL_LINUX_XALLOC_H
|
|
|
|
#define UTIL_LINUX_XALLOC_H
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2011-02-04 08:08:48 -06:00
|
|
|
#include <string.h>
|
2010-10-15 12:03:25 -05:00
|
|
|
|
2011-02-07 10:29:47 -06:00
|
|
|
#include "c.h"
|
|
|
|
|
2011-01-23 14:28:19 -06:00
|
|
|
#ifndef XALLOC_EXIT_CODE
|
|
|
|
# define XALLOC_EXIT_CODE EXIT_FAILURE
|
|
|
|
#endif
|
|
|
|
|
2019-08-18 05:03:44 -05:00
|
|
|
static inline
|
|
|
|
__attribute__((__noreturn__))
|
|
|
|
void __err_oom(const char *file, unsigned int line)
|
2015-07-01 04:03:56 -05:00
|
|
|
{
|
|
|
|
err(XALLOC_EXIT_CODE, "%s: %u: cannot allocate memory", file, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define err_oom() __err_oom(__FILE__, __LINE__)
|
|
|
|
|
2019-08-18 05:03:44 -05:00
|
|
|
static inline
|
|
|
|
__ul_alloc_size(1)
|
|
|
|
__ul_returns_nonnull
|
2010-10-15 12:03:25 -05:00
|
|
|
void *xmalloc(const size_t size)
|
|
|
|
{
|
2019-08-18 05:11:00 -05:00
|
|
|
void *ret = malloc(size);
|
2010-10-15 12:03:25 -05:00
|
|
|
|
2019-08-18 05:11:00 -05:00
|
|
|
if (!ret && size)
|
|
|
|
err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
|
|
|
|
return ret;
|
2010-10-15 12:03:25 -05:00
|
|
|
}
|
|
|
|
|
2019-08-18 05:03:44 -05:00
|
|
|
static inline
|
|
|
|
__ul_alloc_size(2)
|
|
|
|
__ul_returns_nonnull
|
2010-10-15 12:03:25 -05:00
|
|
|
void *xrealloc(void *ptr, const size_t size)
|
|
|
|
{
|
2019-08-18 05:11:00 -05:00
|
|
|
void *ret = realloc(ptr, size);
|
2010-10-15 12:03:25 -05:00
|
|
|
|
2019-08-18 05:11:00 -05:00
|
|
|
if (!ret && size)
|
|
|
|
err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
|
|
|
|
return ret;
|
2010-10-15 12:03:25 -05:00
|
|
|
}
|
|
|
|
|
2019-08-18 05:03:44 -05:00
|
|
|
static inline
|
|
|
|
__ul_calloc_size(1, 2)
|
|
|
|
__ul_returns_nonnull
|
2010-10-15 12:03:25 -05:00
|
|
|
void *xcalloc(const size_t nelems, const size_t size)
|
|
|
|
{
|
2019-08-18 05:11:00 -05:00
|
|
|
void *ret = calloc(nelems, size);
|
2010-10-15 12:03:25 -05:00
|
|
|
|
2019-08-18 05:11:00 -05:00
|
|
|
if (!ret && size && nelems)
|
|
|
|
err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
|
|
|
|
return ret;
|
2010-10-15 12:03:25 -05:00
|
|
|
}
|
|
|
|
|
2019-08-18 05:03:44 -05:00
|
|
|
static inline
|
|
|
|
__attribute__((warn_unused_result))
|
|
|
|
__ul_returns_nonnull
|
|
|
|
char *xstrdup(const char *str)
|
2010-11-24 09:39:15 -06:00
|
|
|
{
|
2019-08-18 05:11:00 -05:00
|
|
|
char *ret;
|
2010-11-24 09:39:15 -06:00
|
|
|
|
2019-08-18 05:11:00 -05:00
|
|
|
assert(str);
|
|
|
|
ret = strdup(str);
|
|
|
|
if (!ret)
|
|
|
|
err(XALLOC_EXIT_CODE, "cannot duplicate string");
|
|
|
|
return ret;
|
2010-11-24 09:39:15 -06:00
|
|
|
}
|
|
|
|
|
2019-08-18 05:03:44 -05:00
|
|
|
static inline
|
|
|
|
__attribute__((warn_unused_result))
|
|
|
|
__ul_returns_nonnull
|
|
|
|
char *xstrndup(const char *str, size_t size)
|
2013-04-10 10:16:01 -05:00
|
|
|
{
|
2019-08-18 05:11:00 -05:00
|
|
|
char *ret;
|
2013-04-10 10:16:01 -05:00
|
|
|
|
2019-08-18 05:11:00 -05:00
|
|
|
assert(str);
|
|
|
|
ret = strndup(str, size);
|
|
|
|
if (!ret)
|
|
|
|
err(XALLOC_EXIT_CODE, "cannot duplicate string");
|
|
|
|
return ret;
|
2013-04-10 10:16:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 05:03:44 -05:00
|
|
|
static inline
|
|
|
|
__attribute__((__format__(printf, 2, 3)))
|
|
|
|
int xasprintf(char **strp, const char *fmt, ...)
|
2012-02-29 08:49:47 -06:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list args;
|
2019-08-18 05:11:00 -05:00
|
|
|
|
2012-02-29 08:49:47 -06:00
|
|
|
va_start(args, fmt);
|
|
|
|
ret = vasprintf(&(*strp), fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
if (ret < 0)
|
|
|
|
err(XALLOC_EXIT_CODE, "cannot allocate string");
|
|
|
|
return ret;
|
|
|
|
}
|
2012-10-19 09:23:54 -05:00
|
|
|
|
2019-08-18 05:03:44 -05:00
|
|
|
static inline
|
|
|
|
__attribute__((__format__(printf, 2, 0)))
|
|
|
|
int xvasprintf(char **strp, const char *fmt, va_list ap)
|
2014-01-09 07:48:49 -06:00
|
|
|
{
|
|
|
|
int ret = vasprintf(&(*strp), fmt, ap);
|
2019-08-18 05:11:00 -05:00
|
|
|
|
2014-01-09 07:48:49 -06:00
|
|
|
if (ret < 0)
|
|
|
|
err(XALLOC_EXIT_CODE, "cannot allocate string");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-19 09:23:54 -05:00
|
|
|
|
2019-08-18 05:03:44 -05:00
|
|
|
static inline
|
|
|
|
__attribute__((warn_unused_result))
|
|
|
|
char *xgethostname(void)
|
2012-10-19 09:23:54 -05:00
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
size_t sz = get_hostname_max() + 1;
|
|
|
|
|
|
|
|
name = xmalloc(sizeof(char) * sz);
|
2013-03-27 10:08:47 -05:00
|
|
|
if (gethostname(name, sz) != 0) {
|
|
|
|
free(name);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-10-19 09:23:54 -05:00
|
|
|
name[sz - 1] = '\0';
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2010-10-15 12:03:25 -05:00
|
|
|
#endif
|