| 1 |
/*
|
| 2 |
* Copyright (C) 2004 Dizzy (dizzy@roedu.net)
|
| 3 |
*
|
| 4 |
* This program is free software; you can redistribute it and/or
|
| 5 |
* modify it under the terms of the GNU General Public License
|
| 6 |
* as published by the Free Software Foundation; either version 2
|
| 7 |
* of the License, or (at your option) any later version.
|
| 8 |
*
|
| 9 |
* This program is distributed in the hope that it will be useful,
|
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
* GNU General Public License for more details.
|
| 13 |
*
|
| 14 |
* You should have received a copy of the GNU General Public License
|
| 15 |
* along with this program; if not, write to the Free Software
|
| 16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 17 |
*/
|
| 18 |
|
| 19 |
#include "common/setup_before.h"
|
| 20 |
#ifndef XALLOC_SKIP
|
| 21 |
|
| 22 |
#ifdef HAVE_STDDEF_H
|
| 23 |
# include <stddef.h>
|
| 24 |
#endif
|
| 25 |
#ifdef STDC_HEADERS
|
| 26 |
# include <stdlib.h>
|
| 27 |
#else
|
| 28 |
# ifdef HAVE_MALLOC_H
|
| 29 |
# include <malloc.h>
|
| 30 |
# endif
|
| 31 |
#endif
|
| 32 |
#ifdef HAVE_STRING_H
|
| 33 |
# include <string.h>
|
| 34 |
#else
|
| 35 |
# ifdef HAVE_STRINGS_H
|
| 36 |
# include <strings.h>
|
| 37 |
# endif
|
| 38 |
#endif
|
| 39 |
#include "compat/strdup.h"
|
| 40 |
#include "common/eventlog.h"
|
| 41 |
#include "common/xalloc.h"
|
| 42 |
#define XALLOC_INTERNAL_ACCESS
|
| 43 |
#include "common/setup_after.h"
|
| 44 |
#undef XALLOC_INTERNAL_ACCESS
|
| 45 |
|
| 46 |
static t_oom_cb oom_cb = NULL;
|
| 47 |
|
| 48 |
void *xmalloc_real(size_t size, const char *fn, unsigned ln)
|
| 49 |
{
|
| 50 |
void *res;
|
| 51 |
|
| 52 |
res = malloc(size);
|
| 53 |
if (!res) {
|
| 54 |
eventlog(eventlog_level_fatal, __FUNCTION__, "out of memory (from %s:%u)",fn,ln);
|
| 55 |
if (oom_cb && oom_cb() && (res = malloc(size))) return res;
|
| 56 |
abort();
|
| 57 |
}
|
| 58 |
|
| 59 |
return res;
|
| 60 |
}
|
| 61 |
|
| 62 |
void *xcalloc_real(size_t nmemb, size_t size, const char *fn, unsigned ln)
|
| 63 |
{
|
| 64 |
void *res;
|
| 65 |
|
| 66 |
res = calloc(nmemb,size);
|
| 67 |
if (!res) {
|
| 68 |
eventlog(eventlog_level_fatal, __FUNCTION__, "out of memory (from %s:%u)",fn,ln);
|
| 69 |
if (oom_cb && oom_cb() && (res = calloc(nmemb,size))) return res;
|
| 70 |
abort();
|
| 71 |
}
|
| 72 |
|
| 73 |
return res;
|
| 74 |
}
|
| 75 |
|
| 76 |
void *xrealloc_real(void *ptr, size_t size, const char *fn, unsigned ln)
|
| 77 |
{
|
| 78 |
void *res;
|
| 79 |
|
| 80 |
res = realloc(ptr,size);
|
| 81 |
if (!res) {
|
| 82 |
eventlog(eventlog_level_fatal, __FUNCTION__, "out of memory (from %s:%u)",fn,ln);
|
| 83 |
if (oom_cb && oom_cb() && (res = realloc(ptr,size))) return res;
|
| 84 |
abort();
|
| 85 |
}
|
| 86 |
|
| 87 |
return res;
|
| 88 |
}
|
| 89 |
|
| 90 |
char *xstrdup_real(const char *str, const char *fn, unsigned ln)
|
| 91 |
{
|
| 92 |
char *res;
|
| 93 |
|
| 94 |
res = strdup(str);
|
| 95 |
if (!res) {
|
| 96 |
eventlog(eventlog_level_fatal, __FUNCTION__, "out of memory (from %s:%u)",fn,ln);
|
| 97 |
if (oom_cb && oom_cb() && (res = strdup(str))) return res;
|
| 98 |
abort();
|
| 99 |
}
|
| 100 |
|
| 101 |
return res;
|
| 102 |
}
|
| 103 |
|
| 104 |
void xfree_real(void *ptr, const char *fn, unsigned ln)
|
| 105 |
{
|
| 106 |
if (!ptr) {
|
| 107 |
eventlog(eventlog_level_error, __FUNCTION__, "got NULL ptr (from %s:%u)",fn,ln);
|
| 108 |
return;
|
| 109 |
}
|
| 110 |
|
| 111 |
free(ptr);
|
| 112 |
}
|
| 113 |
|
| 114 |
void xalloc_setcb(t_oom_cb cb)
|
| 115 |
{
|
| 116 |
oom_cb = cb;
|
| 117 |
}
|
| 118 |
|
| 119 |
#endif /* XALLOC_SKIP */
|