1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2015 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
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 "restart.h" |
32 |
|
33 |
|
34 |
/* |
35 |
* MyCalloc - allocate memory, call outofmemory on failure |
36 |
*/ |
37 |
void * |
38 |
MyCalloc(size_t size) |
39 |
{ |
40 |
void *ret = calloc(1, size); |
41 |
|
42 |
if (ret == NULL) |
43 |
outofmemory(); |
44 |
|
45 |
return ret; |
46 |
} |
47 |
|
48 |
/* |
49 |
* MyRealloc - reallocate memory, call outofmemory on failure |
50 |
*/ |
51 |
void * |
52 |
MyRealloc(void *x, size_t y) |
53 |
{ |
54 |
void *ret = realloc(x, y); |
55 |
|
56 |
if (y && ret == NULL) |
57 |
outofmemory(); |
58 |
|
59 |
return ret; |
60 |
} |
61 |
|
62 |
void |
63 |
MyFree(void *x) |
64 |
{ |
65 |
free(x); |
66 |
} |
67 |
|
68 |
void * |
69 |
xstrdup(const char *s) |
70 |
{ |
71 |
void *ret = malloc(strlen(s) + 1); |
72 |
|
73 |
if (ret == NULL) |
74 |
outofmemory(); |
75 |
|
76 |
strcpy(ret, s); |
77 |
|
78 |
return ret; |
79 |
} |
80 |
|
81 |
void * |
82 |
xstrndup(const char *s, size_t len) |
83 |
{ |
84 |
void *ret = malloc(len + 1); |
85 |
|
86 |
if (ret == NULL) |
87 |
outofmemory(); |
88 |
|
89 |
strlcpy(ret, s, len + 1); |
90 |
|
91 |
return ret; |
92 |
} |
93 |
|
94 |
/* outofmemory() |
95 |
* |
96 |
* input - NONE |
97 |
* output - NONE |
98 |
* side effects - simply try to report there is a problem. |
99 |
* Abort if it was called more than once |
100 |
*/ |
101 |
void |
102 |
outofmemory(void) |
103 |
{ |
104 |
static int was_here = 0; |
105 |
|
106 |
if (was_here++) |
107 |
abort(); |
108 |
|
109 |
server_die("out of memory", SERVER_RESTART); |
110 |
} |