1 /* ====================================================================
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
18 * ====================================================================
21 #include <apr_pools.h>
25 #include "serf_bucket_util.h"
37 serf_bucket_t *serf_bucket_mmap_create(
38 apr_mmap_t *file_mmap,
39 serf_bucket_alloc_t *allocator)
43 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
44 ctx->mmap = file_mmap;
47 ctx->remaining = ctx->mmap->size;
49 return serf_bucket_create(&serf_bucket_type_mmap, allocator, ctx);
52 static apr_status_t serf_mmap_read(serf_bucket_t *bucket,
54 const char **data, apr_size_t *len)
56 mmap_context_t *ctx = bucket->data;
58 if (requested == SERF_READ_ALL_AVAIL || requested > ctx->remaining) {
59 *len = ctx->remaining;
65 /* ### Would it be faster to call this once and do the offset ourselves? */
66 apr_mmap_offset((void**)data, ctx->mmap, ctx->offset);
68 /* For the next read... */
70 ctx->remaining -= *len;
72 if (ctx->remaining == 0) {
78 static apr_status_t serf_mmap_readline(serf_bucket_t *bucket,
79 int acceptable, int *found,
80 const char **data, apr_size_t *len)
82 mmap_context_t *ctx = bucket->data;
85 /* ### Would it be faster to call this once and do the offset ourselves? */
86 apr_mmap_offset((void**)data, ctx->mmap, ctx->offset);
89 /* XXX An overflow is generated if we pass &ctx->remaining to readline.
92 *len = ctx->remaining;
94 serf_util_readline(&end, len, acceptable, found);
99 ctx->remaining -= *len;
101 if (ctx->remaining == 0) {
107 static apr_status_t serf_mmap_peek(serf_bucket_t *bucket,
115 const serf_bucket_type_t serf_bucket_type_mmap = {
119 serf_default_read_iovec,
120 serf_default_read_for_sendfile,
121 serf_default_read_bucket,
123 serf_default_destroy_and_data,
126 #else /* !APR_HAS_MMAP */
128 serf_bucket_t *serf_bucket_mmap_create(apr_mmap_t *file_mmap,
129 serf_bucket_alloc_t *allocator)
134 const serf_bucket_type_t serf_bucket_type_mmap = {