| 1 |
/* |
| 2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
* |
| 4 |
* Copyright (c) 1997-2014 ircd-hybrid development team |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 19 |
* USA |
| 20 |
*/ |
| 21 |
|
| 22 |
/*! \file memory.c |
| 23 |
* \brief Memory utilities. |
| 24 |
* \version $Id$ |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "stdinc.h" |
| 28 |
#include "ircd_defs.h" |
| 29 |
#include "irc_string.h" |
| 30 |
#include "memory.h" |
| 31 |
#include "log.h" |
| 32 |
#include "restart.h" |
| 33 |
|
| 34 |
|
| 35 |
/* |
| 36 |
* MyCalloc - allocate memory, call outofmemory on failure |
| 37 |
*/ |
| 38 |
void * |
| 39 |
MyCalloc(size_t size) |
| 40 |
{ |
| 41 |
void *ret = calloc(1, size); |
| 42 |
|
| 43 |
if (ret == NULL) |
| 44 |
outofmemory(); |
| 45 |
|
| 46 |
return ret; |
| 47 |
} |
| 48 |
|
| 49 |
/* |
| 50 |
* MyRealloc - reallocate memory, call outofmemory on failure |
| 51 |
*/ |
| 52 |
void * |
| 53 |
MyRealloc(void *x, size_t y) |
| 54 |
{ |
| 55 |
void *ret = realloc(x, y); |
| 56 |
|
| 57 |
if (ret == NULL) |
| 58 |
outofmemory(); |
| 59 |
|
| 60 |
return ret; |
| 61 |
} |
| 62 |
|
| 63 |
void |
| 64 |
MyFree(void *x) |
| 65 |
{ |
| 66 |
if (x) |
| 67 |
free(x); |
| 68 |
} |
| 69 |
|
| 70 |
void * |
| 71 |
xstrdup(const char *s) |
| 72 |
{ |
| 73 |
void *ret = malloc(strlen(s) + 1); |
| 74 |
|
| 75 |
if (ret == NULL) |
| 76 |
outofmemory(); |
| 77 |
|
| 78 |
strcpy(ret, s); |
| 79 |
|
| 80 |
return ret; |
| 81 |
} |
| 82 |
|
| 83 |
void * |
| 84 |
xstrndup(const char *s, size_t len) |
| 85 |
{ |
| 86 |
void *ret = malloc(len + 1); |
| 87 |
|
| 88 |
if (ret == NULL) |
| 89 |
outofmemory(); |
| 90 |
|
| 91 |
strlcpy(ret, s, len + 1); |
| 92 |
|
| 93 |
return ret; |
| 94 |
} |
| 95 |
|
| 96 |
/* outofmemory() |
| 97 |
* |
| 98 |
* input - NONE |
| 99 |
* output - NONE |
| 100 |
* side effects - simply try to report there is a problem. |
| 101 |
* Abort if it was called more than once |
| 102 |
*/ |
| 103 |
void |
| 104 |
outofmemory(void) |
| 105 |
{ |
| 106 |
static int was_here = 0; |
| 107 |
|
| 108 |
if (was_here++) |
| 109 |
abort(); |
| 110 |
|
| 111 |
ilog(LOG_TYPE_IRCD, "Out of memory: restarting server..."); |
| 112 |
server_die("Out of Memory", 1); |
| 113 |
} |