4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include "streams_mmap.h"
30 #include "streams_common.h"
33 * Single-byte character memory map-based streams implementation
37 stream_mmap_prime(stream_t
*str
)
41 if (stream_is_primed(str
))
42 return (PRIME_SUCCEEDED
);
44 stream_set(str
, STREAM_PRIMED
);
46 if (str
->s_buffer_size
== 0) {
47 stream_set(str
, STREAM_EOS_REACHED
);
48 return (PRIME_FAILED_EMPTY_FILE
);
51 str
->s_current
.l_data
.sp
= str
->s_buffer
;
52 str
->s_type
.SF
.s_release_origin
= str
->s_buffer
;
53 if ((nl
= (char *)memchr(str
->s_buffer
, '\n', str
->s_buffer_size
)) ==
55 warn(WMSG_NEWLINE_ADDED
, str
->s_filename
);
56 str
->s_current
.l_data_length
= str
->s_buffer_size
;
58 str
->s_current
.l_data_length
= nl
- (char *)str
->s_buffer
;
61 str
->s_current
.l_collate
.sp
= NULL
;
62 str
->s_current
.l_collate_length
= 0;
64 __S(stats_incr_fetches());
65 return (PRIME_SUCCEEDED
);
69 * stream_mmap_fetch() sets the fields of str->s_current to delimit the next
73 stream_mmap_fetch(stream_t
*str
)
75 ssize_t dist_to_buf_end
;
78 ASSERT(stream_is_primed(str
));
79 ASSERT((str
->s_status
& STREAM_EOS_REACHED
) == 0);
82 * adding one for newline
84 str
->s_current
.l_data
.sp
= str
->s_current
.l_data
.sp
+
85 str
->s_current
.l_data_length
+ 1;
87 dist_to_buf_end
= str
->s_buffer_size
- (str
->s_current
.l_data
.sp
88 - (char *)str
->s_buffer
);
89 ASSERT(dist_to_buf_end
>= 0 && dist_to_buf_end
<= str
->s_buffer_size
);
91 next_nl
= memchr(str
->s_current
.l_data
.sp
, '\n', dist_to_buf_end
);
94 str
->s_current
.l_data_length
= next_nl
95 - str
->s_current
.l_data
.sp
;
97 warn(WMSG_NEWLINE_ADDED
, str
->s_filename
);
98 str
->s_current
.l_data_length
= dist_to_buf_end
;
102 * adding one for newline
104 if (str
->s_current
.l_data
.sp
+ str
->s_current
.l_data_length
+ 1 >=
105 (char *)str
->s_buffer
+ str
->s_buffer_size
)
106 stream_set(str
, STREAM_EOS_REACHED
);
108 str
->s_current
.l_collate_length
= 0;
110 __S(stats_incr_fetches());
111 return (NEXT_LINE_COMPLETE
);
115 stream_mmap_is_closable(stream_t
*str
)
117 if (str
->s_status
& STREAM_OPEN
)
123 stream_mmap_close(stream_t
*str
)
125 if (str
->s_type
.SF
.s_fd
> -1) {
126 (void) close(str
->s_type
.SF
.s_fd
);
127 stream_unset(str
, STREAM_OPEN
);
135 stream_mmap_free(stream_t
*str
)
137 if (!(str
->s_status
& STREAM_OPEN
) ||
138 (str
->s_consumer
!= NULL
&&
139 str
->s_consumer
->s_status
& STREAM_NOT_FREEABLE
))
142 if (str
->s_buffer
== NULL
)
145 if (munmap(str
->s_buffer
, str
->s_buffer_size
) < 0)
146 die(EMSG_MUNMAP
, str
->s_filename
);
148 str
->s_buffer
= NULL
;
149 str
->s_buffer_size
= 0;
151 stream_unset(str
, STREAM_PRIMED
);
157 stream_mmap_eos(stream_t
*str
)
161 if (str
== NULL
|| str
->s_status
& STREAM_EOS_REACHED
)
165 * If the file's size is known to be zero, then we are at EOS; the
166 * remaining checks are only sensible if we successfully primed this
167 * stream. The additional character is for the optional newline.
169 if (str
->s_filesize
== 0 ||
170 (stream_is_primed(str
) && str
->s_current
.l_data
.sp
-
171 (char *)str
->s_buffer
+ str
->s_current
.l_data_length
+ 1 >=
172 str
->s_buffer_size
)) {
174 stream_set(str
, STREAM_EOS_REACHED
);
180 #define ALIGNED (~(ulong_t)(PAGESIZE - 1))
183 * In certain cases, we know that we will never need the data on a page again
184 * for the duration of the sort. (These cases are associated with merges
185 * involving temporary files.) We can thus release all pages previous to the
186 * page containing the current line, using the MADV_DONTNEED flag to
187 * madvise(3C). This additional memory management improves our chances of
188 * avoiding a paging situation, by evicting pages we know are of no use.
191 stream_mmap_release_line(stream_t
*str
)
193 caddr_t origin
= str
->s_type
.SF
.s_release_origin
;
196 while ((caddr_t
)((ulong_t
)str
->s_current
.l_data
.sp
& ALIGNED
) -
197 (origin
+ release
) >= DEFAULT_RELEASE_SIZE
)
198 release
+= DEFAULT_RELEASE_SIZE
;
203 if (madvise(origin
, release
, MADV_DONTNEED
) == -1)
204 warn(gettext("madvise failed"));
206 str
->s_type
.SF
.s_release_origin
+= release
;
209 const stream_ops_t stream_mmap_ops
= {
210 stream_mmap_is_closable
,
219 stream_mmap_release_line
,