xref: /freebsd/contrib/sendmail/include/sm/heap.h (revision d7d962ead0b6e5e8a39202d0590022082bf5bfb6)
1 /*
2  * Copyright (c) 2000-2001, 2006 Proofpoint, Inc. and its suppliers.
3  *	All rights reserved.
4  *
5  * By using this file, you agree to the terms and conditions set
6  * forth in the LICENSE file which can be found at the top level of
7  * the sendmail distribution.
8  *
9  *	$Id: heap.h,v 1.24 2013-11-22 20:51:31 ca Exp $
10  */
11 
12 /*
13 **  Sendmail debugging memory allocation package.
14 **  See libsm/heap.html for documentation.
15 */
16 
17 #ifndef SM_HEAP_H
18 # define SM_HEAP_H
19 
20 # include <sm/io.h>
21 # include <stdlib.h>
22 # include <sm/debug.h>
23 # include <sm/exc.h>
24 
25 /* change default to 0 for production? */
26 # ifndef SM_HEAP_CHECK
27 #  define SM_HEAP_CHECK		1
28 # endif
29 
30 # if SM_HEAP_CHECK
31 #  define sm_malloc_x(sz) sm_malloc_tagged_x(sz, __FILE__, __LINE__, SmHeapGroup)
32 #  define sm_malloc(size) sm_malloc_tagged(size, __FILE__, __LINE__, SmHeapGroup)
33 #  define sm_free(ptr) sm_free_tagged(ptr, __FILE__, __LINE__)
34 
35 extern void *sm_malloc_tagged __P((size_t, char *, int, int));
36 extern void *sm_malloc_tagged_x __P((size_t, char *, int, int));
37 extern void sm_free_tagged __P((void *, char *, int));
38 extern void *sm_realloc_x __P((void *, size_t));
39 extern bool sm_heap_register __P((void *, size_t, char *, int, int));
40 extern void sm_heap_checkptr_tagged  __P((void *, char *, int));
41 extern void sm_heap_report __P((SM_FILE_T *, int));
42 
43 # else /* SM_HEAP_CHECK */
44 #  define sm_malloc_tagged(size, file, line, grp)	sm_malloc(size)
45 #  define sm_malloc_tagged_x(size, file, line, grp)	sm_malloc_x(size)
46 #  define sm_free_tagged(ptr, file, line)		sm_free(ptr)
47 #  define sm_heap_register(ptr, size, file, line, grp)	(true)
48 #  define sm_heap_checkptr_tagged(ptr, tag, num)	((void)0)
49 #  define sm_heap_report(file, verbose)			((void)0)
50 
51 extern void *sm_malloc __P((size_t));
52 extern void *sm_malloc_x __P((size_t));
53 extern void *sm_realloc_x __P((void *, size_t));
54 extern void sm_free __P((void *));
55 # endif /* SM_HEAP_CHECK */
56 
57 extern void *sm_realloc __P((void *, size_t));
58 
59 # define sm_heap_checkptr(ptr) sm_heap_checkptr_tagged(ptr, __FILE__, __LINE__)
60 
61 #if 0
62 /*
63 **  sm_f[mc]alloc are plug in replacements for malloc and calloc
64 **  which can be used in a context requiring a function pointer,
65 **  and which are compatible with sm_free.  Warning: sm_heap_report
66 **  cannot report where storage leaked by sm_f[mc]alloc was allocated.
67 */
68 
69 /* XXX unused right now */
70 
71 extern void *
72 sm_fmalloc __P((
73 	size_t));
74 
75 extern void *
76 sm_fcalloc __P((
77 	size_t,
78 	size_t));
79 #endif /* 0 */
80 
81 /*
82 **  Allocate 'permanent' storage that can be freed but may still be
83 **  allocated when the process exits.  sm_heap_report will not complain
84 **  about a storage leak originating from a call to sm_pmalloc.
85 */
86 
87 # define sm_pmalloc(size)   sm_malloc_tagged(size, __FILE__, __LINE__, 0)
88 # define sm_pmalloc_x(size) sm_malloc_tagged_x(size, __FILE__, __LINE__, 0)
89 
90 # define sm_heap_group()	SmHeapGroup
91 # define sm_heap_setgroup(g)	(SmHeapGroup = (g))
92 # define sm_heap_newgroup()	(SmHeapGroup = ++SmHeapMaxGroup)
93 
94 #define SM_FREE(ptr)			\
95 	do				\
96 	{				\
97 		if ((ptr) != NULL)	\
98 		{			\
99 			sm_free(ptr);	\
100 			(ptr) = NULL;	\
101 		}			\
102 	} while (0)
103 
104 extern int SmHeapGroup;
105 extern int SmHeapMaxGroup;
106 
107 extern SM_DEBUG_T SmHeapTrace;
108 extern SM_DEBUG_T SmHeapCheck;
109 extern SM_EXC_T SmHeapOutOfMemory;
110 
111 #endif /* ! SM_HEAP_H */
112