Apache Portable Runtime
apr_mmap.h
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 #ifndef APR_MMAP_H
18 #define APR_MMAP_H
19 
20 /**
21  * @file apr_mmap.h
22  * @brief APR MMAP routines
23  */
24 
25 #include "apr.h"
26 #include "apr_pools.h"
27 #include "apr_errno.h"
28 #include "apr_ring.h"
29 #include "apr_file_io.h" /* for apr_file_t */
30 
31 #ifdef BEOS
32 #include <kernel/OS.h>
33 #endif
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif /* __cplusplus */
38 
39 /**
40  * @defgroup apr_mmap MMAP (Memory Map) Routines
41  * @ingroup APR
42  * @{
43  */
44 
45 /** MMap opened for reading */
46 #define APR_MMAP_READ 1
47 /** MMap opened for writing */
48 #define APR_MMAP_WRITE 2
49 
50 /** @see apr_mmap_t */
51 typedef struct apr_mmap_t apr_mmap_t;
52 
53 /**
54  * @remark
55  * As far as I can tell the only really sane way to store an MMAP is as a
56  * void * and a length. BeOS requires this area_id, but that's just a little
57  * something extra. I am exposing this type, because it doesn't make much
58  * sense to keep it private, and opening it up makes some stuff easier in
59  * Apache.
60  */
61 /** The MMAP structure */
62 struct apr_mmap_t {
63  /** The pool the mmap structure was allocated out of. */
65 #ifdef BEOS
66  /** An area ID. Only valid on BeOS */
67  area_id area;
68 #endif
69 #ifdef WIN32
70  /** The handle of the file mapping */
71  HANDLE mhandle;
72  /** The start of the real memory page area (mapped view) */
73  void *mv;
74  /** The physical start, size and offset */
75  apr_off_t pstart;
76  apr_size_t psize;
77  apr_off_t poffset;
78 #endif
79  /** The start of the memory mapped area */
80  void *mm;
81  /** The amount of data in the mmap */
82  apr_size_t size;
83  /** ring of apr_mmap_t's that reference the same
84  * mmap'ed region; acts in place of a reference count */
86 };
87 
88 #if APR_HAS_MMAP || defined(DOXYGEN)
89 
90 /** @def APR_MMAP_THRESHOLD
91  * Files have to be at least this big before they're mmap()d. This is to deal
92  * with systems where the expense of doing an mmap() and an munmap() outweighs
93  * the benefit for small files. It shouldn't be set lower than 1.
94  */
95 #ifdef MMAP_THRESHOLD
96 # define APR_MMAP_THRESHOLD MMAP_THRESHOLD
97 #else
98 # ifdef SUNOS4
99 # define APR_MMAP_THRESHOLD (8*1024)
100 # else
101 # define APR_MMAP_THRESHOLD 1
102 # endif /* SUNOS4 */
103 #endif /* MMAP_THRESHOLD */
104 
105 /** @def APR_MMAP_LIMIT
106  * Maximum size of MMap region
107  */
108 #ifdef MMAP_LIMIT
109 # define APR_MMAP_LIMIT MMAP_LIMIT
110 #else
111 # define APR_MMAP_LIMIT (4*1024*1024)
112 #endif /* MMAP_LIMIT */
113 
114 /** Can this file be MMaped */
115 #define APR_MMAP_CANDIDATE(filelength) \
116  ((filelength >= APR_MMAP_THRESHOLD) && (filelength < APR_MMAP_LIMIT))
117 
118 /* Function definitions */
119 
120 /**
121  * Create a new mmap'ed file out of an existing APR file.
122  * @param newmmap The newly created mmap'ed file.
123  * @param file The file to turn into an mmap.
124  * @param offset The offset into the file to start the data pointer at.
125  * @param size The size of the file
126  * @param flag bit-wise or of:
127  * <PRE>
128  * APR_MMAP_READ MMap opened for reading
129  * APR_MMAP_WRITE MMap opened for writing
130  * </PRE>
131  * @param cntxt The pool to use when creating the mmap.
132  */
134  apr_file_t *file, apr_off_t offset,
135  apr_size_t size, apr_int32_t flag,
136  apr_pool_t *cntxt);
137 
138 /**
139  * Duplicate the specified MMAP.
140  * @param new_mmap The structure to duplicate into.
141  * @param old_mmap The mmap to duplicate.
142  * @param p The pool to use for new_mmap.
143  */
145  apr_mmap_t *old_mmap,
146  apr_pool_t *p);
147 
148 /**
149  * Remove a mmap'ed.
150  * @param mm The mmap'ed file.
151  */
153 
154 /**
155  * Move the pointer into the mmap'ed file to the specified offset.
156  * @param addr The pointer to the offset specified.
157  * @param mm The mmap'ed file.
158  * @param offset The offset to move to.
159  */
161  apr_off_t offset);
162 
163 #endif /* APR_HAS_MMAP */
164 
165 /** @} */
166 
167 #ifdef __cplusplus
168 }
169 #endif
170 
171 #endif /* ! APR_MMAP_H */
apr_size_t size
Definition: apr_mmap.h:82
APR File I/O Handling.
#define APR_DECLARE(type)
Definition: apr.h:498
void * mm
Definition: apr_mmap.h:80
Definition: apr_mmap.h:62
struct apr_file_t apr_file_t
Definition: apr_file_io.h:209
APR memory allocation.
apr_status_t apr_mmap_delete(apr_mmap_t *mm)
APR_RING_ENTRY(apr_mmap_t) link
APR Error Codes.
APR Platform Definitions.
apr_pool_t * cntxt
Definition: apr_mmap.h:64
apr_status_t apr_mmap_dup(apr_mmap_t **new_mmap, apr_mmap_t *old_mmap, apr_pool_t *p)
struct apr_pool_t apr_pool_t
Definition: apr_pools.h:60
apr_status_t apr_mmap_create(apr_mmap_t **newmmap, apr_file_t *file, apr_off_t offset, apr_size_t size, apr_int32_t flag, apr_pool_t *cntxt)
int apr_status_t
Definition: apr_errno.h:44
apr_status_t apr_mmap_offset(void **addr, apr_mmap_t *mm, apr_off_t offset)
APR Rings.