xref: /titanic_51/usr/src/cmd/sgs/rtld/common/malloc.c (revision 31c6d826a7f7a4ee7d83c8e99f25d82a4a248076)
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
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * 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  */
215aefb655Srie 
227c478bd9Sstevel@tonic-gate /*
2356deab07SRod Evans  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267257d1b4Sraf 
277257d1b4Sraf /*
287257d1b4Sraf  *	Copyright (c) 1988 AT&T
297257d1b4Sraf  *	  All Rights Reserved
307257d1b4Sraf  */
317257d1b4Sraf 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Simplified version of malloc(), calloc() and free(), to be linked with
347c478bd9Sstevel@tonic-gate  * utilities that use [s]brk() and do not define their own version of the
357c478bd9Sstevel@tonic-gate  * routines.
367c478bd9Sstevel@tonic-gate  * The algorithm maps /dev/zero to get extra memory space.
377c478bd9Sstevel@tonic-gate  * Each call to mmap() creates a page. The pages are linked in a list.
387c478bd9Sstevel@tonic-gate  * Each page is divided in blocks. There is at least one block in a page.
397c478bd9Sstevel@tonic-gate  * New memory chunks are allocated on a first-fit basis.
407c478bd9Sstevel@tonic-gate  * Freed blocks are joined in larger blocks. Free pages are unmapped.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include	<stdlib.h>
447c478bd9Sstevel@tonic-gate #include	<sys/types.h>
457c478bd9Sstevel@tonic-gate #include	<sys/mman.h>
467c478bd9Sstevel@tonic-gate #include	<sys/debug.h>
477c478bd9Sstevel@tonic-gate #include	<memory.h>
487c478bd9Sstevel@tonic-gate #include	"_rtld.h"
497c478bd9Sstevel@tonic-gate #include	"msg.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate struct block {
527c478bd9Sstevel@tonic-gate 	size_t		size;		/* Space available for user */
537c478bd9Sstevel@tonic-gate 	struct page	*page;		/* Backwards reference to page */
547c478bd9Sstevel@tonic-gate 	int		status;
557c478bd9Sstevel@tonic-gate 	struct block	*next;
567c478bd9Sstevel@tonic-gate 	void *		memstart[1];
577c478bd9Sstevel@tonic-gate };
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate struct page {
607c478bd9Sstevel@tonic-gate 	size_t		size;		/* Total page size (incl. header) */
617c478bd9Sstevel@tonic-gate 	struct page	*next;
627c478bd9Sstevel@tonic-gate 	struct block	block[1];
637c478bd9Sstevel@tonic-gate };
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	FREE	0
667c478bd9Sstevel@tonic-gate #define	BUSY	1
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	HDR_BLOCK	(sizeof (struct block) - sizeof (void *))
697c478bd9Sstevel@tonic-gate #define	HDR_PAGE	(sizeof (struct page) - sizeof (void *))
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static struct page	*memstart;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #if	DEBUG
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * When built for debugging, scribble a pattern over newly allocated and
767c478bd9Sstevel@tonic-gate  * freed memory.
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate #define	NEWMEM		0
797c478bd9Sstevel@tonic-gate #define	FREMEM		1
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /* LINTED */
827c478bd9Sstevel@tonic-gate const ulong_t	patterns[] = {
83b3fbe5e6Sseizo 	(ulong_t)0xbaddcafebaddcafeULL, (ulong_t)0xdeadbeefdeadbeefULL
847c478bd9Sstevel@tonic-gate };
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static void
877c478bd9Sstevel@tonic-gate scribble(ulong_t *membgn, int pattern, size_t size)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	size_t	memsize = size / sizeof (ulong_t);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	while (memsize--) {
927c478bd9Sstevel@tonic-gate 		if (pattern == FREMEM)
937c478bd9Sstevel@tonic-gate 			ASSERT(*membgn != patterns[pattern]);
947c478bd9Sstevel@tonic-gate 		*membgn++ = patterns[pattern];
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate #endif
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * Defragmentation
1017c478bd9Sstevel@tonic-gate  */
10256deab07SRod Evans void
10356deab07SRod Evans defrag()
1047c478bd9Sstevel@tonic-gate {
10556deab07SRod Evans 	struct page	*page;
10656deab07SRod Evans 	Aliste		idx;
10756deab07SRod Evans 
10856deab07SRod Evans 	for (APLIST_TRAVERSE(free_alp, idx, page)) {
1097c478bd9Sstevel@tonic-gate 		struct block	*block;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 		for (block = page->block; block; block = block->next) {
1127c478bd9Sstevel@tonic-gate 			struct block	*block2;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 			if (block->status == BUSY)
1157c478bd9Sstevel@tonic-gate 				continue;
11656deab07SRod Evans 			for (block2 = block->next; block2 &&
11756deab07SRod Evans 			    block2->status == FREE; block2 = block2->next) {
1187c478bd9Sstevel@tonic-gate 				block->next = block2->next;
1197c478bd9Sstevel@tonic-gate 				block->size += block2->size + HDR_BLOCK;
1207c478bd9Sstevel@tonic-gate 			}
1217c478bd9Sstevel@tonic-gate 		}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 		/*
12456deab07SRod Evans 		 * If a page becomes free, leave it, and save the unmapping
12556deab07SRod Evans 		 * expense, as we'll probably come back and reclaim the page
12656deab07SRod Evans 		 * for later malloc activity.
12756deab07SRod Evans 		 *
12856deab07SRod Evans 		 * Free the defrag index.
1297c478bd9Sstevel@tonic-gate 		 */
13056deab07SRod Evans 		aplist_delete(free_alp, &idx);
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate static void
1357c478bd9Sstevel@tonic-gate split(struct block *block, size_t size)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	if (block->size > size + sizeof (struct block)) {
1387c478bd9Sstevel@tonic-gate 		struct block	*newblock;
1397c478bd9Sstevel@tonic-gate 		/* LINTED */
1407c478bd9Sstevel@tonic-gate 		newblock = (struct block *)
1417c478bd9Sstevel@tonic-gate 		    ((char *)block + HDR_BLOCK + size);
1427c478bd9Sstevel@tonic-gate 		newblock->next = block->next;
1437c478bd9Sstevel@tonic-gate 		block->next = newblock;
1447c478bd9Sstevel@tonic-gate 		newblock->status = FREE;
1457c478bd9Sstevel@tonic-gate 		newblock->page = block->page;
1467c478bd9Sstevel@tonic-gate 		newblock->size = block->size - size - HDR_BLOCK;
1477c478bd9Sstevel@tonic-gate 		block->size = size;
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
15156deab07SRod Evans #include <stdio.h>
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * Replace both malloc() and lmalloc() (libc's private memory allocator).
1557c478bd9Sstevel@tonic-gate  * They are both private here.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate #pragma weak lmalloc = malloc
1587c478bd9Sstevel@tonic-gate void *
1597c478bd9Sstevel@tonic-gate malloc(size_t size)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	struct block	*block;
1627c478bd9Sstevel@tonic-gate 	struct page	*page;
1637c478bd9Sstevel@tonic-gate 
16456deab07SRod Evans 	size = S_DROUND(size);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/*
1677c478bd9Sstevel@tonic-gate 	 * Try to locate necessary space
1687c478bd9Sstevel@tonic-gate 	 */
1697c478bd9Sstevel@tonic-gate 	for (page = memstart; page; page = page->next) {
1707c478bd9Sstevel@tonic-gate 		for (block = page->block; block; block = block->next) {
1717c478bd9Sstevel@tonic-gate 			if ((block->status == FREE) && (block->size >= size))
1727c478bd9Sstevel@tonic-gate 				goto found;
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate found:
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * Need to allocate a new page
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	if (!page) {
1807c478bd9Sstevel@tonic-gate 		size_t	totsize = size + HDR_PAGE;
18156deab07SRod Evans 		size_t	totpage = S_ROUND(totsize, syspagsz);
1827c478bd9Sstevel@tonic-gate 
18356deab07SRod Evans 		if ((page = dz_map(0, 0, totpage,
1847c478bd9Sstevel@tonic-gate 		    PROT_READ | PROT_WRITE | PROT_EXEC,
18556deab07SRod Evans 		    MAP_PRIVATE)) == MAP_FAILED)
1867c478bd9Sstevel@tonic-gate 			return (0);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		page->next = memstart;
1897c478bd9Sstevel@tonic-gate 		memstart = page;
1907c478bd9Sstevel@tonic-gate 		page->size = totpage;
1917c478bd9Sstevel@tonic-gate 		block = page->block;
1927c478bd9Sstevel@tonic-gate 		block->next = 0;
1937c478bd9Sstevel@tonic-gate 		block->status = FREE;
1947c478bd9Sstevel@tonic-gate 		block->size = totpage - HDR_PAGE;
1957c478bd9Sstevel@tonic-gate 		block->page = page;
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	split(block, size);
1997c478bd9Sstevel@tonic-gate #if	DEBUG
2007c478bd9Sstevel@tonic-gate 	scribble((ulong_t *)&block->memstart, NEWMEM, block->size);
2017c478bd9Sstevel@tonic-gate #endif
2027c478bd9Sstevel@tonic-gate 	block->status = BUSY;
2037c478bd9Sstevel@tonic-gate 	return (&block->memstart);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate void *
2077c478bd9Sstevel@tonic-gate calloc(size_t num, size_t size)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	void *	mp;
210*31c6d826SRichard Lowe 	size_t	total;
2117c478bd9Sstevel@tonic-gate 
212*31c6d826SRichard Lowe 	if (num == 0 || size == 0) {
213*31c6d826SRichard Lowe 		total = 0;
214*31c6d826SRichard Lowe 	} else {
215*31c6d826SRichard Lowe 		total = num * size;
216*31c6d826SRichard Lowe 
217*31c6d826SRichard Lowe 		/* check for overflow */
218*31c6d826SRichard Lowe 		if ((total / num) != size) {
219*31c6d826SRichard Lowe 			errno = ENOMEM;
2207c478bd9Sstevel@tonic-gate 			return (NULL);
221*31c6d826SRichard Lowe 		}
222*31c6d826SRichard Lowe 	}
223*31c6d826SRichard Lowe 
224*31c6d826SRichard Lowe 	if ((mp = malloc(total)) == NULL)
225*31c6d826SRichard Lowe 		return (NULL);
226*31c6d826SRichard Lowe 	(void) memset(mp, 0, total);
2277c478bd9Sstevel@tonic-gate 	return (mp);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate void *
2317c478bd9Sstevel@tonic-gate realloc(void *ptr, size_t size)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	struct block	*block;
2347c478bd9Sstevel@tonic-gate 	size_t		osize;
2357c478bd9Sstevel@tonic-gate 	void *		newptr;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
2387c478bd9Sstevel@tonic-gate 		return (malloc(size));
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	/* LINTED */
2417c478bd9Sstevel@tonic-gate 	block = (struct block *)((char *)ptr - HDR_BLOCK);
24256deab07SRod Evans 	size = S_DROUND(size);
2437c478bd9Sstevel@tonic-gate 	osize = block->size;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/*
2467c478bd9Sstevel@tonic-gate 	 * Join block with next one if it is free
2477c478bd9Sstevel@tonic-gate 	 */
2487c478bd9Sstevel@tonic-gate 	if (block->next && block->next->status == FREE) {
2497c478bd9Sstevel@tonic-gate 		block->size += block->next->size + HDR_BLOCK;
2507c478bd9Sstevel@tonic-gate 		block->next = block->next->next;
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	if (size <= block->size) {
2547c478bd9Sstevel@tonic-gate 		split(block, size);
2557c478bd9Sstevel@tonic-gate #if	DEBUG
2567c478bd9Sstevel@tonic-gate 		if (block->size > osize)
2577c478bd9Sstevel@tonic-gate 			scribble((ulong_t *)((char *)ptr + osize), NEWMEM,
2587c478bd9Sstevel@tonic-gate 			    (block->size - osize));
2597c478bd9Sstevel@tonic-gate #endif
2607c478bd9Sstevel@tonic-gate 		return (ptr);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if ((newptr = malloc(size)) == NULL)
2647c478bd9Sstevel@tonic-gate 		return (NULL);
2657c478bd9Sstevel@tonic-gate 	(void) memcpy(newptr, ptr, osize);
2667c478bd9Sstevel@tonic-gate 	block->status = FREE;
267a4bc8592SRod Evans 
268a4bc8592SRod Evans 	/*
269a4bc8592SRod Evans 	 * Add the free block to the free APlist for later defragmentation.
270a4bc8592SRod Evans 	 * However, this addition can only be achieved if there is room on the
271a4bc8592SRod Evans 	 * free APlist.  The APlist can't be allowed to grow, as the growth
272a4bc8592SRod Evans 	 * requires a realloc(), which would recurse back here, resulting in an
273a4bc8592SRod Evans 	 * infinite loop.  If the free APlist is full, defrag() now.  This
274a4bc8592SRod Evans 	 * defragmentation might not be able to collapse any free space, but
275a4bc8592SRod Evans 	 * the free APlist will be cleared as part of the processing, ensuring
276a4bc8592SRod Evans 	 * room for the addition.
277a4bc8592SRod Evans 	 */
278a4bc8592SRod Evans 	if (free_alp && (aplist_nitems(free_alp) >= aplist_arritems(free_alp)))
279a4bc8592SRod Evans 		defrag();
28056deab07SRod Evans 	(void) aplist_test(&free_alp, block->page, AL_CNT_FREELIST);
2817c478bd9Sstevel@tonic-gate 	return (newptr);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /*
2857c478bd9Sstevel@tonic-gate  * Replace both free() and lfree() (libc's private memory allocator).
2867c478bd9Sstevel@tonic-gate  * They are both private here.
2877c478bd9Sstevel@tonic-gate  */
2887c478bd9Sstevel@tonic-gate void
2897c478bd9Sstevel@tonic-gate free(void *ptr)
2907c478bd9Sstevel@tonic-gate {
2917c478bd9Sstevel@tonic-gate 	struct block	*block;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
2947c478bd9Sstevel@tonic-gate 		return;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/* LINTED */
2977c478bd9Sstevel@tonic-gate 	block = (struct block *)((char *)ptr - HDR_BLOCK);
2987c478bd9Sstevel@tonic-gate 	block->status = FREE;
2997c478bd9Sstevel@tonic-gate #if	DEBUG
3007c478bd9Sstevel@tonic-gate 	scribble((ulong_t *)&block->memstart, FREMEM, block->size);
3017c478bd9Sstevel@tonic-gate #endif
30256deab07SRod Evans 	(void) aplist_test(&free_alp, block->page, AL_CNT_FREELIST);
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
3067c478bd9Sstevel@tonic-gate void
3077c478bd9Sstevel@tonic-gate lfree(void *ptr, size_t size)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	free(ptr);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate  * We can use any memory after ld.so.1's .bss up until the next page boundary
3147c478bd9Sstevel@tonic-gate  * as allocatable memory.
3157c478bd9Sstevel@tonic-gate  */
3167c478bd9Sstevel@tonic-gate void
3177c478bd9Sstevel@tonic-gate addfree(void *ptr, size_t bytes)
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	struct block	*block;
3207c478bd9Sstevel@tonic-gate 	struct page	*page;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	if (bytes <= sizeof (struct page))
3237c478bd9Sstevel@tonic-gate 		return;
3247c478bd9Sstevel@tonic-gate 	page = ptr;
3257c478bd9Sstevel@tonic-gate 	page->next = memstart;
3267c478bd9Sstevel@tonic-gate 	memstart = page;
3277c478bd9Sstevel@tonic-gate 	page->size = bytes;
3287c478bd9Sstevel@tonic-gate 	block = page->block;
3297c478bd9Sstevel@tonic-gate 	block->next = 0;
3307c478bd9Sstevel@tonic-gate 	block->status = FREE;
3317c478bd9Sstevel@tonic-gate 	block->size = bytes - HDR_PAGE;
3327c478bd9Sstevel@tonic-gate 	block->page = page;
3337c478bd9Sstevel@tonic-gate }
334