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