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 2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 * Portions Copyright 2006 OmniTI, Inc.
30 /* #pragma ident "@(#)vmem_mmap.c 1.2 05/06/08 SMI" */
39 #ifdef HAVE_SYS_SYSMACROS_H
40 #include <sys/sysmacros.h>
45 #include "vmem_base.h"
47 #define ALLOC_PROT PROT_READ | PROT_WRITE | PROT_EXEC
48 #define FREE_PROT PROT_NONE
50 #define ALLOC_FLAGS MAP_PRIVATE | MAP_ANON
51 #define FREE_FLAGS MAP_PRIVATE | MAP_ANON | MAP_NORESERVE
54 #define CHUNKSIZE (64*1024) /* 64 kilobytes */
56 static size_t CHUNKSIZE;
59 static vmem_t *mmap_heap;
62 vmem_mmap_alloc(vmem_t *src, size_t size, int vmflags)
65 int old_errno = errno;
67 ret = vmem_alloc(src, size, vmflags);
71 mmap(ret, size, ALLOC_PROT, ALLOC_FLAGS | MAP_FIXED, -1, 0) ==
74 vmem_free(src, ret, size);
77 ASSERT((vmflags & VM_NOSLEEP) == VM_NOSLEEP);
88 vmem_mmap_free(vmem_t *src, void *addr, size_t size)
90 int old_errno = errno;
92 VirtualFree(addr, size, MEM_RELEASE);
94 (void) mmap(addr, size, FREE_PROT, FREE_FLAGS | MAP_FIXED, -1, 0);
96 vmem_free(src, addr, size);
101 vmem_mmap_top_alloc(vmem_t *src, size_t size, int vmflags)
105 int old_errno = errno;
107 ret = vmem_alloc(src, size, VM_NOSLEEP);
114 * Need to grow the heap
117 buf = VirtualAlloc(NULL, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
118 if (buf == NULL) buf = MAP_FAILED;
126 size, FREE_PROT, FREE_FLAGS
133 if (buf != MAP_FAILED) {
134 ret = _vmem_extend_alloc(src, buf, size, size, vmflags);
138 (void) munmap(buf, size);
144 * Growing the heap failed. The allocation above will
145 * already have called umem_reap().
147 ASSERT((vmflags & VM_NOSLEEP) == VM_NOSLEEP);
155 vmem_mmap_arena(vmem_alloc_t **a_out, vmem_free_t **f_out)
161 size_t pagesize = _sysconf(_SC_PAGESIZE);
165 GetSystemInfo(&info);
166 pagesize = info.dwPageSize;
167 CHUNKSIZE = info.dwAllocationGranularity;
168 #elif !defined(MAP_ALIGN)
169 CHUNKSIZE = pagesize;
172 if (mmap_heap == NULL) {
173 mmap_heap = vmem_init("mmap_top",
175 vmem_mmap_top_alloc, vmem_free,
176 "mmap_heap", NULL, 0, pagesize,
177 vmem_mmap_alloc, vmem_mmap_free);
181 *a_out = vmem_mmap_alloc;
183 *f_out = vmem_mmap_free;