]>
Commit | Line | Data |
---|---|---|
189935b1 | 1 | /************************************************************************ |
2 | * IRC - Internet Relay Chat, ircd/ircd_alloc.c | |
3 | * Copyright (C) 1999 Thomas Helvey (BleepSoft) | |
4 | * | |
5 | * See file AUTHORS in IRC package for additional names of | |
6 | * the programmers. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 1, or (at your option) | |
11 | * any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | */ | |
22 | /** @file | |
23 | * @brief IRC daemon memory allocation functions. | |
24 | * @version $Id: ircd_alloc.c,v 1.10 2005/01/27 04:07:46 entrope Exp $ | |
25 | */ | |
26 | #include "config.h" | |
27 | ||
28 | #include "ircd_alloc.h" | |
29 | #include "ircd_log.h" | |
30 | #include "ircd_string.h" | |
31 | #include "s_debug.h" | |
32 | ||
33 | /* #include <assert.h> -- Now using assert in ircd_log.h */ | |
34 | #include <string.h> | |
35 | ||
36 | static void nomem_handler(void); | |
37 | ||
38 | /** Variable holding out-of-memory callback. */ | |
39 | static OutOfMemoryHandler noMemHandler = nomem_handler; | |
40 | ||
41 | /** Default handler for out-of-memory conditions. */ | |
42 | static void | |
43 | nomem_handler(void) | |
44 | { | |
45 | #ifdef MDEBUG | |
46 | assert(0); | |
47 | #else | |
48 | Debug((DEBUG_FATAL, "Out of memory, exiting")); | |
49 | exit(2); | |
50 | #endif | |
51 | } | |
52 | ||
53 | /** Set callback function for out-of-memory conditions. */ | |
54 | void | |
55 | set_nomem_handler(OutOfMemoryHandler handler) | |
56 | { | |
57 | noMemHandler = handler; | |
58 | } | |
59 | ||
60 | #ifndef MDEBUG | |
61 | /** Allocate memory. | |
62 | * @param[in] size Number of bytes to allocate. | |
63 | * @param[in] x Type of allocation (ignored). | |
64 | * @param[in] y Name of file doing allocation (ignored). | |
65 | * @param[in] z Line number doing allocation (ignored). | |
66 | * @return Newly allocated block of memory. | |
67 | */ | |
68 | void* DoMalloc(size_t size, const char* x, const char* y, int z) | |
69 | { | |
70 | void* t = malloc(size); | |
71 | if (!t) | |
72 | (*noMemHandler)(); | |
73 | return t; | |
74 | } | |
75 | ||
76 | /** Allocate zero-initialized memory. | |
77 | * @param[in] size Number of bytes to allocate. | |
78 | * @param[in] x Type of allocation (ignored). | |
79 | * @param[in] y Name of file doing allocation (ignored). | |
80 | * @param[in] z Line number doing allocation (ignored). | |
81 | * @return Newly allocated block of memory. | |
82 | */ | |
83 | void* DoMallocZero(size_t size, const char* x, const char* y, int z) | |
84 | { | |
85 | void* t = malloc(size); | |
86 | if (!t) | |
87 | (*noMemHandler)(); | |
88 | memset(t, 0, size); | |
89 | return t; | |
90 | } | |
91 | ||
92 | /** Resize an allocated block of memory. | |
93 | * @param[in] orig Original block to resize. | |
94 | * @param[in] size Minimum size for new block. | |
95 | * @param[in] file Name of file doing reallocation (ignored). | |
96 | * @param[in] line Line number doing reallocation (ignored). | |
97 | */ | |
98 | void* DoRealloc(void *orig, size_t size, const char *file, int line) | |
99 | { | |
100 | void* t = realloc(orig, size); | |
101 | if (!t) | |
102 | (*noMemHandler)(); | |
103 | return t; | |
104 | } | |
105 | #endif |