d3630123ffa095d6978ca01b07bb7f3517b8f6ca
[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  *   $Id$
23  */
24 #include "config.h"
25
26 #include "ircd_alloc.h"
27 #include "ircd_string.h"
28 #include "s_debug.h"
29
30 #include <assert.h>
31
32 #if !defined(MDEBUG)
33 /*
34  * RELEASE: allocation functions
35  */
36
37 static void nomem_handler(void)
38 {
39   Debug((DEBUG_FATAL, "Out of memory, exiting"));
40   exit(2);
41 }
42
43 static OutOfMemoryHandler noMemHandler = nomem_handler;
44
45 void set_nomem_handler(OutOfMemoryHandler handler)
46 {
47   noMemHandler = handler;
48 }
49
50 void* MyMalloc(size_t size)
51 {
52   void* p = malloc(size);
53   if (!p)
54     (*noMemHandler)();
55   return p;
56 }
57
58 void* MyRealloc(void* p, size_t size)
59 {
60   void* x = realloc(p, size);
61   if (!x)
62     (*noMemHandler)();
63   return x;
64 }
65
66 void* MyCalloc(size_t nelem, size_t size)
67 {
68   void* p = calloc(nelem, size);
69   if (!p)
70     (*noMemHandler)();
71   return p;
72 }
73
74 #else /* defined(MDEBUG) */
75 /*
76  * DEBUG: allocation functions
77  */
78 void set_nomem_handler(OutOfMemoryHandler handler)
79 {
80   assert(0 != handler);
81   fda_set_nomem_handler(handler);
82 }
83
84 #endif /* defined(MDEBUG) */
85