Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / ircd_alloc.c
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$
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 /** Allocate memory.
61  * @param[in] size Number of bytes to allocate.
62  * @param[in] x Type of allocation (ignored).
63  * @param[in] y Name of file doing allocation (ignored).
64  * @param[in] z Line number doing allocation (ignored).
65  * @return Newly allocated block of memory.
66  */
67 void* DoMalloc(size_t size, const char* x, const char* y, int z)
68 {
69   void* t = malloc(size);
70   if (!t)
71     (*noMemHandler)();
72   return t;
73 }
74
75 /** Allocate zero-initialized memory.
76  * @param[in] size Number of bytes to allocate.
77  * @param[in] x Type of allocation (ignored).
78  * @param[in] y Name of file doing allocation (ignored).
79  * @param[in] z Line number doing allocation (ignored).
80  * @return Newly allocated block of memory.
81  */
82 void* DoMallocZero(size_t size, const char* x, const char* y, int z)
83 {
84   void* t = malloc(size);
85   if (!t)
86     (*noMemHandler)();
87   memset(t, 0, size);
88   return t;
89 }
90
91 /** Resize an allocated block of memory.
92  * @param[in] orig Original block to resize.
93  * @param[in] size Minimum size for new block.
94  * @param[in] file Name of file doing reallocation (ignored).
95  * @param[in] line Line number doing reallocation (ignored).
96  */
97 void* DoRealloc(void *orig, size_t size, const char *file, int line)
98 {
99   void* t = realloc(orig, size);
100   if (!t)
101     (*noMemHandler)();
102   return t;
103 }