2bfa0186b80dff29bc93702a2b2231f55aad71c7
[ircu2.10.12-pk.git] / include / ircd_alloc.h
1 /*
2  * IRC - Internet Relay Chat, include/ircd_alloc.h
3  * Copyright (C) 1999 Thomas Helvey <tomh@inxpress.net>
4  *                   
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
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * Commentary by Bleep (Thomas Helvey)
21  *
22  * $Id$
23  */
24 #ifndef INCLUDED_ircd_alloc_h
25 #define INCLUDED_ircd_alloc_h
26
27 /*
28  * memory resource allocation and test functions
29  */
30 typedef void (*OutOfMemoryHandler)(void);
31 extern void set_nomem_handler(OutOfMemoryHandler handler);
32
33 /* The mappings for the My* functions... */
34 #define MyMalloc(size) \
35   DoMalloc(size, "malloc", __FILE__, __LINE__)
36
37 #define MyCalloc(nelem, size) \
38   DoMallocZero(size * nelem, "calloc", __FILE__, __LINE__)
39
40 #define MyFree(p) \
41   if (p) \
42     DoFree(p, __FILE__, __LINE__)
43
44 /* No realloc because it is not currently used, and it is not really the
45  * nicest function to be using anyway(i.e. its evil if you want it
46  * go ahead and write it).
47  */
48
49
50 /* First version: fast non-debugging macros... */
51 #ifndef MDEBUG
52 #ifndef INCLUDED_stdlib_h
53 #include <stdlib.h> /* free */
54 #define INCLUDED_stdlib_h
55 #endif
56
57 extern OutOfMemoryHandler noMemHandler;
58
59 #define DoFree(x, file, line) do { free((x)); (x) = 0; } while(0)
60 extern void* DoMalloc(size_t len, const char*, const char*, int);
61 extern void* DoMallocZero(size_t len, const char*, const char*, int);
62 #if 0
63 extern void *malloc_tmp;
64 /*
65   Bleah, this is silly, the function call overhead for doing
66   the RightThing(tm) well worth the cost of avoiding this
67   non-reentrant mess of this macro, and accompanying global.
68 */
69 #define DoMalloc(size, type, file, line) \
70   (\
71      (malloc_tmp = malloc(size), \
72      (malloc_tmp == NULL) ? (*noMemHandler)() : 0), \
73   malloc_tmp)
74
75 #define DoMallocZero(size, type, file, line) \
76   (\
77     (DoMalloc(size, type, file, line), \
78     memset(malloc_tmp, 0, size)), \
79   malloc_tmp)
80 #endif
81
82 /* Second version: slower debugging versions... */
83 #else /* defined(MDEBUG) */
84 #include <sys/types.h>
85 #include "memdebug.h"
86
87 #define DoMalloc(size, type, file, line) \
88   dbg_malloc(size, type, file, line)
89 #define DoMallocZero(size, type, file, line) \
90   dbg_malloc_zero(size, type, file, line)
91 #define DoFree(p, file, line) \
92   dbg_free(p, file, line)
93 #endif /* defined(MDEBUG) */
94
95 #endif /* INCLUDED_ircd_alloc_h */