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.
17 #include "apr_buckets.h"
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
;
29 /* the apr_mmap_t was already cleaned up out from under us */
33 ok
= apr_mmap_offset(&addr
, m
->mmap
, b
->start
);
34 if (ok
!= 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
;
55 static void mmap_bucket_destroy(void *data
)
57 apr_bucket_mmap
*m
= data
;
59 if (apr_bucket_shared_destroy(m
)) {
61 apr_pool_cleanup_kill(m
->mmap
->cntxt
, m
, mmap_bucket_cleanup
);
62 apr_mmap_delete(m
->mmap
);
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
,
77 m
= apr_bucket_alloc(sizeof(*m
), b
->list
);
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
;
90 APU_DECLARE(apr_bucket
*) apr_bucket_mmap_create(apr_mmap_t
*mm
,
93 apr_bucket_alloc_t
*list
)
95 apr_bucket
*b
= apr_bucket_alloc(sizeof(*b
), list
);
98 b
->free
= apr_bucket_free
;
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
;
111 /* the apr_mmap_t was already cleaned up out from under us */
115 /* shortcut if possible */
116 if (apr_pool_is_ancestor(mm
->cntxt
, p
)) {
120 /* duplicate apr_mmap_t into new pool */
121 ok
= apr_mmap_dup(&new_mm
, mm
, p
);
122 if (ok
!= APR_SUCCESS
) {
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
);
135 APU_DECLARE_DATA
const apr_bucket_type_t apr_bucket_type_mmap
= {
136 "MMAP", 5, APR_BUCKET_DATA
,
139 mmap_bucket_setaside
,
140 apr_bucket_shared_split
,
141 apr_bucket_shared_copy