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 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Standalone-specific vmem routines
32 * The standalone allocator operates on a pre-existing blob of memory, the
33 * location and dimensions of which are set using vmem_stand_setsize(). We
34 * then hand out CHUNKSIZE-sized pieces of this blob, until we run out.
37 #define DEF_CHUNKSIZE (64 * 1024) /* 64K */
39 #define DEF_NREGIONS 2
43 #include <sys/sysmacros.h>
48 #include "vmem_base.h"
51 static vmem_t *stand_heap;
53 static size_t stand_chunksize;
55 typedef struct stand_region {
61 static stand_region_t stand_regions[DEF_NREGIONS];
62 static int stand_nregions;
64 extern void membar_producer(void);
69 stand_chunksize = MAX(DEF_CHUNKSIZE, pagesize);
75 vmem_stand_add(caddr_t base, size_t len)
77 stand_region_t *sr = &stand_regions[stand_nregions];
79 ASSERT(pagesize != 0);
81 if (stand_nregions == DEF_NREGIONS) {
83 return (-1); /* we don't have room -- throw it back */
87 * We guarantee that only one call to `vmem_stand_add' will be
88 * active at a time, but we can't ensure that the allocator won't be
89 * in use while this function is being called. As such, we have to
90 * ensure that sr is populated and visible to other processors before
91 * allowing the allocator to access the new region.
94 sr->sr_curtop = (caddr_t)P2ROUNDUP((ulong_t)base, stand_chunksize);
95 sr->sr_left = P2ALIGN(len - (size_t)(sr->sr_curtop - sr->sr_base),
105 stand_parent_alloc(vmem_t *src, size_t size, int vmflags)
107 int old_errno = errno;
113 if ((ret = vmem_alloc(src, size, VM_NOSLEEP)) != NULL) {
118 /* We need to allocate another chunk */
119 chksize = roundup(size, stand_chunksize);
121 for (sr = stand_regions, i = 0; i < stand_nregions; i++, sr++) {
122 if (sr->sr_left >= chksize)
126 if (i == stand_nregions) {
128 * We don't have enough in any of our regions to satisfy the
135 if ((ret = _vmem_extend_alloc(src, sr->sr_curtop, chksize, size,
141 bzero(sr->sr_curtop, chksize);
143 sr->sr_curtop += chksize;
144 sr->sr_left -= chksize;
150 vmem_stand_arena(vmem_alloc_t **a_out, vmem_free_t **f_out)
152 ASSERT(stand_nregions == 1);
154 stand_heap = vmem_init("stand_parent", stand_chunksize,
155 stand_parent_alloc, vmem_free,
156 "stand_heap", NULL, 0, pagesize, vmem_alloc, vmem_free);