heap.h revision 302408
150397Sobrien/*
2169689Skan * Copyright (c) 2000-2001, 2006 Proofpoint, Inc. and its suppliers.
390075Sobrien *	All rights reserved.
450397Sobrien *
590075Sobrien * By using this file, you agree to the terms and conditions set
650397Sobrien * forth in the LICENSE file which can be found at the top level of
790075Sobrien * the sendmail distribution.
890075Sobrien *
990075Sobrien *	$Id: heap.h,v 1.24 2013-11-22 20:51:31 ca Exp $
1090075Sobrien */
1150397Sobrien
1290075Sobrien/*
1390075Sobrien**  Sendmail debugging memory allocation package.
1490075Sobrien**  See libsm/heap.html for documentation.
1590075Sobrien*/
1650397Sobrien
1750397Sobrien#ifndef SM_HEAP_H
1890075Sobrien# define SM_HEAP_H
19169689Skan
20169689Skan# include <sm/io.h>
2150397Sobrien# include <stdlib.h>
2290075Sobrien# include <sm/debug.h>
23117395Skan# include <sm/exc.h>
24169689Skan
2590075Sobrien/* change default to 0 for production? */
26117395Skan# ifndef SM_HEAP_CHECK
27117395Skan#  define SM_HEAP_CHECK		1
28117395Skan# endif /* ! SM_HEAP_CHECK */
29169689Skan
30169689Skan# if SM_HEAP_CHECK
31169689Skan#  define sm_malloc_x(sz) sm_malloc_tagged_x(sz, __FILE__, __LINE__, SmHeapGroup)
32117395Skan#  define sm_malloc(size) sm_malloc_tagged(size, __FILE__, __LINE__, SmHeapGroup)
3350397Sobrien#  define sm_free(ptr) sm_free_tagged(ptr, __FILE__, __LINE__)
3450397Sobrien
3550397Sobrienextern void *sm_malloc_tagged __P((size_t, char *, int, int));
36169689Skanextern void *sm_malloc_tagged_x __P((size_t, char *, int, int));
3750397Sobrienextern void sm_free_tagged __P((void *, char *, int));
3850397Sobrienextern void *sm_realloc_x __P((void *, size_t));
39169689Skanextern bool sm_heap_register __P((void *, size_t, char *, int, int));
4050397Sobrienextern void sm_heap_checkptr_tagged  __P((void *, char *, int));
41169689Skanextern void sm_heap_report __P((SM_FILE_T *, int));
4250397Sobrien
43169689Skan# else /* SM_HEAP_CHECK */
44169689Skan#  define sm_malloc_tagged(size, file, line, grp)	sm_malloc(size)
45169689Skan#  define sm_malloc_tagged_x(size, file, line, grp)	sm_malloc_x(size)
46169689Skan#  define sm_free_tagged(ptr, file, line)		sm_free(ptr)
47169689Skan#  define sm_heap_register(ptr, size, file, line, grp)	(true)
48169689Skan#  define sm_heap_checkptr_tagged(ptr, tag, num)	((void)0)
49169689Skan#  define sm_heap_report(file, verbose)			((void)0)
50169689Skan
5150397Sobrienextern void *sm_malloc __P((size_t));
5250397Sobrienextern void *sm_malloc_x __P((size_t));
53169689Skanextern void *sm_realloc_x __P((void *, size_t));
5450397Sobrienextern void sm_free __P((void *));
55169689Skan# endif /* SM_HEAP_CHECK */
56169689Skan
57169689Skanextern void *sm_realloc __P((void *, size_t));
58169689Skan
59169689Skan# define sm_heap_checkptr(ptr) sm_heap_checkptr_tagged(ptr, __FILE__, __LINE__)
60169689Skan
61169689Skan#if 0
62169689Skan/*
63117395Skan**  sm_f[mc]alloc are plug in replacements for malloc and calloc
6450397Sobrien**  which can be used in a context requiring a function pointer,
6590075Sobrien**  and which are compatible with sm_free.  Warning: sm_heap_report
6690075Sobrien**  cannot report where storage leaked by sm_f[mc]alloc was allocated.
6790075Sobrien*/
68117395Skan
6950397Sobrien/* XXX unused right now */
7050397Sobrien
7150397Sobrienextern void *
72117395Skansm_fmalloc __P((
7390075Sobrien	size_t));
7490075Sobrien
7590075Sobrienextern void *
76169689Skansm_fcalloc __P((
77169689Skan	size_t,
78117395Skan	size_t));
7990075Sobrien#endif /* 0 */
8050397Sobrien
8150397Sobrien/*
8290075Sobrien**  Allocate 'permanent' storage that can be freed but may still be
83169689Skan**  allocated when the process exits.  sm_heap_report will not complain
8450397Sobrien**  about a storage leak originating from a call to sm_pmalloc.
8550397Sobrien*/
86132718Skan
8750397Sobrien# define sm_pmalloc(size)   sm_malloc_tagged(size, __FILE__, __LINE__, 0)
8890075Sobrien# define sm_pmalloc_x(size) sm_malloc_tagged_x(size, __FILE__, __LINE__, 0)
89132718Skan
9050397Sobrien# define sm_heap_group()	SmHeapGroup
9190075Sobrien# define sm_heap_setgroup(g)	(SmHeapGroup = (g))
92169689Skan# define sm_heap_newgroup()	(SmHeapGroup = ++SmHeapMaxGroup)
9390075Sobrien
94169689Skan#define SM_FREE(ptr)			\
95169689Skan	do				\
9650397Sobrien	{				\
97169689Skan		if ((ptr) != NULL)	\
98169689Skan		{			\
99169689Skan			sm_free(ptr);	\
10050397Sobrien			(ptr) = NULL;	\
101169689Skan		}			\
102169689Skan	} while (0)
103169689Skan
104169689Skanextern int SmHeapGroup;
105169689Skanextern int SmHeapMaxGroup;
106169689Skan
107169689Skanextern SM_DEBUG_T SmHeapTrace;
108169689Skanextern SM_DEBUG_T SmHeapCheck;
109169689Skanextern SM_EXC_T SmHeapOutOfMemory;
110169689Skan
111169689Skan#endif /* ! SM_HEAP_H */
112169689Skan