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