1 |
|
/* |
2 |
< |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
< |
* memory.c: Memory utilities. |
2 |
> |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
|
* |
4 |
< |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
4 |
> |
* Copyright (c) 1997-2018 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 |
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 |
18 |
> |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
|
* USA |
21 |
– |
* |
22 |
– |
* $Id$ |
20 |
|
*/ |
21 |
|
|
22 |
+ |
/*! \file memory.c |
23 |
+ |
* \brief Memory utilities. |
24 |
+ |
* \version $Id$ |
25 |
+ |
*/ |
26 |
|
|
27 |
|
#include "stdinc.h" |
27 |
– |
#include "ircd_defs.h" |
28 |
|
#include "irc_string.h" |
29 |
|
#include "memory.h" |
30 |
– |
#include "log.h" |
30 |
|
#include "restart.h" |
31 |
|
|
32 |
|
|
33 |
|
/* |
34 |
< |
* MyMalloc - allocate memory, call outofmemory on failure |
34 |
> |
* xcalloc - allocate memory, call outofmemory on failure |
35 |
|
*/ |
36 |
|
void * |
37 |
< |
MyMalloc(size_t size) |
37 |
> |
xcalloc(size_t size) |
38 |
|
{ |
39 |
|
void *ret = calloc(1, size); |
40 |
|
|
45 |
|
} |
46 |
|
|
47 |
|
/* |
48 |
< |
* MyRealloc - reallocate memory, call outofmemory on failure |
48 |
> |
* xrealloc - reallocate memory, call outofmemory on failure |
49 |
|
*/ |
50 |
|
void * |
51 |
< |
MyRealloc(void *x, size_t y) |
51 |
> |
xrealloc(void *x, size_t y) |
52 |
|
{ |
53 |
|
void *ret = realloc(x, y); |
54 |
|
|
55 |
< |
if (ret == NULL) |
55 |
> |
if (y && ret == NULL) |
56 |
|
outofmemory(); |
57 |
|
|
58 |
|
return ret; |
59 |
|
} |
60 |
|
|
61 |
|
void |
62 |
< |
MyFree(void *x) |
62 |
> |
xfree(void *x) |
63 |
|
{ |
64 |
< |
if (x) |
66 |
< |
free(x); |
64 |
> |
free(x); |
65 |
|
} |
66 |
|
|
67 |
< |
void |
68 |
< |
_DupString(char **x, const char *y) |
67 |
> |
void * |
68 |
> |
xstrdup(const char *s) |
69 |
|
{ |
70 |
< |
(*x) = malloc(strlen(y) + 1); |
71 |
< |
strcpy((*x), y); |
70 |
> |
void *ret = malloc(strlen(s) + 1); |
71 |
> |
|
72 |
> |
if (ret == NULL) |
73 |
> |
outofmemory(); |
74 |
> |
|
75 |
> |
strcpy(ret, s); |
76 |
> |
|
77 |
> |
return ret; |
78 |
> |
} |
79 |
> |
|
80 |
> |
void * |
81 |
> |
xstrndup(const char *s, size_t len) |
82 |
> |
{ |
83 |
> |
void *ret = malloc(len + 1); |
84 |
> |
|
85 |
> |
if (ret == NULL) |
86 |
> |
outofmemory(); |
87 |
> |
|
88 |
> |
strlcpy(ret, s, len + 1); |
89 |
> |
|
90 |
> |
return ret; |
91 |
|
} |
92 |
|
|
93 |
|
/* outofmemory() |
105 |
|
if (was_here++) |
106 |
|
abort(); |
107 |
|
|
108 |
< |
ilog(LOG_TYPE_IRCD, "Out of memory: restarting server..."); |
92 |
< |
restart("Out of Memory"); |
93 |
< |
} |
94 |
< |
|
95 |
< |
#ifndef NDEBUG |
96 |
< |
/* |
97 |
< |
* frob some memory. debugging time. |
98 |
< |
* -- adrian |
99 |
< |
*/ |
100 |
< |
void |
101 |
< |
mem_frob(void *data, int len) |
102 |
< |
{ |
103 |
< |
/* correct for Intel only! little endian */ |
104 |
< |
unsigned char b[4] = { 0xef, 0xbe, 0xad, 0xde }; |
105 |
< |
int i; |
106 |
< |
char *cdata = data; |
107 |
< |
|
108 |
< |
for (i = 0; i < len; i++) |
109 |
< |
{ |
110 |
< |
*cdata = b[i % 4]; |
111 |
< |
cdata++; |
112 |
< |
} |
108 |
> |
server_die("out of memory", SERVER_RESTART); |
109 |
|
} |
114 |
– |
#endif |