xref: /titanic_44/usr/src/lib/libumem/common/malloc.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <unistd.h>
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <errno.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <string.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include "umem_base.h"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include "misc.h"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate /*
42*7c478bd9Sstevel@tonic-gate  * malloc_data_t is an 8-byte structure which is located "before" the pointer
43*7c478bd9Sstevel@tonic-gate  * returned from {m,c,re}alloc and memalign.  The first four bytes give
44*7c478bd9Sstevel@tonic-gate  * information about the buffer, and the second four bytes are a status byte.
45*7c478bd9Sstevel@tonic-gate  *
46*7c478bd9Sstevel@tonic-gate  * See umem_impl.h for the various magic numbers used, and the size
47*7c478bd9Sstevel@tonic-gate  * encode/decode macros.
48*7c478bd9Sstevel@tonic-gate  *
49*7c478bd9Sstevel@tonic-gate  * The 'size' of the buffer includes the tags.  That is, we encode the
50*7c478bd9Sstevel@tonic-gate  * argument to umem_alloc(), not the argument to malloc().
51*7c478bd9Sstevel@tonic-gate  */
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate typedef struct malloc_data {
54*7c478bd9Sstevel@tonic-gate 	uint32_t malloc_size;
55*7c478bd9Sstevel@tonic-gate 	uint32_t malloc_stat; /* = UMEM_MALLOC_ENCODE(state, malloc_size) */
56*7c478bd9Sstevel@tonic-gate } malloc_data_t;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate void *
59*7c478bd9Sstevel@tonic-gate malloc(size_t size_arg)
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate #ifdef _LP64
62*7c478bd9Sstevel@tonic-gate 	uint32_t high_size = 0;
63*7c478bd9Sstevel@tonic-gate #endif
64*7c478bd9Sstevel@tonic-gate 	size_t size;
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	malloc_data_t *ret;
67*7c478bd9Sstevel@tonic-gate 	size = size_arg + sizeof (malloc_data_t);
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate #ifdef _LP64
70*7c478bd9Sstevel@tonic-gate 	if (size > UMEM_SECOND_ALIGN) {
71*7c478bd9Sstevel@tonic-gate 		size += sizeof (malloc_data_t);
72*7c478bd9Sstevel@tonic-gate 		high_size = (size >> 32);
73*7c478bd9Sstevel@tonic-gate 	}
74*7c478bd9Sstevel@tonic-gate #endif
75*7c478bd9Sstevel@tonic-gate 	if (size < size_arg) {
76*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;			/* overflow */
77*7c478bd9Sstevel@tonic-gate 		return (NULL);
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 	ret = (malloc_data_t *)_umem_alloc(size, UMEM_DEFAULT);
80*7c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
81*7c478bd9Sstevel@tonic-gate 		if (size <= UMEM_MAXBUF)
82*7c478bd9Sstevel@tonic-gate 			errno = EAGAIN;
83*7c478bd9Sstevel@tonic-gate 		else
84*7c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
85*7c478bd9Sstevel@tonic-gate 		return (NULL);
86*7c478bd9Sstevel@tonic-gate #ifdef _LP64
87*7c478bd9Sstevel@tonic-gate 	} else if (high_size > 0) {
88*7c478bd9Sstevel@tonic-gate 		uint32_t low_size = (uint32_t)size;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 		/*
91*7c478bd9Sstevel@tonic-gate 		 * uses different magic numbers to make it harder to
92*7c478bd9Sstevel@tonic-gate 		 * undetectably corrupt
93*7c478bd9Sstevel@tonic-gate 		 */
94*7c478bd9Sstevel@tonic-gate 		ret->malloc_size = high_size;
95*7c478bd9Sstevel@tonic-gate 		ret->malloc_stat = UMEM_MALLOC_ENCODE(MALLOC_MAGIC, high_size);
96*7c478bd9Sstevel@tonic-gate 		ret++;
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 		ret->malloc_size = low_size;
99*7c478bd9Sstevel@tonic-gate 		ret->malloc_stat = UMEM_MALLOC_ENCODE(MALLOC_OVERSIZE_MAGIC,
100*7c478bd9Sstevel@tonic-gate 		    low_size);
101*7c478bd9Sstevel@tonic-gate 		ret++;
102*7c478bd9Sstevel@tonic-gate 	} else if (size > UMEM_SECOND_ALIGN) {
103*7c478bd9Sstevel@tonic-gate 		uint32_t low_size = (uint32_t)size;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 		ret++; /* leave the first 8 bytes alone */
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 		ret->malloc_size = low_size;
108*7c478bd9Sstevel@tonic-gate 		ret->malloc_stat = UMEM_MALLOC_ENCODE(MALLOC_SECOND_MAGIC,
109*7c478bd9Sstevel@tonic-gate 		    low_size);
110*7c478bd9Sstevel@tonic-gate 		ret++;
111*7c478bd9Sstevel@tonic-gate #endif
112*7c478bd9Sstevel@tonic-gate 	} else {
113*7c478bd9Sstevel@tonic-gate 		ret->malloc_size = size;
114*7c478bd9Sstevel@tonic-gate 		ret->malloc_stat = UMEM_MALLOC_ENCODE(MALLOC_MAGIC, size);
115*7c478bd9Sstevel@tonic-gate 		ret++;
116*7c478bd9Sstevel@tonic-gate 	}
117*7c478bd9Sstevel@tonic-gate 	return ((void *)ret);
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate void *
121*7c478bd9Sstevel@tonic-gate calloc(size_t nelem, size_t elsize)
122*7c478bd9Sstevel@tonic-gate {
123*7c478bd9Sstevel@tonic-gate 	size_t size = nelem * elsize;
124*7c478bd9Sstevel@tonic-gate 	void *retval;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	if (nelem > 0 && elsize > 0 && size/nelem != elsize) {
127*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;				/* overflow */
128*7c478bd9Sstevel@tonic-gate 		return (NULL);
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	retval = malloc(size);
132*7c478bd9Sstevel@tonic-gate 	if (retval == NULL)
133*7c478bd9Sstevel@tonic-gate 		return (NULL);
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	(void) memset(retval, 0, size);
136*7c478bd9Sstevel@tonic-gate 	return (retval);
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate /*
140*7c478bd9Sstevel@tonic-gate  * memalign uses vmem_xalloc to do its work.
141*7c478bd9Sstevel@tonic-gate  *
142*7c478bd9Sstevel@tonic-gate  * in 64-bit, the memaligned buffer always has two tags.  This simplifies the
143*7c478bd9Sstevel@tonic-gate  * code.
144*7c478bd9Sstevel@tonic-gate  */
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate void *
147*7c478bd9Sstevel@tonic-gate memalign(size_t align, size_t size_arg)
148*7c478bd9Sstevel@tonic-gate {
149*7c478bd9Sstevel@tonic-gate 	size_t size;
150*7c478bd9Sstevel@tonic-gate 	uintptr_t phase;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	void *buf;
153*7c478bd9Sstevel@tonic-gate 	malloc_data_t *ret;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	size_t overhead;
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	if (size_arg == 0 || align == 0 || (align & (align - 1)) != 0) {
158*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
159*7c478bd9Sstevel@tonic-gate 		return (NULL);
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	/*
163*7c478bd9Sstevel@tonic-gate 	 * if malloc provides the required alignment, use it.
164*7c478bd9Sstevel@tonic-gate 	 */
165*7c478bd9Sstevel@tonic-gate 	if (align <= UMEM_ALIGN ||
166*7c478bd9Sstevel@tonic-gate 	    (align <= UMEM_SECOND_ALIGN && size_arg >= UMEM_SECOND_ALIGN))
167*7c478bd9Sstevel@tonic-gate 		return (malloc(size_arg));
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate #ifdef _LP64
170*7c478bd9Sstevel@tonic-gate 	overhead = 2 * sizeof (malloc_data_t);
171*7c478bd9Sstevel@tonic-gate #else
172*7c478bd9Sstevel@tonic-gate 	overhead = sizeof (malloc_data_t);
173*7c478bd9Sstevel@tonic-gate #endif
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	ASSERT(overhead <= align);
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	size = size_arg + overhead;
178*7c478bd9Sstevel@tonic-gate 	phase = align - overhead;
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	if (umem_memalign_arena == NULL && umem_init() == 0) {
181*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
182*7c478bd9Sstevel@tonic-gate 		return (NULL);
183*7c478bd9Sstevel@tonic-gate 	}
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	if (size < size_arg) {
186*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;			/* overflow */
187*7c478bd9Sstevel@tonic-gate 		return (NULL);
188*7c478bd9Sstevel@tonic-gate 	}
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	buf = vmem_xalloc(umem_memalign_arena, size, align, phase,
191*7c478bd9Sstevel@tonic-gate 	    0, NULL, NULL, VM_NOSLEEP);
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
194*7c478bd9Sstevel@tonic-gate 		if ((size_arg + align) <= UMEM_MAXBUF)
195*7c478bd9Sstevel@tonic-gate 			errno = EAGAIN;
196*7c478bd9Sstevel@tonic-gate 		else
197*7c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 		return (NULL);
200*7c478bd9Sstevel@tonic-gate 	}
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	ret = (malloc_data_t *)buf;
203*7c478bd9Sstevel@tonic-gate 	{
204*7c478bd9Sstevel@tonic-gate 		uint32_t low_size = (uint32_t)size;
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate #ifdef _LP64
207*7c478bd9Sstevel@tonic-gate 		uint32_t high_size = (uint32_t)(size >> 32);
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 		ret->malloc_size = high_size;
210*7c478bd9Sstevel@tonic-gate 		ret->malloc_stat = UMEM_MALLOC_ENCODE(MEMALIGN_MAGIC,
211*7c478bd9Sstevel@tonic-gate 		    high_size);
212*7c478bd9Sstevel@tonic-gate 		ret++;
213*7c478bd9Sstevel@tonic-gate #endif
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 		ret->malloc_size = low_size;
216*7c478bd9Sstevel@tonic-gate 		ret->malloc_stat = UMEM_MALLOC_ENCODE(MEMALIGN_MAGIC, low_size);
217*7c478bd9Sstevel@tonic-gate 		ret++;
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	ASSERT(P2PHASE((uintptr_t)ret, align) == 0);
221*7c478bd9Sstevel@tonic-gate 	ASSERT((void *)((uintptr_t)ret - overhead) == buf);
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	return ((void *)ret);
224*7c478bd9Sstevel@tonic-gate }
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate void *
227*7c478bd9Sstevel@tonic-gate valloc(size_t size)
228*7c478bd9Sstevel@tonic-gate {
229*7c478bd9Sstevel@tonic-gate 	return (memalign(pagesize, size));
230*7c478bd9Sstevel@tonic-gate }
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate /*
233*7c478bd9Sstevel@tonic-gate  * process_free:
234*7c478bd9Sstevel@tonic-gate  *
235*7c478bd9Sstevel@tonic-gate  * Pulls information out of a buffer pointer, and optionally free it.
236*7c478bd9Sstevel@tonic-gate  * This is used by free() and realloc() to process buffers.
237*7c478bd9Sstevel@tonic-gate  *
238*7c478bd9Sstevel@tonic-gate  * On failure, calls umem_err_recoverable() with an appropriate message
239*7c478bd9Sstevel@tonic-gate  * On success, returns the data size through *data_size_arg, if (!is_free).
240*7c478bd9Sstevel@tonic-gate  *
241*7c478bd9Sstevel@tonic-gate  * Preserves errno, since free()'s semantics require it.
242*7c478bd9Sstevel@tonic-gate  */
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate static int
245*7c478bd9Sstevel@tonic-gate process_free(void *buf_arg,
246*7c478bd9Sstevel@tonic-gate     int do_free,		/* free the buffer, or just get its size? */
247*7c478bd9Sstevel@tonic-gate     size_t *data_size_arg)	/* output: bytes of data in buf_arg */
248*7c478bd9Sstevel@tonic-gate {
249*7c478bd9Sstevel@tonic-gate 	malloc_data_t *buf;
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	void *base;
252*7c478bd9Sstevel@tonic-gate 	size_t size;
253*7c478bd9Sstevel@tonic-gate 	size_t data_size;
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	const char *message;
256*7c478bd9Sstevel@tonic-gate 	int old_errno = errno;
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 	buf = (malloc_data_t *)buf_arg;
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	buf--;
261*7c478bd9Sstevel@tonic-gate 	size = buf->malloc_size;
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	switch (UMEM_MALLOC_DECODE(buf->malloc_stat, size)) {
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	case MALLOC_MAGIC:
266*7c478bd9Sstevel@tonic-gate 		base = (void *)buf;
267*7c478bd9Sstevel@tonic-gate 		data_size = size - sizeof (malloc_data_t);
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 		if (do_free)
270*7c478bd9Sstevel@tonic-gate 			buf->malloc_stat = UMEM_FREE_PATTERN_32;
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 		goto process_malloc;
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate #ifdef _LP64
275*7c478bd9Sstevel@tonic-gate 	case MALLOC_SECOND_MAGIC:
276*7c478bd9Sstevel@tonic-gate 		base = (void *)(buf - 1);
277*7c478bd9Sstevel@tonic-gate 		data_size = size - 2 * sizeof (malloc_data_t);
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 		if (do_free)
280*7c478bd9Sstevel@tonic-gate 			buf->malloc_stat = UMEM_FREE_PATTERN_32;
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 		goto process_malloc;
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 	case MALLOC_OVERSIZE_MAGIC: {
285*7c478bd9Sstevel@tonic-gate 		size_t high_size;
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 		buf--;
288*7c478bd9Sstevel@tonic-gate 		high_size = buf->malloc_size;
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 		if (UMEM_MALLOC_DECODE(buf->malloc_stat, high_size) !=
291*7c478bd9Sstevel@tonic-gate 		    MALLOC_MAGIC) {
292*7c478bd9Sstevel@tonic-gate 			message = "invalid or corrupted buffer";
293*7c478bd9Sstevel@tonic-gate 			break;
294*7c478bd9Sstevel@tonic-gate 		}
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 		size += high_size << 32;
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 		base = (void *)buf;
299*7c478bd9Sstevel@tonic-gate 		data_size = size - 2 * sizeof (malloc_data_t);
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 		if (do_free) {
302*7c478bd9Sstevel@tonic-gate 			buf->malloc_stat = UMEM_FREE_PATTERN_32;
303*7c478bd9Sstevel@tonic-gate 			(buf + 1)->malloc_stat = UMEM_FREE_PATTERN_32;
304*7c478bd9Sstevel@tonic-gate 		}
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate 		goto process_malloc;
307*7c478bd9Sstevel@tonic-gate 	}
308*7c478bd9Sstevel@tonic-gate #endif
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 	case MEMALIGN_MAGIC: {
311*7c478bd9Sstevel@tonic-gate 		size_t overhead = sizeof (malloc_data_t);
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate #ifdef _LP64
314*7c478bd9Sstevel@tonic-gate 		size_t high_size;
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 		overhead += sizeof (malloc_data_t);
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 		buf--;
319*7c478bd9Sstevel@tonic-gate 		high_size = buf->malloc_size;
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 		if (UMEM_MALLOC_DECODE(buf->malloc_stat, high_size) !=
322*7c478bd9Sstevel@tonic-gate 		    MEMALIGN_MAGIC) {
323*7c478bd9Sstevel@tonic-gate 			message = "invalid or corrupted buffer";
324*7c478bd9Sstevel@tonic-gate 			break;
325*7c478bd9Sstevel@tonic-gate 		}
326*7c478bd9Sstevel@tonic-gate 		size += high_size << 32;
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 		/*
329*7c478bd9Sstevel@tonic-gate 		 * destroy the main tag's malloc_stat
330*7c478bd9Sstevel@tonic-gate 		 */
331*7c478bd9Sstevel@tonic-gate 		if (do_free)
332*7c478bd9Sstevel@tonic-gate 			(buf + 1)->malloc_stat = UMEM_FREE_PATTERN_32;
333*7c478bd9Sstevel@tonic-gate #endif
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 		base = (void *)buf;
336*7c478bd9Sstevel@tonic-gate 		data_size = size - overhead;
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 		if (do_free)
339*7c478bd9Sstevel@tonic-gate 			buf->malloc_stat = UMEM_FREE_PATTERN_32;
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 		goto process_memalign;
342*7c478bd9Sstevel@tonic-gate 	}
343*7c478bd9Sstevel@tonic-gate 	default:
344*7c478bd9Sstevel@tonic-gate 		if (buf->malloc_stat == UMEM_FREE_PATTERN_32)
345*7c478bd9Sstevel@tonic-gate 			message = "double-free or invalid buffer";
346*7c478bd9Sstevel@tonic-gate 		else
347*7c478bd9Sstevel@tonic-gate 			message = "invalid or corrupted buffer";
348*7c478bd9Sstevel@tonic-gate 		break;
349*7c478bd9Sstevel@tonic-gate 	}
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 	umem_err_recoverable("%s(%p): %s\n",
352*7c478bd9Sstevel@tonic-gate 	    do_free? "free" : "realloc", buf_arg, message);
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 	errno = old_errno;
355*7c478bd9Sstevel@tonic-gate 	return (0);
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate process_malloc:
358*7c478bd9Sstevel@tonic-gate 	if (do_free)
359*7c478bd9Sstevel@tonic-gate 		_umem_free(base, size);
360*7c478bd9Sstevel@tonic-gate 	else
361*7c478bd9Sstevel@tonic-gate 		*data_size_arg = data_size;
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate 	errno = old_errno;
364*7c478bd9Sstevel@tonic-gate 	return (1);
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate process_memalign:
367*7c478bd9Sstevel@tonic-gate 	if (do_free)
368*7c478bd9Sstevel@tonic-gate 		vmem_xfree(umem_memalign_arena, base, size);
369*7c478bd9Sstevel@tonic-gate 	else
370*7c478bd9Sstevel@tonic-gate 		*data_size_arg = data_size;
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	errno = old_errno;
373*7c478bd9Sstevel@tonic-gate 	return (1);
374*7c478bd9Sstevel@tonic-gate }
375*7c478bd9Sstevel@tonic-gate 
376*7c478bd9Sstevel@tonic-gate void
377*7c478bd9Sstevel@tonic-gate free(void *buf)
378*7c478bd9Sstevel@tonic-gate {
379*7c478bd9Sstevel@tonic-gate 	if (buf == NULL)
380*7c478bd9Sstevel@tonic-gate 		return;
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 	/*
383*7c478bd9Sstevel@tonic-gate 	 * Process buf, freeing it if it is not corrupt.
384*7c478bd9Sstevel@tonic-gate 	 */
385*7c478bd9Sstevel@tonic-gate 	(void) process_free(buf, 1, NULL);
386*7c478bd9Sstevel@tonic-gate }
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate void *
389*7c478bd9Sstevel@tonic-gate realloc(void *buf_arg, size_t newsize)
390*7c478bd9Sstevel@tonic-gate {
391*7c478bd9Sstevel@tonic-gate 	size_t oldsize;
392*7c478bd9Sstevel@tonic-gate 	void *buf;
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 	if (buf_arg == NULL)
395*7c478bd9Sstevel@tonic-gate 		return (malloc(newsize));
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate 	/*
398*7c478bd9Sstevel@tonic-gate 	 * get the old data size without freeing the buffer
399*7c478bd9Sstevel@tonic-gate 	 */
400*7c478bd9Sstevel@tonic-gate 	if (process_free(buf_arg, 0, &oldsize) == 0) {
401*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
402*7c478bd9Sstevel@tonic-gate 		return (NULL);
403*7c478bd9Sstevel@tonic-gate 	}
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate 	if (newsize == oldsize)		/* size didn't change */
406*7c478bd9Sstevel@tonic-gate 		return (buf_arg);
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 	buf = malloc(newsize);
409*7c478bd9Sstevel@tonic-gate 	if (buf == NULL)
410*7c478bd9Sstevel@tonic-gate 		return (NULL);
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate 	(void) memcpy(buf, buf_arg, MIN(newsize, oldsize));
413*7c478bd9Sstevel@tonic-gate 	free(buf_arg);
414*7c478bd9Sstevel@tonic-gate 	return (buf);
415*7c478bd9Sstevel@tonic-gate }
416