2007-08-02 14:48:26 -04:00
|
|
|
/*
|
2015-01-07 17:44:44 -05:00
|
|
|
* Copyright (C) 2007 Moritz Grimm <mgrimm@mrsserver.net>
|
2007-08-02 14:48:26 -04:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2015-04-22 17:38:19 -04:00
|
|
|
#include "compat.h"
|
2007-08-02 14:48:26 -04:00
|
|
|
|
2015-04-22 17:38:19 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2007-08-02 14:48:26 -04:00
|
|
|
|
2015-04-22 17:38:19 -04:00
|
|
|
#include "log.h"
|
|
|
|
#include "xalloc.h"
|
2007-08-02 14:48:26 -04:00
|
|
|
|
|
|
|
void *
|
|
|
|
xmalloc_c(size_t size, const char *file, unsigned int line)
|
|
|
|
{
|
2015-04-22 17:38:19 -04:00
|
|
|
void *ret = malloc(size);
|
2007-08-02 14:48:26 -04:00
|
|
|
|
2015-04-22 17:38:19 -04:00
|
|
|
if (NULL == ret) {
|
|
|
|
log_alert("%s[%u]: cannot allocate %zu bytes",
|
|
|
|
file, line, size);
|
|
|
|
exit(1);
|
2007-08-02 14:48:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2015-04-22 17:38:19 -04:00
|
|
|
xcalloc_c(size_t nmemb, size_t size, const char *file, unsigned int line)
|
2007-08-02 14:48:26 -04:00
|
|
|
{
|
2015-04-22 17:38:19 -04:00
|
|
|
void *ret = calloc(nmemb, size);
|
2007-08-02 14:48:26 -04:00
|
|
|
|
2015-04-22 17:38:19 -04:00
|
|
|
if (NULL == ret) {
|
|
|
|
log_alert("%s[%u]: cannot allocate %zu * %zu bytes",
|
|
|
|
file, line, nmemb, size);
|
|
|
|
exit(1);
|
2007-08-02 14:48:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2015-04-22 17:38:19 -04:00
|
|
|
xreallocarray_c(void *ptr, size_t nmemb, size_t size, const char *file,
|
|
|
|
unsigned int line)
|
2007-08-02 14:48:26 -04:00
|
|
|
{
|
2015-04-22 17:38:19 -04:00
|
|
|
void *ret = reallocarray(ptr, nmemb, size);
|
2007-08-02 14:48:26 -04:00
|
|
|
|
2015-04-22 17:38:19 -04:00
|
|
|
if (NULL == ret) {
|
|
|
|
log_alert("%s[%u]: cannot allocate %zu * %zu bytes",
|
|
|
|
file, line, nmemb, size);
|
|
|
|
exit(1);
|
2007-08-02 14:48:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
xstrdup_c(const char *str, const char *file, unsigned int line)
|
|
|
|
{
|
2015-04-22 17:38:19 -04:00
|
|
|
char *ret = strdup(str);
|
2007-08-02 14:48:26 -04:00
|
|
|
|
2015-04-22 17:38:19 -04:00
|
|
|
if (NULL == ret) {
|
|
|
|
log_error("%s[%u]: cannot allocate %zu bytes",
|
|
|
|
file, line, strlen(str) + 1);
|
|
|
|
exit(1);
|
2007-08-02 14:48:26 -04:00
|
|
|
}
|
|
|
|
|
2015-04-22 17:38:19 -04:00
|
|
|
return (ret);
|
2007-08-02 14:48:26 -04:00
|
|
|
}
|
|
|
|
|
2015-04-22 17:38:19 -04:00
|
|
|
void
|
|
|
|
xfree_c(void *ptr, const char *file, unsigned int line)
|
2007-08-02 14:48:26 -04:00
|
|
|
{
|
2015-04-22 17:38:19 -04:00
|
|
|
(void)file;
|
|
|
|
(void)line;
|
|
|
|
free(ptr);
|
2007-08-02 14:48:26 -04:00
|
|
|
}
|