Apache HTTPD
mmap.c
Go to the documentation of this file.
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include "apr.h"
18#include "apr_private.h"
19#include "apr_general.h"
20#include "apr_mmap.h"
21#include "apr_errno.h"
22#include "apr_arch_file_io.h"
23#include "apr_portable.h"
24#include "apr_strings.h"
25
26#if APR_HAS_MMAP
27
29{
30 apr_mmap_t *mm = themmap;
31 apr_mmap_t *next = APR_RING_NEXT(mm,link);
32
33 /* we no longer refer to the mmaped region */
34 APR_RING_REMOVE(mm,link);
35 APR_RING_NEXT(mm,link) = NULL;
36 APR_RING_PREV(mm,link) = NULL;
37
38 if (next != mm) {
39 /* more references exist, so we're done */
40 return APR_SUCCESS;
41 }
42
43 if (mm->mv) {
44 if (!UnmapViewOfFile(mm->mv))
45 {
47 CloseHandle(mm->mhandle);
48 mm->mv = NULL;
49 mm->mhandle = NULL;
50 return rv;
51 }
52 mm->mv = NULL;
53 }
54 if (mm->mhandle)
55 {
56 if (!CloseHandle(mm->mhandle))
57 {
59 CloseHandle(mm->mhandle);
60 mm->mhandle = NULL;
61 return rv;
62 }
63 mm->mhandle = NULL;
64 }
65 return APR_SUCCESS;
66}
67
71{
72 static DWORD memblock = 0;
73 DWORD fmaccess = 0;
74 DWORD mvaccess = 0;
77
78 if (size == 0)
79 return APR_EINVAL;
80
81 if (flag & APR_MMAP_WRITE)
83 else if (flag & APR_MMAP_READ)
85
86 if (flag & APR_MMAP_READ)
88 if (flag & APR_MMAP_WRITE)
90
92 || file->buffered)
93 return APR_EBADF;
94
95 if (!memblock)
96 {
99 memblock = si.dwAllocationGranularity;
100 }
101
102 *new = apr_pcalloc(cont, sizeof(apr_mmap_t));
103 (*new)->pstart = (offset / memblock) * memblock;
104 (*new)->poffset = offset - (*new)->pstart;
105 (*new)->psize = (apr_size_t)((*new)->poffset) + size;
106 /* The size of the CreateFileMapping object is the current size
107 * of the size of the mmap object (e.g. file size), not the size
108 * of the mapped region!
109 */
110
111 (*new)->mhandle = CreateFileMapping(file->filehand, NULL, fmaccess,
112 0, 0, NULL);
113 if (!(*new)->mhandle || (*new)->mhandle == INVALID_HANDLE_VALUE)
114 {
115 *new = NULL;
116 return apr_get_os_error();
117 }
118
119 offlo = (DWORD)(*new)->pstart;
120 offhi = (DWORD)((*new)->pstart >> 32);
121 (*new)->mv = MapViewOfFile((*new)->mhandle, mvaccess, offhi,
122 offlo, (*new)->psize);
123 if (!(*new)->mv)
124 {
126 CloseHandle((*new)->mhandle);
127 *new = NULL;
128 return rv;
129 }
130
131 (*new)->mm = (char*)((*new)->mv) + (*new)->poffset;
132 (*new)->size = size;
133 (*new)->cntxt = cont;
134 APR_RING_ELEM_INIT(*new, link);
135
136 /* register the cleanup... */
137 apr_pool_cleanup_register((*new)->cntxt, (void*)(*new), mmap_cleanup,
139 return APR_SUCCESS;
140}
141
144 apr_pool_t *p)
145{
147 (*new_mmap)->cntxt = p;
148
150
153 return APR_SUCCESS;
154}
155
157{
158 return apr_pool_cleanup_run(mm->cntxt, mm, mmap_cleanup);
159}
160
161#endif
#define CloseHandle(h)
APR Error Codes.
APR Miscellaneous library routines.
APR MMAP routines.
APR Portability Routines.
APR Strings library.
#define APR_EBADF
Definition apr_errno.h:704
#define APR_EINVAL
Definition apr_errno.h:711
const void apr_status_t(*) apr_status_t(* APR_DECLARE)(void) apr_pool_pre_cleanup_register(apr_pool_t *p
Definition apr_pools.h:646
apr_size_t size
#define APR_SUCCESS
Definition apr_errno.h:225
#define apr_get_os_error()
Definition apr_errno.h:1217
int apr_status_t
Definition apr_errno.h:44
const char apr_int32_t flag
apr_seek_where_t apr_off_t * offset
const char apr_file_t * file
apr_pool_t * cont
Definition apr_getopt.h:103
#define APR_MMAP_READ
Definition apr_mmap.h:46
#define APR_MMAP_WRITE
Definition apr_mmap.h:48
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
#define APR_RING_INSERT_AFTER(lep, nep, link)
Definition apr_ring.h:273
#define APR_RING_PREV(ep, link)
Definition apr_ring.h:183
#define APR_RING_REMOVE(ep, link)
Definition apr_ring.h:381
#define APR_RING_NEXT(ep, link)
Definition apr_ring.h:177
#define APR_RING_ELEM_INIT(ep, link)
Definition apr_ring.h:212
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
apr_pool_t * cntxt
Definition apr_mmap.h:64
typedef DWORD(WINAPI *apr_winapi_fpt_GetCompressedFileSizeA)(IN LPCSTR lpFileName