1 /* Copyright 2002-2004 Justin Erenkrantz and Greg Stein
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <apr_pools.h>
20 #include "serf_bucket_util.h"
32 serf_bucket_t *serf_bucket_mmap_create(
33 apr_mmap_t *file_mmap,
34 serf_bucket_alloc_t *allocator)
38 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
39 ctx->mmap = file_mmap;
42 ctx->remaining = ctx->mmap->size;
44 return serf_bucket_create(&serf_bucket_type_mmap, allocator, ctx);
47 static apr_status_t serf_mmap_read(serf_bucket_t *bucket,
49 const char **data, apr_size_t *len)
51 mmap_context_t *ctx = bucket->data;
53 if (requested == SERF_READ_ALL_AVAIL || requested > ctx->remaining) {
54 *len = ctx->remaining;
60 /* ### Would it be faster to call this once and do the offset ourselves? */
61 apr_mmap_offset((void**)data, ctx->mmap, ctx->offset);
63 /* For the next read... */
65 ctx->remaining -= *len;
67 if (ctx->remaining == 0) {
73 static apr_status_t serf_mmap_readline(serf_bucket_t *bucket,
74 int acceptable, int *found,
75 const char **data, apr_size_t *len)
77 mmap_context_t *ctx = bucket->data;
80 /* ### Would it be faster to call this once and do the offset ourselves? */
81 apr_mmap_offset((void**)data, ctx->mmap, ctx->offset);
84 /* XXX An overflow is generated if we pass &ctx->remaining to readline.
87 *len = ctx->remaining;
89 serf_util_readline(&end, len, acceptable, found);
94 ctx->remaining -= *len;
96 if (ctx->remaining == 0) {
102 static apr_status_t serf_mmap_peek(serf_bucket_t *bucket,
110 const serf_bucket_type_t serf_bucket_type_mmap = {
114 serf_default_read_iovec,
115 serf_default_read_for_sendfile,
116 serf_default_read_bucket,
118 serf_default_destroy_and_data,
121 #else /* !APR_HAS_MMAP */
123 serf_bucket_t *serf_bucket_mmap_create(apr_mmap_t *file_mmap,
124 serf_bucket_alloc_t *allocator)
129 const serf_bucket_type_t serf_bucket_type_mmap = {