]>
Commit | Line | Data |
---|---|---|
189935b1 | 1 | /* |
2 | * IRC - Internet Relay Chat, include/ircd_alloc.h | |
3 | * Copyright (C) 1999 Thomas Helvey <tomh@inxpress.net> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2, or (at your option) | |
8 | * any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | * | |
19 | * Commentary by Bleep (Thomas Helvey) | |
20 | */ | |
21 | /** @file | |
22 | * @brief IRC daemon memory allocation functions. | |
23 | * @version $Id: ircd_alloc.h,v 1.8 2005/01/27 04:07:44 entrope Exp $ | |
24 | */ | |
25 | #ifndef INCLUDED_ircd_alloc_h | |
26 | #define INCLUDED_ircd_alloc_h | |
27 | ||
28 | /* | |
29 | * memory resource allocation and test functions | |
30 | */ | |
31 | /** Type of handler for out-of-memory conditions. */ | |
32 | typedef void (*OutOfMemoryHandler)(void); | |
33 | extern void set_nomem_handler(OutOfMemoryHandler handler); | |
34 | ||
35 | /* The mappings for the My* functions... */ | |
36 | /** Helper macro for standard allocations. */ | |
37 | #define MyMalloc(size) \ | |
38 | DoMalloc(size, "malloc", __FILE__, __LINE__) | |
39 | ||
40 | /** Helper macro for zero-initialized allocations. */ | |
41 | #define MyCalloc(nelem, size) \ | |
42 | DoMallocZero((size) * (nelem), "calloc", __FILE__, __LINE__) | |
43 | ||
44 | /** Helper macro for freeing memory. */ | |
45 | #define MyFree(p) \ | |
46 | if (p) \ | |
47 | DoFree(p, __FILE__, __LINE__) | |
48 | ||
49 | /** Helper macro for reallocating memory. */ | |
50 | #define MyRealloc(p, size) \ | |
51 | DoRealloc(p, size, __FILE__, __LINE__) | |
52 | ||
53 | /* First version: fast non-debugging macros... */ | |
54 | #ifndef MDEBUG | |
55 | #ifndef INCLUDED_stdlib_h | |
56 | #include <stdlib.h> /* free */ | |
57 | #define INCLUDED_stdlib_h | |
58 | #endif | |
59 | ||
60 | /** Implementation macro for freeing memory. */ | |
61 | #define DoFree(x, file, line) do { free((x)); (x) = 0; } while(0) | |
62 | extern void* DoMalloc(size_t len, const char*, const char*, int); | |
63 | extern void* DoMallocZero(size_t len, const char*, const char*, int); | |
64 | extern void *DoRealloc(void *, size_t, const char*, int); | |
65 | ||
66 | /* Second version: slower debugging versions... */ | |
67 | #else /* defined(MDEBUG) */ | |
68 | #include <sys/types.h> | |
69 | #include "memdebug.h" | |
70 | ||
71 | #define DoMalloc(size, type, file, line) \ | |
72 | dbg_malloc(size, type, file, line) | |
73 | #define DoMallocZero(size, type, file, line) \ | |
74 | dbg_malloc_zero(size, type, file, line) | |
75 | #define DoFree(p, file, line) \ | |
76 | do { dbg_free(p, file, line); (p) = 0; } while (0) | |
77 | #define DoRealloc(p, size, file, line) \ | |
78 | dbg_realloc(p, size, file, line) | |
79 | #endif /* defined(MDEBUG) */ | |
80 | ||
81 | #endif /* INCLUDED_ircd_alloc_h */ |