2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * Standalone-specific vmem routines
2N/A *
2N/A * The standalone allocator operates on a pre-existing blob of memory, the
2N/A * location and dimensions of which are set using vmem_stand_setsize(). We
2N/A * then hand out CHUNKSIZE-sized pieces of this blob, until we run out.
2N/A */
2N/A
2N/A#define DEF_CHUNKSIZE (64 * 1024) /* 64K */
2N/A
2N/A#define DEF_NREGIONS 2
2N/A
2N/A#include <errno.h>
2N/A#include <limits.h>
2N/A#include <sys/sysmacros.h>
2N/A#include <sys/mman.h>
2N/A#include <unistd.h>
2N/A#include <strings.h>
2N/A
2N/A#include "vmem_base.h"
2N/A#include "misc.h"
2N/A
2N/Astatic vmem_t *stand_heap;
2N/A
2N/Astatic size_t stand_chunksize;
2N/A
2N/Atypedef struct stand_region {
2N/A caddr_t sr_base;
2N/A caddr_t sr_curtop;
2N/A size_t sr_left;
2N/A} stand_region_t;
2N/A
2N/Astatic stand_region_t stand_regions[DEF_NREGIONS];
2N/Astatic int stand_nregions;
2N/A
2N/Aextern void membar_producer(void);
2N/A
2N/Avoid
2N/Avmem_stand_init(void)
2N/A{
2N/A stand_chunksize = MAX(DEF_CHUNKSIZE, pagesize);
2N/A
2N/A stand_nregions = 0;
2N/A}
2N/A
2N/Aint
2N/Avmem_stand_add(caddr_t base, size_t len)
2N/A{
2N/A stand_region_t *sr = &stand_regions[stand_nregions];
2N/A
2N/A ASSERT(pagesize != 0);
2N/A
2N/A if (stand_nregions == DEF_NREGIONS) {
2N/A errno = ENOSPC;
2N/A return (-1); /* we don't have room -- throw it back */
2N/A }
2N/A
2N/A /*
2N/A * We guarantee that only one call to `vmem_stand_add' will be
2N/A * active at a time, but we can't ensure that the allocator won't be
2N/A * in use while this function is being called. As such, we have to
2N/A * ensure that sr is populated and visible to other processors before
2N/A * allowing the allocator to access the new region.
2N/A */
2N/A sr->sr_base = base;
2N/A sr->sr_curtop = (caddr_t)P2ROUNDUP((ulong_t)base, stand_chunksize);
2N/A sr->sr_left = P2ALIGN(len - (size_t)(sr->sr_curtop - sr->sr_base),
2N/A stand_chunksize);
2N/A membar_producer();
2N/A
2N/A stand_nregions++;
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic void *
2N/Astand_parent_alloc(vmem_t *src, size_t size, int vmflags)
2N/A{
2N/A int old_errno = errno;
2N/A stand_region_t *sr;
2N/A size_t chksize;
2N/A void *ret;
2N/A int i;
2N/A
2N/A if ((ret = vmem_alloc(src, size, VM_NOSLEEP)) != NULL) {
2N/A errno = old_errno;
2N/A return (ret);
2N/A }
2N/A
2N/A /* We need to allocate another chunk */
2N/A chksize = roundup(size, stand_chunksize);
2N/A
2N/A for (sr = stand_regions, i = 0; i < stand_nregions; i++, sr++) {
2N/A if (sr->sr_left >= chksize)
2N/A break;
2N/A }
2N/A
2N/A if (i == stand_nregions) {
2N/A /*
2N/A * We don't have enough in any of our regions to satisfy the
2N/A * request.
2N/A */
2N/A errno = old_errno;
2N/A return (NULL);
2N/A }
2N/A
2N/A if ((ret = _vmem_extend_alloc(src, sr->sr_curtop, chksize, size,
2N/A vmflags)) == NULL) {
2N/A errno = old_errno;
2N/A return (NULL);
2N/A }
2N/A
2N/A bzero(sr->sr_curtop, chksize);
2N/A
2N/A sr->sr_curtop += chksize;
2N/A sr->sr_left -= chksize;
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/Avmem_t *
2N/Avmem_stand_arena(vmem_alloc_t **a_out, vmem_free_t **f_out)
2N/A{
2N/A ASSERT(stand_nregions == 1);
2N/A
2N/A stand_heap = vmem_init("stand_parent", stand_chunksize,
2N/A stand_parent_alloc, vmem_free,
2N/A "stand_heap", NULL, 0, pagesize, vmem_alloc, vmem_free);
2N/A
2N/A if (a_out != NULL)
2N/A *a_out = vmem_alloc;
2N/A if (f_out != NULL)
2N/A *f_out = vmem_free;
2N/A
2N/A return (stand_heap);
2N/A}