xref: /titanic_51/usr/src/lib/libmtmalloc/tests/reallocs.c (revision db3659e514c8bf3f03bcca6dd082e363bd7b466a)
1*db3659e5SSurya Prakki /*
2*db3659e5SSurya Prakki  * CDDL HEADER START
3*db3659e5SSurya Prakki  *
4*db3659e5SSurya Prakki  * The contents of this file are subject to the terms of the
5*db3659e5SSurya Prakki  * Common Development and Distribution License (the "License").
6*db3659e5SSurya Prakki  * You may not use this file except in compliance with the License.
7*db3659e5SSurya Prakki  *
8*db3659e5SSurya Prakki  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*db3659e5SSurya Prakki  * or http://www.opensolaris.org/os/licensing.
10*db3659e5SSurya Prakki  * See the License for the specific language governing permissions
11*db3659e5SSurya Prakki  * and limitations under the License.
12*db3659e5SSurya Prakki  *
13*db3659e5SSurya Prakki  * When distributing Covered Code, include this CDDL HEADER in each
14*db3659e5SSurya Prakki  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*db3659e5SSurya Prakki  * If applicable, add the following below this CDDL HEADER, with the
16*db3659e5SSurya Prakki  * fields enclosed by brackets "[]" replaced with your own identifying
17*db3659e5SSurya Prakki  * information: Portions Copyright [yyyy] [name of copyright owner]
18*db3659e5SSurya Prakki  *
19*db3659e5SSurya Prakki  * CDDL HEADER END
20*db3659e5SSurya Prakki  */
21*db3659e5SSurya Prakki /*
22*db3659e5SSurya Prakki  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23*db3659e5SSurya Prakki  * Use is subject to license terms.
24*db3659e5SSurya Prakki  */
25*db3659e5SSurya Prakki 
26*db3659e5SSurya Prakki #include <stdio.h>
27*db3659e5SSurya Prakki #include <stdlib.h>
28*db3659e5SSurya Prakki 
29*db3659e5SSurya Prakki /*
30*db3659e5SSurya Prakki  * realloc() optimization for Small sized caches.
31*db3659e5SSurya Prakki  * If a malloc'ed buffer can satisfy a realloc() request
32*db3659e5SSurya Prakki  * as well, it should return the same buffer.
33*db3659e5SSurya Prakki  *
34*db3659e5SSurya Prakki  * cc -O -o reallocs reallocs.c -lmtmalloc
35*db3659e5SSurya Prakki  */
36*db3659e5SSurya Prakki 
37*db3659e5SSurya Prakki #define	MAX_CACHE_SZ 65536
38*db3659e5SSurya Prakki 
39*db3659e5SSurya Prakki int
40*db3659e5SSurya Prakki main()
41*db3659e5SSurya Prakki {
42*db3659e5SSurya Prakki 	void *p, *q;
43*db3659e5SSurya Prakki 	size_t size;
44*db3659e5SSurya Prakki 
45*db3659e5SSurya Prakki 	for (size = 1; size <= MAX_CACHE_SZ; size++) {
46*db3659e5SSurya Prakki 		p = malloc(size);
47*db3659e5SSurya Prakki 		q = realloc(p, size);
48*db3659e5SSurya Prakki 		if (p != q)
49*db3659e5SSurya Prakki 			(void) fprintf(stderr, "size: %d, p: 0x%p, q: 0x%p\n",
50*db3659e5SSurya Prakki 			    size, p, q);
51*db3659e5SSurya Prakki 		free(q);
52*db3659e5SSurya Prakki 	}
53*db3659e5SSurya Prakki 	return (0);
54*db3659e5SSurya Prakki }
55