xref: /titanic_44/usr/src/lib/libmapmalloc/common/textmem.c (revision 1d53067866b073ea6710000ba4dd448441361988)
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*1d530678Sraf  * Common Development and Distribution License (the "License").
6*1d530678Sraf  * 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  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
23*1d530678Sraf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*1d530678Sraf  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*1d530678Sraf /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*1d530678Sraf /*	  All Rights Reserved  	*/
29*1d530678Sraf 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVR4/MNLS 1.1.2.1 */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * Simplified version of malloc(), free() and realloc(), to be linked with
397c478bd9Sstevel@tonic-gate  * utilities that use [s]brk() and do not define their own version of the
407c478bd9Sstevel@tonic-gate  * routines.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * The algorithm used to get extra memory space by mmap'ing /dev/zero. This
437c478bd9Sstevel@tonic-gate  * breaks if the application closes the open descriptor, so now it uses
447c478bd9Sstevel@tonic-gate  * mmap's MAP_ANON feature.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * Each call to mmap() creates a page. The pages are linked in a list.
477c478bd9Sstevel@tonic-gate  * Each page is divided in blocks. There is at least one block in a page.
487c478bd9Sstevel@tonic-gate  * New memory chunks are allocated on a first-fit basis.
497c478bd9Sstevel@tonic-gate  * Freed blocks are joined in larger blocks. Free pages are unmapped.
507c478bd9Sstevel@tonic-gate  */
51*1d530678Sraf #include <c_synonyms.h>
527c478bd9Sstevel@tonic-gate #include <stdlib.h>
537c478bd9Sstevel@tonic-gate #include <sys/types.h>
547c478bd9Sstevel@tonic-gate #include <sys/mman.h>
557c478bd9Sstevel@tonic-gate #include <fcntl.h>
567c478bd9Sstevel@tonic-gate #include <errno.h>
577c478bd9Sstevel@tonic-gate #include <unistd.h>
587c478bd9Sstevel@tonic-gate #include <thread.h>
59*1d530678Sraf #include <pthread.h>
607c478bd9Sstevel@tonic-gate #include <synch.h>
617c478bd9Sstevel@tonic-gate #include <string.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static mutex_t lock = DEFAULTMUTEX;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate struct block {
667c478bd9Sstevel@tonic-gate 	size_t size;		/* Space available for user */
677c478bd9Sstevel@tonic-gate 	struct page *page;	/* Backwards reference to page */
687c478bd9Sstevel@tonic-gate 	int status;
697c478bd9Sstevel@tonic-gate 	struct block *next;
707c478bd9Sstevel@tonic-gate 	void *memstart[1];
717c478bd9Sstevel@tonic-gate };
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate struct page {
747c478bd9Sstevel@tonic-gate 	size_t size;		/* Total page size (incl. header) */
757c478bd9Sstevel@tonic-gate 	struct page *next;
767c478bd9Sstevel@tonic-gate 	struct block block[1];
777c478bd9Sstevel@tonic-gate };
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #define	FREE	0
807c478bd9Sstevel@tonic-gate #define	BUSY	1
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	HDR_BLOCK	(sizeof (struct block) - sizeof (void *))
837c478bd9Sstevel@tonic-gate #define	HDR_PAGE	(sizeof (struct page) - sizeof (void *))
847c478bd9Sstevel@tonic-gate #define	MINSZ		sizeof (double)
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /* for convenience */
877c478bd9Sstevel@tonic-gate #ifndef	NULL
887c478bd9Sstevel@tonic-gate #define	NULL		(0)
897c478bd9Sstevel@tonic-gate #endif
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate struct page *memstart;
927c478bd9Sstevel@tonic-gate static int pagesize;
937c478bd9Sstevel@tonic-gate static void defrag(struct page *);
947c478bd9Sstevel@tonic-gate static void split(struct block *,  size_t);
957c478bd9Sstevel@tonic-gate static void *malloc_unlocked(size_t);
967c478bd9Sstevel@tonic-gate static size_t align(size_t, int);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate void *
997c478bd9Sstevel@tonic-gate malloc(size_t size)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	void *retval;
1027c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&lock);
1037c478bd9Sstevel@tonic-gate 	retval = malloc_unlocked(size);
1047c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
1057c478bd9Sstevel@tonic-gate 	return (retval);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate static void *
1107c478bd9Sstevel@tonic-gate malloc_unlocked(size_t size)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	struct block *block;
1137c478bd9Sstevel@tonic-gate 	struct page *page;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	if (pagesize == 0)
1167c478bd9Sstevel@tonic-gate 		pagesize = (int)sysconf(_SC_PAGESIZE);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	size = align(size, MINSZ);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/*
1217c478bd9Sstevel@tonic-gate 	 * Try to locate necessary space
1227c478bd9Sstevel@tonic-gate 	 */
1237c478bd9Sstevel@tonic-gate 	for (page = memstart; page; page = page->next) {
1247c478bd9Sstevel@tonic-gate 		for (block = page->block; block; block = block->next) {
1257c478bd9Sstevel@tonic-gate 			if (block->status == FREE && block->size >= size)
1267c478bd9Sstevel@tonic-gate 				goto found;
1277c478bd9Sstevel@tonic-gate 		}
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate found:
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/*
1327c478bd9Sstevel@tonic-gate 	 * Need to allocate a new page
1337c478bd9Sstevel@tonic-gate 	 */
1347c478bd9Sstevel@tonic-gate 	if (!page) {
1357c478bd9Sstevel@tonic-gate 		size_t totsize = size + HDR_PAGE;
1367c478bd9Sstevel@tonic-gate 		size_t totpage = align(totsize, pagesize);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 		if ((page = (struct page *)mmap(0, totpage,
1397c478bd9Sstevel@tonic-gate 		    PROT_READ|PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0))
1407c478bd9Sstevel@tonic-gate 			== MAP_FAILED)
1417c478bd9Sstevel@tonic-gate 			return (0);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		page->next = memstart;
1447c478bd9Sstevel@tonic-gate 		memstart = page;
1457c478bd9Sstevel@tonic-gate 		page->size = totpage;
1467c478bd9Sstevel@tonic-gate 		block = page->block;
1477c478bd9Sstevel@tonic-gate 		block->next = 0;
1487c478bd9Sstevel@tonic-gate 		block->status = FREE;
1497c478bd9Sstevel@tonic-gate 		block->size = totpage - HDR_PAGE;
1507c478bd9Sstevel@tonic-gate 		block->page = page;
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	split(block, size);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	block->status = BUSY;
1567c478bd9Sstevel@tonic-gate 	return (&block->memstart);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate void *
1607c478bd9Sstevel@tonic-gate realloc(void *ptr, size_t size)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	struct block *block;
1637c478bd9Sstevel@tonic-gate 	size_t osize;
1647c478bd9Sstevel@tonic-gate 	void *newptr;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&lock);
1677c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
1687c478bd9Sstevel@tonic-gate 		newptr = malloc_unlocked(size);
1697c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
1707c478bd9Sstevel@tonic-gate 		return (newptr);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 	block = (struct block *)((char *)ptr - HDR_BLOCK);
1737c478bd9Sstevel@tonic-gate 	size = align(size, MINSZ);
1747c478bd9Sstevel@tonic-gate 	osize = block->size;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * Join block with next one if it is free
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	if (block->next && block->next->status == FREE) {
1807c478bd9Sstevel@tonic-gate 		block->size += block->next->size + HDR_BLOCK;
1817c478bd9Sstevel@tonic-gate 		block->next = block->next->next;
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (size <= block->size) {
1857c478bd9Sstevel@tonic-gate 		split(block, size);
1867c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
1877c478bd9Sstevel@tonic-gate 		return (ptr);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	newptr = malloc_unlocked(size);
1917c478bd9Sstevel@tonic-gate 	(void) memcpy(newptr, ptr, osize);
1927c478bd9Sstevel@tonic-gate 	block->status = FREE;
1937c478bd9Sstevel@tonic-gate 	defrag(block->page);
1947c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
1957c478bd9Sstevel@tonic-gate 	return (newptr);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate void
1997c478bd9Sstevel@tonic-gate free(void *ptr)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	struct block *block;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&lock);
2047c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
2057c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
2067c478bd9Sstevel@tonic-gate 		return;
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	block = (struct block *)((char *)ptr - HDR_BLOCK);
2097c478bd9Sstevel@tonic-gate 	block->status = FREE;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	defrag(block->page);
2127c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * Align size on an appropriate boundary
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate static size_t
2197c478bd9Sstevel@tonic-gate align(size_t size, int bound)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	if (size < bound)
2227c478bd9Sstevel@tonic-gate 		return ((size_t)bound);
2237c478bd9Sstevel@tonic-gate 	else
2247c478bd9Sstevel@tonic-gate 		return (size + bound - 1 - (size + bound - 1) % bound);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate static void
2287c478bd9Sstevel@tonic-gate split(struct block *block, size_t size)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	if (block->size > size + sizeof (struct block)) {
2317c478bd9Sstevel@tonic-gate 		struct block *newblock;
2327c478bd9Sstevel@tonic-gate 		newblock = (struct block *)((char *)block + HDR_BLOCK + size);
2337c478bd9Sstevel@tonic-gate 		newblock->next = block->next;
2347c478bd9Sstevel@tonic-gate 		block->next = newblock;
2357c478bd9Sstevel@tonic-gate 		newblock->status = FREE;
2367c478bd9Sstevel@tonic-gate 		newblock->page = block->page;
2377c478bd9Sstevel@tonic-gate 		newblock->size = block->size - size - HDR_BLOCK;
2387c478bd9Sstevel@tonic-gate 		block->size = size;
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate  * Defragmentation
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate static void
2467c478bd9Sstevel@tonic-gate defrag(struct page *page)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate 	struct block *block;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	for (block = page->block; block; block = block->next) {
2517c478bd9Sstevel@tonic-gate 		struct block *block2;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 		if (block->status == BUSY)
2547c478bd9Sstevel@tonic-gate 			continue;
2557c478bd9Sstevel@tonic-gate 		for (block2 = block->next; block2 && block2->status == FREE;
2567c478bd9Sstevel@tonic-gate 			block2 = block2->next) {
2577c478bd9Sstevel@tonic-gate 			block->next = block2->next;
2587c478bd9Sstevel@tonic-gate 			block->size += block2->size + HDR_BLOCK;
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	/*
2637c478bd9Sstevel@tonic-gate 	 * Free page
2647c478bd9Sstevel@tonic-gate 	 */
2657c478bd9Sstevel@tonic-gate 	if (page->block->size == page->size - HDR_PAGE) {
2667c478bd9Sstevel@tonic-gate 		if (page == memstart)
2677c478bd9Sstevel@tonic-gate 			memstart = page->next;
2687c478bd9Sstevel@tonic-gate 		else {
2697c478bd9Sstevel@tonic-gate 			struct page *page2;
2707c478bd9Sstevel@tonic-gate 			for (page2 = memstart; page2->next;
2717c478bd9Sstevel@tonic-gate 				page2 = page2->next) {
2727c478bd9Sstevel@tonic-gate 				if (page2->next == page) {
2737c478bd9Sstevel@tonic-gate 					page2->next = page->next;
2747c478bd9Sstevel@tonic-gate 					break;
2757c478bd9Sstevel@tonic-gate 				}
2767c478bd9Sstevel@tonic-gate 			}
2777c478bd9Sstevel@tonic-gate 		}
2787c478bd9Sstevel@tonic-gate 		(void) munmap((caddr_t)page, page->size);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate }
281*1d530678Sraf 
282*1d530678Sraf static void
283*1d530678Sraf malloc_prepare()
284*1d530678Sraf {
285*1d530678Sraf 	(void) mutex_lock(&lock);
286*1d530678Sraf }
287*1d530678Sraf 
288*1d530678Sraf static void
289*1d530678Sraf malloc_release()
290*1d530678Sraf {
291*1d530678Sraf 	(void) mutex_unlock(&lock);
292*1d530678Sraf }
293*1d530678Sraf 
294*1d530678Sraf #pragma init(malloc_init)
295*1d530678Sraf static void
296*1d530678Sraf malloc_init(void)
297*1d530678Sraf {
298*1d530678Sraf 	(void) pthread_atfork(malloc_prepare, malloc_release, malloc_release);
299*1d530678Sraf }
300