1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* 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"
21
#include "apr_errno.h"
22
#include "apr_arch_file_io.h"
23
#include "apr_portable.h"
24
#include "apr_strings.h"
28
static apr_status_t mmap_cleanup(void *themmap)
30
apr_mmap_t *mm = themmap;
31
apr_mmap_t *next = APR_RING_NEXT(mm,link);
34
/* we no longer refer to the mmaped region */
35
APR_RING_REMOVE(mm,link);
36
APR_RING_NEXT(mm,link) = NULL;
37
APR_RING_PREV(mm,link) = NULL;
40
/* more references exist, so we're done */
45
if (!UnmapViewOfFile(mm->mv))
47
apr_status_t rv = apr_get_os_error();
48
CloseHandle(mm->mhandle);
57
if (!CloseHandle(mm->mhandle))
59
apr_status_t rv = apr_get_os_error();
60
CloseHandle(mm->mhandle);
69
APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new, apr_file_t *file,
70
apr_off_t offset, apr_size_t size,
71
apr_int32_t flag, apr_pool_t *cont)
73
static DWORD memblock = 0;
82
if (flag & APR_MMAP_WRITE)
83
fmaccess |= PAGE_READWRITE;
84
else if (flag & APR_MMAP_READ)
85
fmaccess |= PAGE_READONLY;
87
if (flag & APR_MMAP_READ)
88
mvaccess |= FILE_MAP_READ;
89
if (flag & APR_MMAP_WRITE)
90
mvaccess |= FILE_MAP_WRITE;
92
if (!file || !file->filehand || file->filehand == INVALID_HANDLE_VALUE
100
memblock = si.dwAllocationGranularity;
103
*new = apr_pcalloc(cont, sizeof(apr_mmap_t));
104
(*new)->pstart = (offset / memblock) * memblock;
105
(*new)->poffset = offset - (*new)->pstart;
106
(*new)->psize = (apr_size_t)((*new)->poffset) + size;
107
/* The size of the CreateFileMapping object is the current size
108
* of the size of the mmap object (e.g. file size), not the size
109
* of the mapped region!
112
(*new)->mhandle = CreateFileMapping(file->filehand, NULL, fmaccess,
114
if (!(*new)->mhandle || (*new)->mhandle == INVALID_HANDLE_VALUE)
117
return apr_get_os_error();
120
offlo = (DWORD)(*new)->pstart;
121
offhi = (DWORD)((*new)->pstart >> 32);
122
(*new)->mv = MapViewOfFile((*new)->mhandle, mvaccess, offhi,
123
offlo, (*new)->psize);
126
apr_status_t rv = apr_get_os_error();
127
CloseHandle((*new)->mhandle);
132
(*new)->mm = (char*)((*new)->mv) + (*new)->poffset;
134
(*new)->cntxt = cont;
135
APR_RING_ELEM_INIT(*new, link);
137
/* register the cleanup... */
138
apr_pool_cleanup_register((*new)->cntxt, (void*)(*new), mmap_cleanup,
139
apr_pool_cleanup_null);
143
APR_DECLARE(apr_status_t) apr_mmap_dup(apr_mmap_t **new_mmap,
144
apr_mmap_t *old_mmap,
147
*new_mmap = (apr_mmap_t *)apr_pmemdup(p, old_mmap, sizeof(apr_mmap_t));
148
(*new_mmap)->cntxt = p;
150
APR_RING_INSERT_AFTER(old_mmap, *new_mmap, link);
152
apr_pool_cleanup_register(p, *new_mmap, mmap_cleanup,
153
apr_pool_cleanup_null);
157
APR_DECLARE(apr_status_t) apr_mmap_delete(apr_mmap_t *mm)
159
return apr_pool_cleanup_run(mm->cntxt, mm, mmap_cleanup);