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