xref: /titanic_44/usr/src/lib/libumem/common/malloc.c (revision 87cd24809d33b40017e71ecba06bd30fc99fac6f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*87cd2480Sjwadams  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "umem_base.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "misc.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * malloc_data_t is an 8-byte structure which is located "before" the pointer
437c478bd9Sstevel@tonic-gate  * returned from {m,c,re}alloc and memalign.  The first four bytes give
447c478bd9Sstevel@tonic-gate  * information about the buffer, and the second four bytes are a status byte.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * See umem_impl.h for the various magic numbers used, and the size
477c478bd9Sstevel@tonic-gate  * encode/decode macros.
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * The 'size' of the buffer includes the tags.  That is, we encode the
507c478bd9Sstevel@tonic-gate  * argument to umem_alloc(), not the argument to malloc().
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate typedef struct malloc_data {
547c478bd9Sstevel@tonic-gate 	uint32_t malloc_size;
557c478bd9Sstevel@tonic-gate 	uint32_t malloc_stat; /* = UMEM_MALLOC_ENCODE(state, malloc_size) */
567c478bd9Sstevel@tonic-gate } malloc_data_t;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate void *
597c478bd9Sstevel@tonic-gate malloc(size_t size_arg)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate #ifdef _LP64
627c478bd9Sstevel@tonic-gate 	uint32_t high_size = 0;
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate 	size_t size;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	malloc_data_t *ret;
677c478bd9Sstevel@tonic-gate 	size = size_arg + sizeof (malloc_data_t);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #ifdef _LP64
707c478bd9Sstevel@tonic-gate 	if (size > UMEM_SECOND_ALIGN) {
717c478bd9Sstevel@tonic-gate 		size += sizeof (malloc_data_t);
727c478bd9Sstevel@tonic-gate 		high_size = (size >> 32);
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate #endif
757c478bd9Sstevel@tonic-gate 	if (size < size_arg) {
767c478bd9Sstevel@tonic-gate 		errno = ENOMEM;			/* overflow */
777c478bd9Sstevel@tonic-gate 		return (NULL);
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 	ret = (malloc_data_t *)_umem_alloc(size, UMEM_DEFAULT);
807c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
817c478bd9Sstevel@tonic-gate 		if (size <= UMEM_MAXBUF)
827c478bd9Sstevel@tonic-gate 			errno = EAGAIN;
837c478bd9Sstevel@tonic-gate 		else
847c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
857c478bd9Sstevel@tonic-gate 		return (NULL);
867c478bd9Sstevel@tonic-gate #ifdef _LP64
877c478bd9Sstevel@tonic-gate 	} else if (high_size > 0) {
887c478bd9Sstevel@tonic-gate 		uint32_t low_size = (uint32_t)size;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 		/*
917c478bd9Sstevel@tonic-gate 		 * uses different magic numbers to make it harder to
927c478bd9Sstevel@tonic-gate 		 * undetectably corrupt
937c478bd9Sstevel@tonic-gate 		 */
947c478bd9Sstevel@tonic-gate 		ret->malloc_size = high_size;
957c478bd9Sstevel@tonic-gate 		ret->malloc_stat = UMEM_MALLOC_ENCODE(MALLOC_MAGIC, high_size);
967c478bd9Sstevel@tonic-gate 		ret++;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 		ret->malloc_size = low_size;
997c478bd9Sstevel@tonic-gate 		ret->malloc_stat = UMEM_MALLOC_ENCODE(MALLOC_OVERSIZE_MAGIC,
1007c478bd9Sstevel@tonic-gate 		    low_size);
1017c478bd9Sstevel@tonic-gate 		ret++;
1027c478bd9Sstevel@tonic-gate 	} else if (size > UMEM_SECOND_ALIGN) {
1037c478bd9Sstevel@tonic-gate 		uint32_t low_size = (uint32_t)size;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		ret++; /* leave the first 8 bytes alone */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 		ret->malloc_size = low_size;
1087c478bd9Sstevel@tonic-gate 		ret->malloc_stat = UMEM_MALLOC_ENCODE(MALLOC_SECOND_MAGIC,
1097c478bd9Sstevel@tonic-gate 		    low_size);
1107c478bd9Sstevel@tonic-gate 		ret++;
1117c478bd9Sstevel@tonic-gate #endif
1127c478bd9Sstevel@tonic-gate 	} else {
1137c478bd9Sstevel@tonic-gate 		ret->malloc_size = size;
1147c478bd9Sstevel@tonic-gate 		ret->malloc_stat = UMEM_MALLOC_ENCODE(MALLOC_MAGIC, size);
1157c478bd9Sstevel@tonic-gate 		ret++;
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 	return ((void *)ret);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate void *
1217c478bd9Sstevel@tonic-gate calloc(size_t nelem, size_t elsize)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	size_t size = nelem * elsize;
1247c478bd9Sstevel@tonic-gate 	void *retval;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if (nelem > 0 && elsize > 0 && size/nelem != elsize) {
1277c478bd9Sstevel@tonic-gate 		errno = ENOMEM;				/* overflow */
1287c478bd9Sstevel@tonic-gate 		return (NULL);
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	retval = malloc(size);
1327c478bd9Sstevel@tonic-gate 	if (retval == NULL)
1337c478bd9Sstevel@tonic-gate 		return (NULL);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	(void) memset(retval, 0, size);
1367c478bd9Sstevel@tonic-gate 	return (retval);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * memalign uses vmem_xalloc to do its work.
1417c478bd9Sstevel@tonic-gate  *
1427c478bd9Sstevel@tonic-gate  * in 64-bit, the memaligned buffer always has two tags.  This simplifies the
1437c478bd9Sstevel@tonic-gate  * code.
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate void *
1477c478bd9Sstevel@tonic-gate memalign(size_t align, size_t size_arg)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	size_t size;
1507c478bd9Sstevel@tonic-gate 	uintptr_t phase;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	void *buf;
1537c478bd9Sstevel@tonic-gate 	malloc_data_t *ret;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	size_t overhead;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if (size_arg == 0 || align == 0 || (align & (align - 1)) != 0) {
1587c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1597c478bd9Sstevel@tonic-gate 		return (NULL);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	/*
1637c478bd9Sstevel@tonic-gate 	 * if malloc provides the required alignment, use it.
1647c478bd9Sstevel@tonic-gate 	 */
1657c478bd9Sstevel@tonic-gate 	if (align <= UMEM_ALIGN ||
1667c478bd9Sstevel@tonic-gate 	    (align <= UMEM_SECOND_ALIGN && size_arg >= UMEM_SECOND_ALIGN))
1677c478bd9Sstevel@tonic-gate 		return (malloc(size_arg));
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate #ifdef _LP64
1707c478bd9Sstevel@tonic-gate 	overhead = 2 * sizeof (malloc_data_t);
1717c478bd9Sstevel@tonic-gate #else
1727c478bd9Sstevel@tonic-gate 	overhead = sizeof (malloc_data_t);
1737c478bd9Sstevel@tonic-gate #endif
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	ASSERT(overhead <= align);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	size = size_arg + overhead;
1787c478bd9Sstevel@tonic-gate 	phase = align - overhead;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (umem_memalign_arena == NULL && umem_init() == 0) {
1817c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1827c478bd9Sstevel@tonic-gate 		return (NULL);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (size < size_arg) {
1867c478bd9Sstevel@tonic-gate 		errno = ENOMEM;			/* overflow */
1877c478bd9Sstevel@tonic-gate 		return (NULL);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	buf = vmem_xalloc(umem_memalign_arena, size, align, phase,
1917c478bd9Sstevel@tonic-gate 	    0, NULL, NULL, VM_NOSLEEP);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
1947c478bd9Sstevel@tonic-gate 		if ((size_arg + align) <= UMEM_MAXBUF)
1957c478bd9Sstevel@tonic-gate 			errno = EAGAIN;
1967c478bd9Sstevel@tonic-gate 		else
1977c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 		return (NULL);
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	ret = (malloc_data_t *)buf;
2037c478bd9Sstevel@tonic-gate 	{
2047c478bd9Sstevel@tonic-gate 		uint32_t low_size = (uint32_t)size;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate #ifdef _LP64
2077c478bd9Sstevel@tonic-gate 		uint32_t high_size = (uint32_t)(size >> 32);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		ret->malloc_size = high_size;
2107c478bd9Sstevel@tonic-gate 		ret->malloc_stat = UMEM_MALLOC_ENCODE(MEMALIGN_MAGIC,
2117c478bd9Sstevel@tonic-gate 		    high_size);
2127c478bd9Sstevel@tonic-gate 		ret++;
2137c478bd9Sstevel@tonic-gate #endif
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		ret->malloc_size = low_size;
2167c478bd9Sstevel@tonic-gate 		ret->malloc_stat = UMEM_MALLOC_ENCODE(MEMALIGN_MAGIC, low_size);
2177c478bd9Sstevel@tonic-gate 		ret++;
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	ASSERT(P2PHASE((uintptr_t)ret, align) == 0);
2217c478bd9Sstevel@tonic-gate 	ASSERT((void *)((uintptr_t)ret - overhead) == buf);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	return ((void *)ret);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate void *
2277c478bd9Sstevel@tonic-gate valloc(size_t size)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	return (memalign(pagesize, size));
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /*
2337c478bd9Sstevel@tonic-gate  * process_free:
2347c478bd9Sstevel@tonic-gate  *
2357c478bd9Sstevel@tonic-gate  * Pulls information out of a buffer pointer, and optionally free it.
2367c478bd9Sstevel@tonic-gate  * This is used by free() and realloc() to process buffers.
2377c478bd9Sstevel@tonic-gate  *
2387c478bd9Sstevel@tonic-gate  * On failure, calls umem_err_recoverable() with an appropriate message
2397c478bd9Sstevel@tonic-gate  * On success, returns the data size through *data_size_arg, if (!is_free).
2407c478bd9Sstevel@tonic-gate  *
2417c478bd9Sstevel@tonic-gate  * Preserves errno, since free()'s semantics require it.
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate static int
2457c478bd9Sstevel@tonic-gate process_free(void *buf_arg,
2467c478bd9Sstevel@tonic-gate     int do_free,		/* free the buffer, or just get its size? */
2477c478bd9Sstevel@tonic-gate     size_t *data_size_arg)	/* output: bytes of data in buf_arg */
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	malloc_data_t *buf;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	void *base;
2527c478bd9Sstevel@tonic-gate 	size_t size;
2537c478bd9Sstevel@tonic-gate 	size_t data_size;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	const char *message;
2567c478bd9Sstevel@tonic-gate 	int old_errno = errno;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	buf = (malloc_data_t *)buf_arg;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	buf--;
2617c478bd9Sstevel@tonic-gate 	size = buf->malloc_size;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	switch (UMEM_MALLOC_DECODE(buf->malloc_stat, size)) {
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	case MALLOC_MAGIC:
2667c478bd9Sstevel@tonic-gate 		base = (void *)buf;
2677c478bd9Sstevel@tonic-gate 		data_size = size - sizeof (malloc_data_t);
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		if (do_free)
2707c478bd9Sstevel@tonic-gate 			buf->malloc_stat = UMEM_FREE_PATTERN_32;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 		goto process_malloc;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate #ifdef _LP64
2757c478bd9Sstevel@tonic-gate 	case MALLOC_SECOND_MAGIC:
2767c478bd9Sstevel@tonic-gate 		base = (void *)(buf - 1);
2777c478bd9Sstevel@tonic-gate 		data_size = size - 2 * sizeof (malloc_data_t);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		if (do_free)
2807c478bd9Sstevel@tonic-gate 			buf->malloc_stat = UMEM_FREE_PATTERN_32;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 		goto process_malloc;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	case MALLOC_OVERSIZE_MAGIC: {
2857c478bd9Sstevel@tonic-gate 		size_t high_size;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		buf--;
2887c478bd9Sstevel@tonic-gate 		high_size = buf->malloc_size;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		if (UMEM_MALLOC_DECODE(buf->malloc_stat, high_size) !=
2917c478bd9Sstevel@tonic-gate 		    MALLOC_MAGIC) {
2927c478bd9Sstevel@tonic-gate 			message = "invalid or corrupted buffer";
2937c478bd9Sstevel@tonic-gate 			break;
2947c478bd9Sstevel@tonic-gate 		}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 		size += high_size << 32;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 		base = (void *)buf;
2997c478bd9Sstevel@tonic-gate 		data_size = size - 2 * sizeof (malloc_data_t);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		if (do_free) {
3027c478bd9Sstevel@tonic-gate 			buf->malloc_stat = UMEM_FREE_PATTERN_32;
3037c478bd9Sstevel@tonic-gate 			(buf + 1)->malloc_stat = UMEM_FREE_PATTERN_32;
3047c478bd9Sstevel@tonic-gate 		}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 		goto process_malloc;
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate #endif
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	case MEMALIGN_MAGIC: {
3117c478bd9Sstevel@tonic-gate 		size_t overhead = sizeof (malloc_data_t);
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate #ifdef _LP64
3147c478bd9Sstevel@tonic-gate 		size_t high_size;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 		overhead += sizeof (malloc_data_t);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 		buf--;
3197c478bd9Sstevel@tonic-gate 		high_size = buf->malloc_size;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 		if (UMEM_MALLOC_DECODE(buf->malloc_stat, high_size) !=
3227c478bd9Sstevel@tonic-gate 		    MEMALIGN_MAGIC) {
3237c478bd9Sstevel@tonic-gate 			message = "invalid or corrupted buffer";
3247c478bd9Sstevel@tonic-gate 			break;
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 		size += high_size << 32;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 		/*
3297c478bd9Sstevel@tonic-gate 		 * destroy the main tag's malloc_stat
3307c478bd9Sstevel@tonic-gate 		 */
3317c478bd9Sstevel@tonic-gate 		if (do_free)
3327c478bd9Sstevel@tonic-gate 			(buf + 1)->malloc_stat = UMEM_FREE_PATTERN_32;
3337c478bd9Sstevel@tonic-gate #endif
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		base = (void *)buf;
3367c478bd9Sstevel@tonic-gate 		data_size = size - overhead;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 		if (do_free)
3397c478bd9Sstevel@tonic-gate 			buf->malloc_stat = UMEM_FREE_PATTERN_32;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		goto process_memalign;
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 	default:
3447c478bd9Sstevel@tonic-gate 		if (buf->malloc_stat == UMEM_FREE_PATTERN_32)
3457c478bd9Sstevel@tonic-gate 			message = "double-free or invalid buffer";
3467c478bd9Sstevel@tonic-gate 		else
3477c478bd9Sstevel@tonic-gate 			message = "invalid or corrupted buffer";
3487c478bd9Sstevel@tonic-gate 		break;
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	umem_err_recoverable("%s(%p): %s\n",
3527c478bd9Sstevel@tonic-gate 	    do_free? "free" : "realloc", buf_arg, message);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	errno = old_errno;
3557c478bd9Sstevel@tonic-gate 	return (0);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate process_malloc:
3587c478bd9Sstevel@tonic-gate 	if (do_free)
3597c478bd9Sstevel@tonic-gate 		_umem_free(base, size);
3607c478bd9Sstevel@tonic-gate 	else
3617c478bd9Sstevel@tonic-gate 		*data_size_arg = data_size;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	errno = old_errno;
3647c478bd9Sstevel@tonic-gate 	return (1);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate process_memalign:
3677c478bd9Sstevel@tonic-gate 	if (do_free)
3687c478bd9Sstevel@tonic-gate 		vmem_xfree(umem_memalign_arena, base, size);
3697c478bd9Sstevel@tonic-gate 	else
3707c478bd9Sstevel@tonic-gate 		*data_size_arg = data_size;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	errno = old_errno;
3737c478bd9Sstevel@tonic-gate 	return (1);
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate void
3777c478bd9Sstevel@tonic-gate free(void *buf)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	if (buf == NULL)
3807c478bd9Sstevel@tonic-gate 		return;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/*
3837c478bd9Sstevel@tonic-gate 	 * Process buf, freeing it if it is not corrupt.
3847c478bd9Sstevel@tonic-gate 	 */
3857c478bd9Sstevel@tonic-gate 	(void) process_free(buf, 1, NULL);
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate void *
3897c478bd9Sstevel@tonic-gate realloc(void *buf_arg, size_t newsize)
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate 	size_t oldsize;
3927c478bd9Sstevel@tonic-gate 	void *buf;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	if (buf_arg == NULL)
3957c478bd9Sstevel@tonic-gate 		return (malloc(newsize));
3967c478bd9Sstevel@tonic-gate 
397*87cd2480Sjwadams 	if (newsize == 0) {
398*87cd2480Sjwadams 		free(buf_arg);
399*87cd2480Sjwadams 		return (NULL);
400*87cd2480Sjwadams 	}
401*87cd2480Sjwadams 
4027c478bd9Sstevel@tonic-gate 	/*
4037c478bd9Sstevel@tonic-gate 	 * get the old data size without freeing the buffer
4047c478bd9Sstevel@tonic-gate 	 */
4057c478bd9Sstevel@tonic-gate 	if (process_free(buf_arg, 0, &oldsize) == 0) {
4067c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4077c478bd9Sstevel@tonic-gate 		return (NULL);
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	if (newsize == oldsize)		/* size didn't change */
4117c478bd9Sstevel@tonic-gate 		return (buf_arg);
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	buf = malloc(newsize);
4147c478bd9Sstevel@tonic-gate 	if (buf == NULL)
4157c478bd9Sstevel@tonic-gate 		return (NULL);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	(void) memcpy(buf, buf_arg, MIN(newsize, oldsize));
4187c478bd9Sstevel@tonic-gate 	free(buf_arg);
4197c478bd9Sstevel@tonic-gate 	return (buf);
4207c478bd9Sstevel@tonic-gate }
421