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