1 |
adx |
30 |
/* |
2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
adx |
30 |
* |
4 |
michael |
5347 |
* Copyright (c) 1997-2015 ircd-hybrid development team |
5 |
adx |
30 |
* |
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 |
michael |
4565 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
adx |
30 |
* USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
michael |
2916 |
/*! \file memory.c |
23 |
|
|
* \brief Memory utilities. |
24 |
|
|
* \version $Id$ |
25 |
|
|
*/ |
26 |
adx |
30 |
|
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 |
michael |
3504 |
* MyCalloc - allocate memory, call outofmemory on failure |
36 |
adx |
30 |
*/ |
37 |
|
|
void * |
38 |
michael |
3504 |
MyCalloc(size_t size) |
39 |
adx |
30 |
{ |
40 |
|
|
void *ret = calloc(1, size); |
41 |
|
|
|
42 |
|
|
if (ret == NULL) |
43 |
|
|
outofmemory(); |
44 |
michael |
1011 |
|
45 |
|
|
return ret; |
46 |
adx |
30 |
} |
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 |
michael |
4473 |
if (y && ret == NULL) |
57 |
adx |
30 |
outofmemory(); |
58 |
michael |
1011 |
|
59 |
|
|
return ret; |
60 |
adx |
30 |
} |
61 |
|
|
|
62 |
|
|
void |
63 |
|
|
MyFree(void *x) |
64 |
|
|
{ |
65 |
michael |
5425 |
free(x); |
66 |
adx |
30 |
} |
67 |
|
|
|
68 |
michael |
1646 |
void * |
69 |
|
|
xstrdup(const char *s) |
70 |
adx |
30 |
{ |
71 |
michael |
1646 |
void *ret = malloc(strlen(s) + 1); |
72 |
|
|
|
73 |
|
|
if (ret == NULL) |
74 |
|
|
outofmemory(); |
75 |
|
|
|
76 |
|
|
strcpy(ret, s); |
77 |
|
|
|
78 |
|
|
return ret; |
79 |
adx |
30 |
} |
80 |
|
|
|
81 |
michael |
1646 |
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 |
adx |
30 |
/* 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 |
michael |
1011 |
if (was_here++) |
107 |
adx |
30 |
abort(); |
108 |
|
|
|
109 |
michael |
6556 |
server_die("out of memory", SERVER_RESTART); |
110 |
adx |
30 |
} |