]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/serf/buckets/mmap_buckets.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / serf / buckets / mmap_buckets.c
1 /* Copyright 2002-2004 Justin Erenkrantz and Greg Stein
2  *
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
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <apr_pools.h>
17 #include <apr_mmap.h>
18
19 #include "serf.h"
20 #include "serf_bucket_util.h"
21
22 #if APR_HAS_MMAP
23
24 typedef struct {
25     apr_mmap_t *mmap;
26     void *current;
27     apr_off_t offset;
28     apr_off_t remaining;
29 } mmap_context_t;
30
31
32 serf_bucket_t *serf_bucket_mmap_create(
33     apr_mmap_t *file_mmap,
34     serf_bucket_alloc_t *allocator)
35 {
36     mmap_context_t *ctx;
37
38     ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
39     ctx->mmap = file_mmap;
40     ctx->current = NULL;
41     ctx->offset = 0;
42     ctx->remaining = ctx->mmap->size;
43
44     return serf_bucket_create(&serf_bucket_type_mmap, allocator, ctx);
45 }
46
47 static apr_status_t serf_mmap_read(serf_bucket_t *bucket,
48                                      apr_size_t requested,
49                                      const char **data, apr_size_t *len)
50 {
51     mmap_context_t *ctx = bucket->data;
52
53     if (requested == SERF_READ_ALL_AVAIL || requested > ctx->remaining) {
54         *len = ctx->remaining;
55     }
56     else {
57         *len = requested;
58     }
59
60     /* ### Would it be faster to call this once and do the offset ourselves? */
61     apr_mmap_offset((void**)data, ctx->mmap, ctx->offset);
62
63     /* For the next read... */
64     ctx->offset += *len;
65     ctx->remaining -= *len;
66
67     if (ctx->remaining == 0) {
68         return APR_EOF;
69     }
70     return APR_SUCCESS;
71 }
72
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)
76 {
77     mmap_context_t *ctx = bucket->data;
78     const char *end;
79
80     /* ### Would it be faster to call this once and do the offset ourselves? */
81     apr_mmap_offset((void**)data, ctx->mmap, ctx->offset);
82     end = *data;
83
84     /* XXX An overflow is generated if we pass &ctx->remaining to readline.
85      * Not real clear why.
86      */
87     *len = ctx->remaining;
88
89     serf_util_readline(&end, len, acceptable, found);
90
91     *len = end - *data;
92
93     ctx->offset += *len;
94     ctx->remaining -= *len;
95
96     if (ctx->remaining == 0) {
97         return APR_EOF;
98     }
99     return APR_SUCCESS;
100 }
101
102 static apr_status_t serf_mmap_peek(serf_bucket_t *bucket,
103                                      const char **data,
104                                      apr_size_t *len)
105 {
106     /* Oh, bah. */
107     return APR_ENOTIMPL;
108 }
109
110 const serf_bucket_type_t serf_bucket_type_mmap = {
111     "MMAP",
112     serf_mmap_read,
113     serf_mmap_readline,
114     serf_default_read_iovec,
115     serf_default_read_for_sendfile,
116     serf_default_read_bucket,
117     serf_mmap_peek,
118     serf_default_destroy_and_data,
119 };
120
121 #else /* !APR_HAS_MMAP */
122
123 serf_bucket_t *serf_bucket_mmap_create(apr_mmap_t *file_mmap,
124                                        serf_bucket_alloc_t *allocator)
125 {
126     return NULL;
127 }
128
129 const serf_bucket_type_t serf_bucket_type_mmap = {
130     "MMAP",
131     NULL,
132     NULL,
133     NULL,
134     NULL,
135     NULL,
136     NULL,
137     NULL,
138 };
139
140 #endif