xref: /illumos-gate/usr/src/cmd/sendmail/libsm/t-heap.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
3*7c478bd9Sstevel@tonic-gate  *	All rights reserved.
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
6*7c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
7*7c478bd9Sstevel@tonic-gate  * the sendmail distribution.
8*7c478bd9Sstevel@tonic-gate  */
9*7c478bd9Sstevel@tonic-gate 
10*7c478bd9Sstevel@tonic-gate #include <sm/gen.h>
11*7c478bd9Sstevel@tonic-gate SM_IDSTR(id, "@(#)$Id: t-heap.c,v 1.8 2001/03/06 17:27:36 ca Exp $")
12*7c478bd9Sstevel@tonic-gate 
13*7c478bd9Sstevel@tonic-gate #include <sm/debug.h>
14*7c478bd9Sstevel@tonic-gate #include <sm/heap.h>
15*7c478bd9Sstevel@tonic-gate #include <sm/io.h>
16*7c478bd9Sstevel@tonic-gate #include <sm/test.h>
17*7c478bd9Sstevel@tonic-gate #include <sm/xtrap.h>
18*7c478bd9Sstevel@tonic-gate 
19*7c478bd9Sstevel@tonic-gate #if SM_HEAP_CHECK
20*7c478bd9Sstevel@tonic-gate extern SM_DEBUG_T SmHeapCheck;
21*7c478bd9Sstevel@tonic-gate # define HEAP_CHECK sm_debug_active(&SmHeapCheck, 1)
22*7c478bd9Sstevel@tonic-gate #else /* SM_HEAP_CHECK */
23*7c478bd9Sstevel@tonic-gate # define HEAP_CHECK 0
24*7c478bd9Sstevel@tonic-gate #endif /* SM_HEAP_CHECK */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate int
main(argc,argv)27*7c478bd9Sstevel@tonic-gate main(argc, argv)
28*7c478bd9Sstevel@tonic-gate 	int argc;
29*7c478bd9Sstevel@tonic-gate 	char **argv;
30*7c478bd9Sstevel@tonic-gate {
31*7c478bd9Sstevel@tonic-gate 	void *p;
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate 	sm_test_begin(argc, argv, "test heap handling");
34*7c478bd9Sstevel@tonic-gate 	if (argc > 1)
35*7c478bd9Sstevel@tonic-gate 		sm_debug_addsettings_x(argv[1]);
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate 	p = sm_malloc(10);
38*7c478bd9Sstevel@tonic-gate 	SM_TEST(p != NULL);
39*7c478bd9Sstevel@tonic-gate 	p = sm_realloc_x(p, 20);
40*7c478bd9Sstevel@tonic-gate 	SM_TEST(p != NULL);
41*7c478bd9Sstevel@tonic-gate 	p = sm_realloc(p, 30);
42*7c478bd9Sstevel@tonic-gate 	SM_TEST(p != NULL);
43*7c478bd9Sstevel@tonic-gate 	if (HEAP_CHECK)
44*7c478bd9Sstevel@tonic-gate 	{
45*7c478bd9Sstevel@tonic-gate 		sm_dprintf("heap with 1 30-byte block allocated:\n");
46*7c478bd9Sstevel@tonic-gate 		sm_heap_report(smioout, 3);
47*7c478bd9Sstevel@tonic-gate 	}
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 	if (HEAP_CHECK)
50*7c478bd9Sstevel@tonic-gate 	{
51*7c478bd9Sstevel@tonic-gate 		sm_free(p);
52*7c478bd9Sstevel@tonic-gate 		sm_dprintf("heap with 0 blocks allocated:\n");
53*7c478bd9Sstevel@tonic-gate 		sm_heap_report(smioout, 3);
54*7c478bd9Sstevel@tonic-gate 		sm_dprintf("xtrap count = %d\n", SmXtrapCount);
55*7c478bd9Sstevel@tonic-gate 	}
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate #if DEBUG
58*7c478bd9Sstevel@tonic-gate 	/* this will cause a core dump */
59*7c478bd9Sstevel@tonic-gate 	sm_dprintf("about to free %p for the second time\n", p);
60*7c478bd9Sstevel@tonic-gate 	sm_free(p);
61*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	return sm_test_end();
64*7c478bd9Sstevel@tonic-gate }
65