Versions 1.2.8/0.9.13 were released December 4, 2006
[apr-util.git] / buckets / apr_buckets_mmap.c
blob3e7a9d73c25d5e20f860da0065f9825f53600c56
1 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
2 * applicable.
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.
17 #include "apr_buckets.h"
19 #if APR_HAS_MMAP
21 static apr_status_t mmap_bucket_read(apr_bucket *b, const char **str,
22 apr_size_t *length, apr_read_type_e block)
24 apr_bucket_mmap *m = b->data;
25 apr_status_t ok;
26 void *addr;
28 if (!m->mmap) {
29 /* the apr_mmap_t was already cleaned up out from under us */
30 return APR_EINVAL;
33 ok = apr_mmap_offset(&addr, m->mmap, b->start);
34 if (ok != APR_SUCCESS) {
35 return ok;
37 *str = addr;
38 *length = b->length;
39 return APR_SUCCESS;
42 static apr_status_t mmap_bucket_cleanup(void *data)
44 /* the apr_mmap_t is about to disappear out from under us, so we
45 * have no choice but to pretend it doesn't exist anymore. the
46 * refcount is now useless because there's nothing to refer to
47 * anymore. so the only valid action on any remaining referrer
48 * is to delete it. no more reads, no more anything. */
49 apr_bucket_mmap *m = data;
51 m->mmap = NULL;
52 return APR_SUCCESS;
55 static void mmap_bucket_destroy(void *data)
57 apr_bucket_mmap *m = data;
59 if (apr_bucket_shared_destroy(m)) {
60 if (m->mmap) {
61 apr_pool_cleanup_kill(m->mmap->cntxt, m, mmap_bucket_cleanup);
62 apr_mmap_delete(m->mmap);
64 apr_bucket_free(m);
69 * XXX: are the start and length arguments useful?
71 APU_DECLARE(apr_bucket *) apr_bucket_mmap_make(apr_bucket *b, apr_mmap_t *mm,
72 apr_off_t start,
73 apr_size_t length)
75 apr_bucket_mmap *m;
77 m = apr_bucket_alloc(sizeof(*m), b->list);
78 m->mmap = mm;
80 apr_pool_cleanup_register(mm->cntxt, m, mmap_bucket_cleanup,
81 apr_pool_cleanup_null);
83 b = apr_bucket_shared_make(b, m, start, length);
84 b->type = &apr_bucket_type_mmap;
86 return b;
90 APU_DECLARE(apr_bucket *) apr_bucket_mmap_create(apr_mmap_t *mm,
91 apr_off_t start,
92 apr_size_t length,
93 apr_bucket_alloc_t *list)
95 apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
97 APR_BUCKET_INIT(b);
98 b->free = apr_bucket_free;
99 b->list = list;
100 return apr_bucket_mmap_make(b, mm, start, length);
103 static apr_status_t mmap_bucket_setaside(apr_bucket *b, apr_pool_t *p)
105 apr_bucket_mmap *m = b->data;
106 apr_mmap_t *mm = m->mmap;
107 apr_mmap_t *new_mm;
108 apr_status_t ok;
110 if (!mm) {
111 /* the apr_mmap_t was already cleaned up out from under us */
112 return APR_EINVAL;
115 /* shortcut if possible */
116 if (apr_pool_is_ancestor(mm->cntxt, p)) {
117 return APR_SUCCESS;
120 /* duplicate apr_mmap_t into new pool */
121 ok = apr_mmap_dup(&new_mm, mm, p);
122 if (ok != APR_SUCCESS) {
123 return ok;
126 /* decrement refcount on old apr_bucket_mmap */
127 mmap_bucket_destroy(m);
129 /* create new apr_bucket_mmap pointing to new apr_mmap_t */
130 apr_bucket_mmap_make(b, new_mm, b->start, b->length);
132 return APR_SUCCESS;
135 APU_DECLARE_DATA const apr_bucket_type_t apr_bucket_type_mmap = {
136 "MMAP", 5, APR_BUCKET_DATA,
137 mmap_bucket_destroy,
138 mmap_bucket_read,
139 mmap_bucket_setaside,
140 apr_bucket_shared_split,
141 apr_bucket_shared_copy
144 #endif