xref: /onnv-gate/usr/src/lib/libc/port/threads/alloc.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
56515Sraf  * Common Development and Distribution License (the "License").
66515Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
216515Sraf 
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include "lint.h"
270Sstevel@tonic-gate #include "thr_uberdata.h"
280Sstevel@tonic-gate #include <sys/syscall.h>
290Sstevel@tonic-gate 
300Sstevel@tonic-gate extern int __systemcall6(sysret_t *, int, ...);
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * This is a small and simple power of two memory allocator that is
340Sstevel@tonic-gate  * used internally by libc.  Allocations are fast and memory is never
350Sstevel@tonic-gate  * returned to the system, except for allocations of 64 Kbytes and larger,
360Sstevel@tonic-gate  * which are simply mmap()ed and munmap()ed as needed.  Smaller allocations
370Sstevel@tonic-gate  * (minimum size is 64 bytes) are obtained from mmap() of 64K chunks
380Sstevel@tonic-gate  * broken up into unit allocations and maintained on free lists.
390Sstevel@tonic-gate  * The interface requires the caller to keep track of the size of an
400Sstevel@tonic-gate  * allocated block and to pass that size back when freeing a block.
410Sstevel@tonic-gate  *
420Sstevel@tonic-gate  * This allocator is called during initialization, from code called
430Sstevel@tonic-gate  * from the dynamic linker, so it must not call anything that might
440Sstevel@tonic-gate  * re-invoke the dynamic linker to resolve a symbol.  That is,
450Sstevel@tonic-gate  * it must only call functions that are wholly private to libc.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * Also, this allocator must be unique across all link maps
480Sstevel@tonic-gate  * because pointers returned by lmalloc() are stored in the
490Sstevel@tonic-gate  * thread structure, which is constant across all link maps.
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  * Memory blocks returned by lmalloc() are initialized to zero.
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #define	MINSIZE		64	/* (1 << MINSHIFT) */
550Sstevel@tonic-gate #define	MINSHIFT	6
560Sstevel@tonic-gate #define	CHUNKSIZE	(64 * 1024)
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * bucketnum	allocation size
600Sstevel@tonic-gate  * 0		64
610Sstevel@tonic-gate  * 1		128
620Sstevel@tonic-gate  * 2		256
630Sstevel@tonic-gate  * 3		512
640Sstevel@tonic-gate  * 4		1024
650Sstevel@tonic-gate  * 5		2048
660Sstevel@tonic-gate  * 6		4096
670Sstevel@tonic-gate  * 7		8192
680Sstevel@tonic-gate  * 8		16384
690Sstevel@tonic-gate  * 9		32768
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * See "thr_uberdata.h" for the definition of bucket_t.
740Sstevel@tonic-gate  * The 10 (NBUCKETS) buckets are allocated in uberdata.
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * Performance hack:
790Sstevel@tonic-gate  *
800Sstevel@tonic-gate  * On the very first lmalloc(), before any memory has been allocated,
810Sstevel@tonic-gate  * mmap() a 24K block of memory and carve out six 2K chunks, each
820Sstevel@tonic-gate  * of which is subdivided for the initial allocations from buckets
830Sstevel@tonic-gate  * 0, 1, 2, 3, 4 and 5, giving them initial numbers of elements
840Sstevel@tonic-gate  * 32, 16, 8, 4, 2 and 1, respectively.  The remaining 12K is cut
850Sstevel@tonic-gate  * into one 4K buffer for bucket 6 and one 8K buffer for bucket 7.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * This results in almost all simple single-threaded processes,
880Sstevel@tonic-gate  * such as those employed in the kenbus test suite, having to
890Sstevel@tonic-gate  * allocate only this one 24K block during their lifetimes.
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #define	SUBCHUNKSIZE	2048
930Sstevel@tonic-gate #define	BASE_SIZE	(24 * 1024)
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static void
initial_allocation(bucket_t * bp)960Sstevel@tonic-gate initial_allocation(bucket_t *bp)	/* &__uberdata.bucket[0] */
970Sstevel@tonic-gate {
980Sstevel@tonic-gate 	sysret_t rval;
990Sstevel@tonic-gate 	void *ptr;
1000Sstevel@tonic-gate 	size_t size;
1010Sstevel@tonic-gate 	size_t n;
1020Sstevel@tonic-gate 	int bucketnum;
1030Sstevel@tonic-gate 	void *base;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	/*
1060Sstevel@tonic-gate 	 * We do this seemingly obtuse call to __systemcall6(SYS_mmap)
1070Sstevel@tonic-gate 	 * instead of simply calling mmap() directly because, if the
1080Sstevel@tonic-gate 	 * mmap() system call fails, we must make sure that __cerror()
1096515Sraf 	 * is not called, because that would call ___errno()
1100Sstevel@tonic-gate 	 * which would dereference curthread and, because we are very
1110Sstevel@tonic-gate 	 * early in libc initialization, curthread is NULL and we would
1120Sstevel@tonic-gate 	 * draw a hard-to-debug SIGSEGV core dump, or worse.
1130Sstevel@tonic-gate 	 * We opt to give a thread panic message instead.
1140Sstevel@tonic-gate 	 */
1150Sstevel@tonic-gate 	if (__systemcall6(&rval, SYS_mmap, CHUNKSIZE, BASE_SIZE,
1160Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE | PROT_EXEC,
1170Sstevel@tonic-gate 	    _MAP_NEW | MAP_PRIVATE | MAP_ANON | MAP_ALIGN, -1L, (off_t)0) != 0)
1180Sstevel@tonic-gate 		thr_panic("initial allocation failed; swap space exhausted?");
1190Sstevel@tonic-gate 	base = (void *)rval.sys_rval1;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	for (bucketnum = 0; bucketnum < 6; bucketnum++, bp++) {
1220Sstevel@tonic-gate 		size = (size_t)MINSIZE << bucketnum;
1230Sstevel@tonic-gate 		n = SUBCHUNKSIZE / size;
1240Sstevel@tonic-gate 		ptr = (void *)((caddr_t)base + bucketnum * SUBCHUNKSIZE);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 		ASSERT(bp->free_list == NULL);
1270Sstevel@tonic-gate 		bp->free_list = ptr;
1280Sstevel@tonic-gate 		while (--n != 0) {
1290Sstevel@tonic-gate 			void *next = (void *)((caddr_t)ptr + size);
1300Sstevel@tonic-gate 			*(void **)ptr = next;
1310Sstevel@tonic-gate 			ptr = next;
1320Sstevel@tonic-gate 		}
1330Sstevel@tonic-gate 		*(void **)ptr = NULL;
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	ptr = (void *)((caddr_t)base + bucketnum * SUBCHUNKSIZE);
1370Sstevel@tonic-gate 	ASSERT(bp->free_list == NULL);
1380Sstevel@tonic-gate 	bp->free_list = ptr;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	ptr = (void *)((caddr_t)ptr + 2 * SUBCHUNKSIZE);
1410Sstevel@tonic-gate 	bp++;
1420Sstevel@tonic-gate 	ASSERT(bp->free_list == NULL);
1430Sstevel@tonic-gate 	bp->free_list = ptr;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	ASSERT(((caddr_t)ptr - (caddr_t)base + 4 * SUBCHUNKSIZE) == BASE_SIZE);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate 
148*13093SRoger.Faulkner@Oracle.COM /*
149*13093SRoger.Faulkner@Oracle.COM  * This highbit code is the same as the code in fls_impl().
150*13093SRoger.Faulkner@Oracle.COM  * We inline it here for speed.
151*13093SRoger.Faulkner@Oracle.COM  */
1520Sstevel@tonic-gate static int
getbucketnum(size_t size)1530Sstevel@tonic-gate getbucketnum(size_t size)
1540Sstevel@tonic-gate {
155*13093SRoger.Faulkner@Oracle.COM 	int highbit = 1;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	if (size-- <= MINSIZE)
1580Sstevel@tonic-gate 		return (0);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate #ifdef _LP64
1610Sstevel@tonic-gate 	if (size & 0xffffffff00000000ul)
1620Sstevel@tonic-gate 		highbit += 32, size >>= 32;
1630Sstevel@tonic-gate #endif
1640Sstevel@tonic-gate 	if (size & 0xffff0000)
1650Sstevel@tonic-gate 		highbit += 16, size >>= 16;
1660Sstevel@tonic-gate 	if (size & 0xff00)
1670Sstevel@tonic-gate 		highbit += 8, size >>= 8;
1680Sstevel@tonic-gate 	if (size & 0xf0)
1690Sstevel@tonic-gate 		highbit += 4, size >>= 4;
1700Sstevel@tonic-gate 	if (size & 0xc)
1710Sstevel@tonic-gate 		highbit += 2, size >>= 2;
1720Sstevel@tonic-gate 	if (size & 0x2)
1730Sstevel@tonic-gate 		highbit += 1;
1740Sstevel@tonic-gate 
175*13093SRoger.Faulkner@Oracle.COM 	ASSERT(highbit > MINSHIFT);
176*13093SRoger.Faulkner@Oracle.COM 	return (highbit - MINSHIFT);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate void *
lmalloc(size_t size)1800Sstevel@tonic-gate lmalloc(size_t size)
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate 	int bucketnum = getbucketnum(size);
1830Sstevel@tonic-gate 	ulwp_t *self;
1840Sstevel@tonic-gate 	uberdata_t *udp;
1850Sstevel@tonic-gate 	bucket_t *bp;
1860Sstevel@tonic-gate 	void *ptr;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	/*
1890Sstevel@tonic-gate 	 * ulwp_t structures must be allocated from a rwx mapping since it
1900Sstevel@tonic-gate 	 * is a normal data object _and_ it contains instructions that are
1910Sstevel@tonic-gate 	 * executed for user-land DTrace tracing with the fasttrap provider.
1920Sstevel@tonic-gate 	 */
1930Sstevel@tonic-gate 	int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	/* round size up to the proper power of 2 */
1960Sstevel@tonic-gate 	size = (size_t)MINSIZE << bucketnum;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	if (bucketnum >= NBUCKETS) {
1990Sstevel@tonic-gate 		/* mmap() allocates memory already set to zero */
2006515Sraf 		ptr = mmap((void *)CHUNKSIZE, size, prot,
2016515Sraf 		    MAP_PRIVATE|MAP_ANON|MAP_ALIGN, -1, (off_t)0);
2020Sstevel@tonic-gate 		if (ptr == MAP_FAILED)
2030Sstevel@tonic-gate 			ptr = NULL;
2040Sstevel@tonic-gate 		return (ptr);
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	if ((self = __curthread()) == NULL)
2080Sstevel@tonic-gate 		udp = &__uberdata;
2090Sstevel@tonic-gate 	else
2100Sstevel@tonic-gate 		udp = self->ul_uberdata;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	if (udp->bucket_init == 0) {
2130Sstevel@tonic-gate 		ASSERT(udp->nthreads == 0);
2140Sstevel@tonic-gate 		initial_allocation(udp->bucket);
2150Sstevel@tonic-gate 		udp->bucket_init = 1;
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	bp = &udp->bucket[bucketnum];
2190Sstevel@tonic-gate 	if (self != NULL)
2200Sstevel@tonic-gate 		lmutex_lock(&bp->bucket_lock);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	if ((ptr = bp->free_list) == NULL) {
2230Sstevel@tonic-gate 		size_t bsize;
2240Sstevel@tonic-gate 		size_t n;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 		/*
2270Sstevel@tonic-gate 		 * Double the number of chunks mmap()ed each time,
2280Sstevel@tonic-gate 		 * in case of large numbers of allocations.
2290Sstevel@tonic-gate 		 */
2300Sstevel@tonic-gate 		if (bp->chunks == 0)
2310Sstevel@tonic-gate 			bp->chunks = 1;
2320Sstevel@tonic-gate 		else
2330Sstevel@tonic-gate 			bp->chunks <<= 1;
2340Sstevel@tonic-gate 		for (;;) {
2350Sstevel@tonic-gate 			bsize = CHUNKSIZE * bp->chunks;
2360Sstevel@tonic-gate 			n = bsize / size;
2376515Sraf 			ptr = mmap((void *)CHUNKSIZE, bsize, prot,
2386515Sraf 			    MAP_PRIVATE|MAP_ANON|MAP_ALIGN, -1, (off_t)0);
2390Sstevel@tonic-gate 			if (ptr != MAP_FAILED)
2400Sstevel@tonic-gate 				break;
2410Sstevel@tonic-gate 			/* try a smaller chunk allocation */
2420Sstevel@tonic-gate 			if ((bp->chunks >>= 1) == 0) {
2430Sstevel@tonic-gate 				if (self != NULL)
2440Sstevel@tonic-gate 					lmutex_unlock(&bp->bucket_lock);
2450Sstevel@tonic-gate 				return (NULL);
2460Sstevel@tonic-gate 			}
2470Sstevel@tonic-gate 		}
2480Sstevel@tonic-gate 		bp->free_list = ptr;
2490Sstevel@tonic-gate 		while (--n != 0) {
2500Sstevel@tonic-gate 			void *next = (void *)((caddr_t)ptr + size);
2510Sstevel@tonic-gate 			*(void **)ptr = next;
2520Sstevel@tonic-gate 			ptr = next;
2530Sstevel@tonic-gate 		}
2540Sstevel@tonic-gate 		*(void **)ptr = NULL;
2550Sstevel@tonic-gate 		ptr = bp->free_list;
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 	bp->free_list = *(void **)ptr;
2580Sstevel@tonic-gate 	if (self != NULL)
2590Sstevel@tonic-gate 		lmutex_unlock(&bp->bucket_lock);
2600Sstevel@tonic-gate 	/*
2610Sstevel@tonic-gate 	 * We maintain the free list already zeroed except for the pointer
2620Sstevel@tonic-gate 	 * stored at the head of the block (mmap() allocates memory already
2630Sstevel@tonic-gate 	 * set to zero), so all we have to do is zero out the pointer.
2640Sstevel@tonic-gate 	 */
2650Sstevel@tonic-gate 	*(void **)ptr = NULL;
2660Sstevel@tonic-gate 	return (ptr);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate void
lfree(void * ptr,size_t size)2700Sstevel@tonic-gate lfree(void *ptr, size_t size)
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate 	int bucketnum = getbucketnum(size);
2730Sstevel@tonic-gate 	ulwp_t *self;
2740Sstevel@tonic-gate 	bucket_t *bp;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	/* round size up to the proper power of 2 */
2770Sstevel@tonic-gate 	size = (size_t)MINSIZE << bucketnum;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if (bucketnum >= NBUCKETS) {
2800Sstevel@tonic-gate 		/* see comment below */
2810Sstevel@tonic-gate 		if (((uintptr_t)ptr & (CHUNKSIZE - 1)) != 0)
2820Sstevel@tonic-gate 			goto bad;
2836515Sraf 		(void) munmap(ptr, size);
2840Sstevel@tonic-gate 		return;
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	/*
2880Sstevel@tonic-gate 	 * If the low order bits are not all zero as expected, then panic.
2890Sstevel@tonic-gate 	 * This can be caused by an application calling, for example,
2900Sstevel@tonic-gate 	 * pthread_attr_destroy() without having first called
2910Sstevel@tonic-gate 	 * pthread_attr_init() (thereby passing uninitialized data
2920Sstevel@tonic-gate 	 * to pthread_attr_destroy() who then calls lfree() with
2930Sstevel@tonic-gate 	 * the uninitialized data).
2940Sstevel@tonic-gate 	 */
2950Sstevel@tonic-gate 	if (((uintptr_t)ptr & (size - 1)) != 0)
2960Sstevel@tonic-gate 		goto bad;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	/*
2990Sstevel@tonic-gate 	 * Zeroing the memory here saves time later when reallocating it.
3000Sstevel@tonic-gate 	 */
3016515Sraf 	(void) memset(ptr, 0, size);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if ((self = __curthread()) == NULL)
3040Sstevel@tonic-gate 		bp = &__uberdata.bucket[bucketnum];
3050Sstevel@tonic-gate 	else {
3060Sstevel@tonic-gate 		bp = &self->ul_uberdata->bucket[bucketnum];
3070Sstevel@tonic-gate 		lmutex_lock(&bp->bucket_lock);
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate 	*(void **)ptr = bp->free_list;
3100Sstevel@tonic-gate 	bp->free_list = ptr;
3110Sstevel@tonic-gate 	if (self != NULL)
3120Sstevel@tonic-gate 		lmutex_unlock(&bp->bucket_lock);
3130Sstevel@tonic-gate 	return;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate bad:
3160Sstevel@tonic-gate 	thr_panic("lfree() called with a misaligned pointer");
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate /*
3200Sstevel@tonic-gate  * The following functions can be used internally to libc
3210Sstevel@tonic-gate  * to make memory allocations in the style of malloc()/free()
3220Sstevel@tonic-gate  * (where the size of the allocation is not remembered by the caller)
3230Sstevel@tonic-gate  * but which are safe to use within critical sections, that is,
3240Sstevel@tonic-gate  * sections of code bounded by enter_critical()/exit_critical(),
3250Sstevel@tonic-gate  * lmutex_lock()/lmutex_unlock() or lrw_rdlock()/lrw_wrlock()/lrw_unlock().
3260Sstevel@tonic-gate  *
3270Sstevel@tonic-gate  * These functions must never be used to allocate memory that is
3280Sstevel@tonic-gate  * passed out of libc, for example by strdup(), because it is a
3290Sstevel@tonic-gate  * fatal error to free() an object allocated by libc_malloc().
3300Sstevel@tonic-gate  * Such objects can only be freed by calling libc_free().
3310Sstevel@tonic-gate  */
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate #ifdef	_LP64
3340Sstevel@tonic-gate #define	ALIGNMENT	16
3350Sstevel@tonic-gate #else
3360Sstevel@tonic-gate #define	ALIGNMENT	8
3370Sstevel@tonic-gate #endif
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate typedef union {
3400Sstevel@tonic-gate 	size_t	private_size;
3410Sstevel@tonic-gate 	char	private_align[ALIGNMENT];
3420Sstevel@tonic-gate } private_header_t;
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate void *
libc_malloc(size_t size)3450Sstevel@tonic-gate libc_malloc(size_t size)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	private_header_t *ptr;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	size = (size_t)MINSIZE << getbucketnum(size + sizeof (*ptr));
3500Sstevel@tonic-gate 	if ((ptr = lmalloc(size)) == NULL)
3510Sstevel@tonic-gate 		return (NULL);
3520Sstevel@tonic-gate 	ptr->private_size = size;
3530Sstevel@tonic-gate 	return (ptr + 1);
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate void *
libc_realloc(void * old,size_t size)3570Sstevel@tonic-gate libc_realloc(void *old, size_t size)
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate 	private_header_t *ptr;
3600Sstevel@tonic-gate 	void *new;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	size = (size_t)MINSIZE << getbucketnum(size + sizeof (*ptr));
3630Sstevel@tonic-gate 	if ((ptr = lmalloc(size)) == NULL)
3640Sstevel@tonic-gate 		return (NULL);
3650Sstevel@tonic-gate 	ptr->private_size = size;
3660Sstevel@tonic-gate 	new = ptr + 1;
3670Sstevel@tonic-gate 	if (old != NULL) {
3680Sstevel@tonic-gate 		ptr = (private_header_t *)old - 1;
3690Sstevel@tonic-gate 		if (size >= ptr->private_size)
3700Sstevel@tonic-gate 			size = ptr->private_size;
3716515Sraf 		(void) memcpy(new, old, size - sizeof (*ptr));
3720Sstevel@tonic-gate 		lfree(ptr, ptr->private_size);
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate 	return (new);
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate void
libc_free(void * p)3780Sstevel@tonic-gate libc_free(void *p)
3790Sstevel@tonic-gate {
3800Sstevel@tonic-gate 	private_header_t *ptr;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	if (p) {
3830Sstevel@tonic-gate 		ptr = (private_header_t *)p - 1;
3840Sstevel@tonic-gate 		lfree(ptr, ptr->private_size);
3850Sstevel@tonic-gate 	}
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate char *
libc_strdup(const char * s1)3890Sstevel@tonic-gate libc_strdup(const char *s1)
3900Sstevel@tonic-gate {
3910Sstevel@tonic-gate 	char *s2 = libc_malloc(strlen(s1) + 1);
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	if (s2)
3940Sstevel@tonic-gate 		(void) strcpy(s2, s1);
3950Sstevel@tonic-gate 	return (s2);
3960Sstevel@tonic-gate }
397