4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 * Portions Copyright 2006 OmniTI, Inc.
30 /* #pragma ident "@(#)vmem_sbrk.c 1.4 05/06/08 SMI" */
33 * The structure of the sbrk backend:
38 * | (vmem_sbrk_alloc(), vmem_free())
43 * | | ... | (vmem_alloc(), vmem_free())
46 * The sbrk_top arena holds all controlled memory. vmem_sbrk_alloc() handles
47 * allocations from it, including growing the heap when we run low.
49 * Growing the heap is complicated by the fact that we have to extend the
50 * sbrk_top arena (using _vmem_extend_alloc()), and that can fail. Since
51 * other threads may be actively allocating, we can't return the memory.
53 * Instead, we put it on a doubly-linked list, sbrk_fails, which we search
54 * before calling sbrk().
58 /* #include "mtlib.h" */
61 #ifdef HAVE_SYS_SYSMACROS_H
62 #include <sys/sysmacros.h>
67 #include "vmem_base.h"
71 size_t vmem_sbrk_pagesize = 0; /* the preferred page size of the heap */
73 #define MIN_ALLOC (64*1024)
75 static size_t real_pagesize;
76 static vmem_t *sbrk_heap;
78 typedef struct sbrk_fail {
79 struct sbrk_fail *sf_next;
80 struct sbrk_fail *sf_prev;
81 void *sf_base; /* == the sbrk_fail's address */
82 size_t sf_size; /* the size of this buffer */
85 static sbrk_fail_t sbrk_fails = {
92 static mutex_t sbrk_faillock = DEFAULTMUTEX;
95 * _sbrk_grow_aligned() aligns the old break to a low_align boundry,
96 * adds min_size, aligns to a high_align boundry, and calls _brk_unlocked()
97 * to set the new break. The low_aligned-aligned value is returned, and
98 * the actual space allocated is returned through actual_size.
100 * Unlike sbrk(2), _sbrk_grow_aligned takes an unsigned size, and does
101 * not allow shrinking the heap.
104 _sbrk_grow_aligned(size_t min_size, size_t low_align, size_t high_align,
114 #define BRKALIGN(x) (caddr_t)P2ROUNDUP((uintptr_t)(x), ALIGNSZ)
116 if ((low_align & (low_align - 1)) != 0 ||
117 (high_align & (high_align - 1)) != 0) {
121 low_align = MAX(low_align, ALIGNSZ);
122 high_align = MAX(high_align, ALIGNSZ);
124 old_brk = (uintptr_t)BRKALIGN(sbrk(0));
125 ret_brk = P2ROUNDUP(old_brk, low_align);
126 high_brk = ret_brk + min_size;
127 new_brk = P2ROUNDUP(high_brk, high_align);
132 if (ret_brk < old_brk || high_brk < ret_brk || new_brk < high_brk) {
137 brk_result = brk((void *)new_brk);
142 if (actual_size != NULL)
143 *actual_size = (new_brk - ret_brk);
144 return ((void *)ret_brk);
148 * Try to extend src with [pos, pos + size).
150 * If it fails, add the block to the sbrk_fails list.
153 vmem_sbrk_extend_alloc(vmem_t *src, void *pos, size_t size, size_t alloc,
156 sbrk_fail_t *fnext, *fprev, *fp;
159 ret = _vmem_extend_alloc(src, pos, size, alloc, vmflags);
163 fp = (sbrk_fail_t *)pos;
165 ASSERT(sizeof (sbrk_fail_t) <= size);
170 (void) mutex_lock(&sbrk_faillock);
171 fp->sf_next = fnext = &sbrk_fails;
172 fp->sf_prev = fprev = sbrk_fails.sf_prev;
175 (void) mutex_unlock(&sbrk_faillock);
181 * Try to add at least size bytes to src, using the sbrk_fails list
184 vmem_sbrk_tryfail(vmem_t *src, size_t size, int vmflags)
188 (void) mutex_lock(&sbrk_faillock);
189 for (fp = sbrk_fails.sf_next; fp != &sbrk_fails; fp = fp->sf_next) {
190 if (fp->sf_size >= size) {
191 fp->sf_next->sf_prev = fp->sf_prev;
192 fp->sf_prev->sf_next = fp->sf_next;
193 fp->sf_next = fp->sf_prev = NULL;
197 (void) mutex_unlock(&sbrk_faillock);
199 if (fp != &sbrk_fails) {
200 ASSERT(fp->sf_base == (void *)fp);
201 return (vmem_sbrk_extend_alloc(src, fp, fp->sf_size, size,
205 * nothing of the right size on the freelist
211 vmem_sbrk_alloc(vmem_t *src, size_t size, int vmflags)
213 extern void *_sbrk_grow_aligned(size_t min_size, size_t low_align,
214 size_t high_align, size_t *actual_size);
220 int old_errno = errno;
222 ret = vmem_alloc(src, size, VM_NOSLEEP);
229 * The allocation failed. We need to grow the heap.
231 * First, try to use any buffers which failed earlier.
233 if (sbrk_fails.sf_next != &sbrk_fails &&
234 (ret = vmem_sbrk_tryfail(src, size, vmflags)) != NULL)
237 buf_size = MAX(size, MIN_ALLOC);
240 * buf_size gets overwritten with the actual allocated size
242 buf = _sbrk_grow_aligned(buf_size, real_pagesize, vmem_sbrk_pagesize,
245 if (buf != MAP_FAILED) {
246 ret = vmem_sbrk_extend_alloc(src, buf, buf_size, size, vmflags);
254 * Growing the heap failed. The vmem_alloc() above called umem_reap().
256 ASSERT((vmflags & VM_NOSLEEP) == VM_NOSLEEP);
266 vmem_sbrk_lockup(void)
268 (void) mutex_lock(&sbrk_faillock);
272 vmem_sbrk_release(void)
274 (void) mutex_unlock(&sbrk_faillock);
278 vmem_sbrk_arena(vmem_alloc_t **a_out, vmem_free_t **f_out)
280 if (sbrk_heap == NULL) {
283 real_pagesize = sysconf(_SC_PAGESIZE);
285 heap_size = vmem_sbrk_pagesize;
289 } else if (heap_size != 0 && !ISP2(heap_size)) {
291 log_message("ignoring bad pagesize: 0x%p\n", heap_size);
293 if (heap_size <= real_pagesize) {
294 heap_size = real_pagesize;
296 #ifdef MHA_MAPSIZE_BSSBRK
297 struct memcntl_mha mha;
298 mha.mha_cmd = MHA_MAPSIZE_BSSBRK;
300 mha.mha_pagesize = heap_size;
302 if (memcntl(NULL, 0, MC_HAT_ADVISE, (char *)&mha, 0, 0)
304 log_message("unable to set MAPSIZE_BSSBRK to "
305 "0x%p\n", heap_size);
306 heap_size = real_pagesize;
309 heap_size = real_pagesize;
312 vmem_sbrk_pagesize = heap_size;
314 sbrk_heap = vmem_init("sbrk_top", real_pagesize,
315 vmem_sbrk_alloc, vmem_free,
316 "sbrk_heap", NULL, 0, real_pagesize,
317 vmem_alloc, vmem_free);