1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "apr_private.h"
19 #include "apr_general.h"
20 #include "apr_strings.h"
22 #include "apr_errno.h"
23 #include "apr_arch_file_io.h"
24 #include "apr_portable.h"
26 /* System headers required for the mmap library */
28 #include <kernel/OS.h>
36 #ifdef HAVE_SYS_STAT_H
39 #ifdef HAVE_SYS_MMAN_H
43 #if APR_HAS_MMAP || defined(BEOS)
45 static apr_status_t mmap_cleanup(void *themmap)
47 apr_mmap_t *mm = themmap;
48 apr_mmap_t *next = APR_RING_NEXT(mm,link);
51 /* we no longer refer to the mmaped region */
52 APR_RING_REMOVE(mm,link);
53 APR_RING_NEXT(mm,link) = NULL;
54 APR_RING_PREV(mm,link) = NULL;
57 /* more references exist, so we're done */
62 rv = delete_area(mm->area);
64 rv = munmap(mm->mm, mm->size);
74 APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new,
75 apr_file_t *file, apr_off_t offset,
76 apr_size_t size, apr_int32_t flag,
84 apr_int32_t native_flags = 0;
87 #if APR_HAS_LARGE_FILES && defined(HAVE_MMAP64)
89 #elif APR_HAS_LARGE_FILES && SIZEOF_OFF_T == 4
90 /* LFS but no mmap64: check for overflow */
91 if ((apr_int64_t)offset + size > INT_MAX)
98 if (file == NULL || file->filedes == -1 || file->buffered)
100 (*new) = (apr_mmap_t *)apr_pcalloc(cont, sizeof(apr_mmap_t));
103 /* XXX: mmap shouldn't really change the seek offset */
104 apr_file_seek(file, APR_SET, &offset);
106 /* There seems to be some strange interactions that mean our area must
107 * be set as READ & WRITE or writev will fail! Go figure...
108 * So we ignore the value in flags and always ask for both READ and WRITE
110 pages = (size + B_PAGE_SIZE -1) / B_PAGE_SIZE;
111 aid = create_area("apr_mmap", &mm , B_ANY_ADDRESS, pages * B_PAGE_SIZE,
112 B_NO_LOCK, B_WRITE_AREA|B_READ_AREA);
114 if (aid < B_NO_ERROR) {
115 /* we failed to get an area we can use... */
120 if (aid >= B_NO_ERROR)
121 read(file->filedes, mm, size);
126 if (flag & APR_MMAP_WRITE) {
127 native_flags |= PROT_WRITE;
129 if (flag & APR_MMAP_READ) {
130 native_flags |= PROT_READ;
133 mm = mmap(NULL, size, native_flags, MAP_SHARED, file->filedes, offset);
135 if (mm == (void *)-1) {
136 /* we failed to get an mmap'd file... */
144 (*new)->cntxt = cont;
145 APR_RING_ELEM_INIT(*new, link);
147 /* register the cleanup... */
148 apr_pool_cleanup_register((*new)->cntxt, (void*)(*new), mmap_cleanup,
149 apr_pool_cleanup_null);
153 APR_DECLARE(apr_status_t) apr_mmap_dup(apr_mmap_t **new_mmap,
154 apr_mmap_t *old_mmap,
157 *new_mmap = (apr_mmap_t *)apr_pmemdup(p, old_mmap, sizeof(apr_mmap_t));
158 (*new_mmap)->cntxt = p;
160 APR_RING_INSERT_AFTER(old_mmap, *new_mmap, link);
162 apr_pool_cleanup_register(p, *new_mmap, mmap_cleanup,
163 apr_pool_cleanup_null);
167 APR_DECLARE(apr_status_t) apr_mmap_delete(apr_mmap_t *mm)
169 return apr_pool_cleanup_run(mm->cntxt, mm, mmap_cleanup);