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 (the "License").
2N/A * You may not use this file except in compliance 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/*
2N/A * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A
2N/A#include <unistd.h>
2N/A#include <errno.h>
2N/A#include <sys/mman.h>
2N/A#include <sys/sysmacros.h>
2N/A#include "vmem_base.h"
2N/A
2N/A#define ALLOC_PROT PROT_READ | PROT_WRITE | PROT_EXEC
2N/A#define FREE_PROT PROT_NONE
2N/A
2N/A#define ALLOC_FLAGS MAP_PRIVATE | MAP_ANON
2N/A#define FREE_FLAGS MAP_PRIVATE | MAP_ANON | MAP_NORESERVE
2N/A
2N/A#define CHUNKSIZE (64*1024) /* 64 kilobytes */
2N/A
2N/Astatic vmem_t *mmap_heap;
2N/A
2N/Astatic void *
2N/Avmem_mmap_alloc(vmem_t *src, size_t size, int vmflags)
2N/A{
2N/A void *ret;
2N/A int old_errno = errno;
2N/A
2N/A ret = vmem_alloc(src, size, vmflags | vmem_extra_flags);
2N/A if (ret != NULL &&
2N/A mmap(ret, size, ALLOC_PROT, ALLOC_FLAGS | MAP_FIXED, -1, 0) ==
2N/A MAP_FAILED) {
2N/A vmem_free(src, ret, size);
2N/A vmem_reap();
2N/A
2N/A ASSERT((vmflags & VM_NOSLEEP) == VM_NOSLEEP);
2N/A errno = old_errno;
2N/A return (NULL);
2N/A }
2N/A
2N/A errno = old_errno;
2N/A return (ret);
2N/A}
2N/A
2N/Astatic void
2N/Avmem_mmap_free(vmem_t *src, void *addr, size_t size)
2N/A{
2N/A int old_errno = errno;
2N/A (void) mmap(addr, size, FREE_PROT, FREE_FLAGS | MAP_FIXED, -1, 0);
2N/A vmem_free(src, addr, size);
2N/A errno = old_errno;
2N/A}
2N/A
2N/Astatic void *
2N/Avmem_mmap_top_alloc(vmem_t *src, size_t size, int vmflags)
2N/A{
2N/A void *ret;
2N/A void *buf;
2N/A int old_errno = errno;
2N/A
2N/A ret = vmem_alloc(src, size, VM_NOSLEEP);
2N/A
2N/A if (ret) {
2N/A errno = old_errno;
2N/A return (ret);
2N/A }
2N/A /*
2N/A * Need to grow the heap
2N/A */
2N/A buf = mmap((void *)CHUNKSIZE, size, FREE_PROT, FREE_FLAGS | MAP_ALIGN,
2N/A -1, 0);
2N/A
2N/A if (buf != MAP_FAILED) {
2N/A ret = _vmem_extend_alloc(src, buf, size, size, vmflags);
2N/A if (ret != NULL)
2N/A return (ret);
2N/A else {
2N/A (void) munmap(buf, size);
2N/A errno = old_errno;
2N/A return (NULL);
2N/A }
2N/A } else {
2N/A /*
2N/A * Growing the heap failed. The allocation above will
2N/A * already have called umem_reap().
2N/A */
2N/A ASSERT((vmflags & VM_NOSLEEP) == VM_NOSLEEP);
2N/A
2N/A errno = old_errno;
2N/A return (NULL);
2N/A }
2N/A}
2N/A
2N/Avmem_t *
2N/Avmem_mmap_arena(vmem_alloc_t **a_out, vmem_free_t **f_out)
2N/A{
2N/A size_t pagesize = sysconf(_SC_PAGESIZE);
2N/A
2N/A if (mmap_heap == NULL) {
2N/A mmap_heap = vmem_init("mmap_top", CHUNKSIZE,
2N/A vmem_mmap_top_alloc, vmem_free,
2N/A "mmap_heap", NULL, 0, pagesize,
2N/A vmem_mmap_alloc, vmem_mmap_free);
2N/A }
2N/A
2N/A if (a_out != NULL)
2N/A *a_out = vmem_mmap_alloc;
2N/A if (f_out != NULL)
2N/A *f_out = vmem_mmap_free;
2N/A
2N/A return (mmap_heap);
2N/A}