| 1 | /* |
| 2 | * Copyright (C) 2004 Dizzy |
| 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 | #define XALLOC_INTERNAL_ACCESS |
| 22 | #include "xalloc.h" |
| 23 | #undef XALLOC_INTERNAL_ACCESS |
| 24 | |
| 25 | #include "compat/strdup.h" |
| 26 | #include "common/eventlog.h" |
| 27 | #include "common/setup_after.h" |
| 28 | |
| 29 | namespace pvpgn |
| 30 | { |
| 31 | |
| 32 | static t_oom_cb oom_cb = NULL; |
| 33 | |
| 34 | void *xmalloc_real(std::size_t size, const char *fn, unsigned ln) |
| 35 | { |
| 36 | void *res; |
| 37 | |
| 38 | res = malloc(size); |
| 39 | if (!res) { |
| 40 | eventlog(eventlog_level_fatal, __FUNCTION__, "out of memory (from {}:{})", fn, ln); |
| 41 | if (oom_cb && oom_cb() && (res = malloc(size))) return res; |
| 42 | std::abort(); |
| 43 | } |
| 44 | |
| 45 | return res; |
| 46 | } |
| 47 | |
| 48 | void *xcalloc_real(std::size_t nmemb, std::size_t size, const char *fn, unsigned ln) |
| 49 | { |
| 50 | void *res; |
| 51 | |
| 52 | res = calloc(nmemb, size); |
| 53 | if (!res) { |
| 54 | eventlog(eventlog_level_fatal, __FUNCTION__, "out of memory (from {}:{})", fn, ln); |
| 55 | if (oom_cb && oom_cb() && (res = calloc(nmemb, size))) return res; |
| 56 | std::abort(); |
| 57 | } |
| 58 | |
| 59 | return res; |
| 60 | } |
| 61 | |
| 62 | void *xrealloc_real(void *ptr, std::size_t size, const char *fn, unsigned ln) |
| 63 | { |
| 64 | void *res; |
| 65 | |
| 66 | res = std::realloc(ptr, size); |
| 67 | if (!res) { |
| 68 | eventlog(eventlog_level_fatal, __FUNCTION__, "out of memory (from {}:{})", fn, ln); |
| 69 | if (oom_cb && oom_cb() && (res = std::realloc(ptr, size))) return res; |
| 70 | std::abort(); |
| 71 | } |
| 72 | |
| 73 | return res; |
| 74 | } |
| 75 | |
| 76 | char *xstrdup_real(const char *str, const char *fn, unsigned ln) |
| 77 | { |
| 78 | char *res; |
| 79 | |
| 80 | res = strdup(str); |
| 81 | if (!res) { |
| 82 | eventlog(eventlog_level_fatal, __FUNCTION__, "out of memory (from {}:{})", fn, ln); |
| 83 | if (oom_cb && oom_cb() && (res = strdup(str))) return res; |
| 84 | std::abort(); |
| 85 | } |
| 86 | |
| 87 | return res; |
| 88 | } |
| 89 | |
| 90 | void xfree_real(void *ptr, const char *fn, unsigned ln) |
| 91 | { |
| 92 | if (!ptr) { |
| 93 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL ptr (from {}:{})", fn, ln); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | free(ptr); |
| 98 | } |
| 99 | |
| 100 | void xalloc_setcb(t_oom_cb cb) |
| 101 | { |
| 102 | oom_cb = cb; |
| 103 | } |
| 104 | |
| 105 | } |
| 106 | |
| 107 | #endif /* XALLOC_SKIP */ |