1 |
adx |
30 |
/* |
2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
adx |
30 |
* |
4 |
michael |
2916 |
* Copyright (c) 1997-2014 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 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
|
|
* 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 |
michael |
1309 |
#include "log.h" |
32 |
adx |
30 |
#include "restart.h" |
33 |
|
|
|
34 |
|
|
|
35 |
|
|
/* |
36 |
|
|
* MyMalloc - allocate memory, call outofmemory on failure |
37 |
|
|
*/ |
38 |
|
|
void * |
39 |
|
|
MyMalloc(size_t size) |
40 |
|
|
{ |
41 |
|
|
void *ret = calloc(1, size); |
42 |
|
|
|
43 |
|
|
if (ret == NULL) |
44 |
|
|
outofmemory(); |
45 |
michael |
1011 |
|
46 |
|
|
return ret; |
47 |
adx |
30 |
} |
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 |
michael |
1011 |
|
60 |
|
|
return ret; |
61 |
adx |
30 |
} |
62 |
|
|
|
63 |
|
|
void |
64 |
|
|
MyFree(void *x) |
65 |
|
|
{ |
66 |
|
|
if (x) |
67 |
|
|
free(x); |
68 |
|
|
} |
69 |
|
|
|
70 |
michael |
1646 |
void * |
71 |
|
|
xstrdup(const char *s) |
72 |
adx |
30 |
{ |
73 |
michael |
1646 |
void *ret = malloc(strlen(s) + 1); |
74 |
|
|
|
75 |
|
|
if (ret == NULL) |
76 |
|
|
outofmemory(); |
77 |
|
|
|
78 |
|
|
strcpy(ret, s); |
79 |
|
|
|
80 |
|
|
return ret; |
81 |
adx |
30 |
} |
82 |
|
|
|
83 |
michael |
1646 |
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 |
adx |
30 |
/* 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 |
michael |
1011 |
if (was_here++) |
109 |
adx |
30 |
abort(); |
110 |
|
|
|
111 |
michael |
1247 |
ilog(LOG_TYPE_IRCD, "Out of memory: restarting server..."); |
112 |
michael |
3167 |
server_die("Out of Memory", 1); |
113 |
adx |
30 |
} |