xref: /titanic_41/usr/src/cmd/sendmail/libsm/t-heap.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
11*7c478bd9Sstevel@tonic-gate 
12*7c478bd9Sstevel@tonic-gate #include <sm/gen.h>
13*7c478bd9Sstevel@tonic-gate SM_IDSTR(id, "@(#)$Id: t-heap.c,v 1.8 2001/03/06 17:27:36 ca Exp $")
14*7c478bd9Sstevel@tonic-gate 
15*7c478bd9Sstevel@tonic-gate #include <sm/debug.h>
16*7c478bd9Sstevel@tonic-gate #include <sm/heap.h>
17*7c478bd9Sstevel@tonic-gate #include <sm/io.h>
18*7c478bd9Sstevel@tonic-gate #include <sm/test.h>
19*7c478bd9Sstevel@tonic-gate #include <sm/xtrap.h>
20*7c478bd9Sstevel@tonic-gate 
21*7c478bd9Sstevel@tonic-gate #if SM_HEAP_CHECK
22*7c478bd9Sstevel@tonic-gate extern SM_DEBUG_T SmHeapCheck;
23*7c478bd9Sstevel@tonic-gate # define HEAP_CHECK sm_debug_active(&SmHeapCheck, 1)
24*7c478bd9Sstevel@tonic-gate #else /* SM_HEAP_CHECK */
25*7c478bd9Sstevel@tonic-gate # define HEAP_CHECK 0
26*7c478bd9Sstevel@tonic-gate #endif /* SM_HEAP_CHECK */
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate int
main(argc,argv)29*7c478bd9Sstevel@tonic-gate main(argc, argv)
30*7c478bd9Sstevel@tonic-gate 	int argc;
31*7c478bd9Sstevel@tonic-gate 	char **argv;
32*7c478bd9Sstevel@tonic-gate {
33*7c478bd9Sstevel@tonic-gate 	void *p;
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate 	sm_test_begin(argc, argv, "test heap handling");
36*7c478bd9Sstevel@tonic-gate 	if (argc > 1)
37*7c478bd9Sstevel@tonic-gate 		sm_debug_addsettings_x(argv[1]);
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate 	p = sm_malloc(10);
40*7c478bd9Sstevel@tonic-gate 	SM_TEST(p != NULL);
41*7c478bd9Sstevel@tonic-gate 	p = sm_realloc_x(p, 20);
42*7c478bd9Sstevel@tonic-gate 	SM_TEST(p != NULL);
43*7c478bd9Sstevel@tonic-gate 	p = sm_realloc(p, 30);
44*7c478bd9Sstevel@tonic-gate 	SM_TEST(p != NULL);
45*7c478bd9Sstevel@tonic-gate 	if (HEAP_CHECK)
46*7c478bd9Sstevel@tonic-gate 	{
47*7c478bd9Sstevel@tonic-gate 		sm_dprintf("heap with 1 30-byte block allocated:\n");
48*7c478bd9Sstevel@tonic-gate 		sm_heap_report(smioout, 3);
49*7c478bd9Sstevel@tonic-gate 	}
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate 	if (HEAP_CHECK)
52*7c478bd9Sstevel@tonic-gate 	{
53*7c478bd9Sstevel@tonic-gate 		sm_free(p);
54*7c478bd9Sstevel@tonic-gate 		sm_dprintf("heap with 0 blocks allocated:\n");
55*7c478bd9Sstevel@tonic-gate 		sm_heap_report(smioout, 3);
56*7c478bd9Sstevel@tonic-gate 		sm_dprintf("xtrap count = %d\n", SmXtrapCount);
57*7c478bd9Sstevel@tonic-gate 	}
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate #if DEBUG
60*7c478bd9Sstevel@tonic-gate 	/* this will cause a core dump */
61*7c478bd9Sstevel@tonic-gate 	sm_dprintf("about to free %p for the second time\n", p);
62*7c478bd9Sstevel@tonic-gate 	sm_free(p);
63*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	return sm_test_end();
66*7c478bd9Sstevel@tonic-gate }
67