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