]>
git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/rangelist.c
a69f16f0db1dacbe534fb4348f4f323d135ef2db
2 * Copyright (c) 2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/param.h>
25 #include <mach/boolean.h>
27 #include <sys/malloc.h>
29 #include "rangelist.h"
31 static enum rl_overlaptype
rl_scan_from(struct rl_head
*rangelist
, off_t start
, off_t end
, struct rl_entry
**overlap
, struct rl_entry
*range
);
32 static void rl_collapse_forwards(struct rl_head
*rangelist
, struct rl_entry
*range
);
33 static void rl_collapse_backwards(struct rl_head
*rangelist
, struct rl_entry
*range
);
34 static void rl_collapse_neighbors(struct rl_head
*rangelist
, struct rl_entry
*range
);
39 rl_verify(struct rl_head
*rangelist
) {
40 struct rl_entry
*entry
;
43 if (CIRCLEQ_EMPTY(rangelist
)) return;
44 entry
= CIRCLEQ_FIRST(rangelist
);
46 if (CIRCLEQ_NEXT(entry
, rl_link
) == entry
) panic("rl_verify: circular rangelist?!");
47 if ((limit
> 0) && (entry
->rl_start
<= limit
)) panic("rl_verify: bad entry start?!");
48 if (entry
->rl_end
< entry
->rl_start
) panic("rl_verify: bad entry end?!");
49 limit
= entry
->rl_end
;
50 if (entry
== CIRCLEQ_LAST(rangelist
)) return;
51 entry
= CIRCLEQ_NEXT(entry
, rl_link
);
59 * Initialize a range list head
62 rl_init(struct rl_head
*rangelist
)
64 CIRCLEQ_INIT(rangelist
);
70 * Add a range to the list
73 rl_add(off_t start
, off_t end
, struct rl_head
*rangelist
)
75 struct rl_entry
*range
;
76 struct rl_entry
*overlap
;
77 enum rl_overlaptype ovcase
;
80 if (end
< start
) panic("rl_add: end < start?!");
83 ovcase
= rl_scan(rangelist
, start
, end
, &overlap
);
89 * 2) overlap contains range
90 * 3) range contains overlap
91 * 4) overlap starts before range
92 * 5) overlap ends after range
95 case RL_NOOVERLAP
: /* 0: no overlap */
97 * If the list was empty 'prev' is undisturbed and 'overlap' == NULL;
98 * if the search hit a non-overlapping entry PAST the start of the
99 * new range, 'prev' points to ITS predecessor, and 'overlap' points
102 MALLOC(range
, struct rl_entry
*, sizeof(*range
), M_TEMP
, M_WAITOK
);
103 range
->rl_start
= start
;
106 /* Link in the new range: */
108 CIRCLEQ_INSERT_AFTER(rangelist
, overlap
, range
, rl_link
);
110 CIRCLEQ_INSERT_HEAD(rangelist
, range
, rl_link
);
113 /* Check to see if any ranges can be combined (possibly including the immediately
114 preceding range entry)
116 rl_collapse_neighbors(rangelist
, range
);
119 case RL_MATCHINGOVERLAP
: /* 1: overlap == range */
120 case RL_OVERLAPCONTAINSRANGE
: /* 2: overlap contains range */
121 range
= overlap
; /* for debug output below */
124 case RL_OVERLAPISCONTAINED
: /* 3: range contains overlap */
126 * Replace the overlap with the new, larger range:
128 overlap
->rl_start
= start
;
129 overlap
->rl_end
= end
;
130 rl_collapse_neighbors(rangelist
, overlap
);
131 range
= overlap
; /* for debug output below */
134 case RL_OVERLAPSTARTSBEFORE
: /* 4: overlap starts before range */
136 * Expand the overlap area to cover the new range:
138 overlap
->rl_end
= end
;
139 rl_collapse_forwards(rangelist
, overlap
);
140 range
= overlap
; /* for debug output below */
143 case RL_OVERLAPENDSAFTER
: /* 5: overlap ends after range */
145 * Expand the overlap area to cover the new range:
147 overlap
->rl_start
= start
;
148 rl_collapse_backwards(rangelist
, overlap
);
149 range
= overlap
; /* for debug output below */
154 rl_verify(rangelist
);
161 * Remove a range from a range list.
163 * Generally, find the range (or an overlap to that range)
164 * and remove it (or shrink it), then wakeup anyone we can.
167 rl_remove(off_t start
, off_t end
, struct rl_head
*rangelist
)
169 struct rl_entry
*range
, *next_range
, *overlap
, *splitrange
;
170 int ovcase
, moretotest
;
173 if (end
< start
) panic("rl_remove: end < start?!");
176 if (CIRCLEQ_EMPTY(rangelist
)) {
180 range
= CIRCLEQ_FIRST(rangelist
);
181 while ((ovcase
= rl_scan_from(rangelist
, start
, end
, &overlap
, range
))) {
184 case RL_MATCHINGOVERLAP
: /* 1: overlap == range */
185 CIRCLEQ_REMOVE(rangelist
, overlap
, rl_link
);
186 FREE(overlap
, M_TEMP
);
189 case RL_OVERLAPCONTAINSRANGE
: /* 2: overlap contains range: split it */
190 if (overlap
->rl_start
== start
) {
191 overlap
->rl_start
= end
+ 1;
195 if (overlap
->rl_end
== end
) {
196 overlap
->rl_end
= start
- 1;
201 * Make a new range consisting of the last part of the encompassing range
203 MALLOC(splitrange
, struct rl_entry
*, sizeof *splitrange
, M_TEMP
, M_WAITOK
);
204 splitrange
->rl_start
= end
+ 1;
205 splitrange
->rl_end
= overlap
->rl_end
;
206 overlap
->rl_end
= start
- 1;
209 * Now link the new entry into the range list after the range from which it was split:
211 CIRCLEQ_INSERT_AFTER(rangelist
, overlap
, splitrange
, rl_link
);
214 case RL_OVERLAPISCONTAINED
: /* 3: range contains overlap */
215 moretotest
= (overlap
!= CIRCLEQ_LAST(rangelist
));
217 if (CIRCLEQ_NEXT(overlap
, rl_link
) == overlap
) panic("rl_remove: circular range list?!");
219 next_range
= CIRCLEQ_NEXT(overlap
, rl_link
); /* Check before discarding overlap entry */
220 CIRCLEQ_REMOVE(rangelist
, overlap
, rl_link
);
221 FREE(overlap
, M_TEMP
);
228 case RL_OVERLAPSTARTSBEFORE
: /* 4: overlap starts before range */
229 moretotest
= (overlap
!= CIRCLEQ_LAST(rangelist
));
230 overlap
->rl_end
= start
- 1;
233 if (CIRCLEQ_NEXT(overlap
, rl_link
) == overlap
) panic("rl_remove: circular range list?!");
235 range
= CIRCLEQ_NEXT(overlap
, rl_link
);
240 case RL_OVERLAPENDSAFTER
: /* 5: overlap ends after range */
241 overlap
->rl_start
= (end
== RL_INFINITY
? RL_INFINITY
: end
+ 1);
248 rl_verify(rangelist
);
255 * Scan a range list for an entry in a specified range (if any):
257 * NOTE: this returns only the FIRST overlapping range.
258 * There may be more than one.
262 rl_scan(struct rl_head
*rangelist
,
265 struct rl_entry
**overlap
) {
267 if (CIRCLEQ_EMPTY(rangelist
)) {
272 return rl_scan_from(rangelist
, start
, end
, overlap
, CIRCLEQ_FIRST(rangelist
));
278 * Walk the list of ranges for an entry to
279 * find an overlapping range (if any).
281 * NOTE: this returns only the FIRST overlapping range.
282 * There may be more than one.
284 static enum rl_overlaptype
285 rl_scan_from(struct rl_head
*rangelist
,
288 struct rl_entry
**overlap
,
289 struct rl_entry
*range
)
291 if (CIRCLEQ_EMPTY(rangelist
)) {
297 rl_verify(rangelist
);
304 * OK, check for overlap
307 * 0) no overlap (RL_NOOVERLAP)
308 * 1) overlap == range (RL_MATCHINGOVERLAP)
309 * 2) overlap contains range (RL_OVERLAPCONTAINSRANGE)
310 * 3) range contains overlap (RL_OVERLAPISCONTAINED)
311 * 4) overlap starts before range (RL_OVERLAPSTARTSBEFORE)
312 * 5) overlap ends after range (RL_OVERLAPENDSAFTER)
314 if (((range
->rl_end
!= RL_INFINITY
) && (start
> range
->rl_end
)) ||
315 ((end
!= RL_INFINITY
) && (range
->rl_start
> end
))) {
316 /* Case 0 (RL_NOOVERLAP), at least with the current entry: */
317 if ((end
!= RL_INFINITY
) && (range
->rl_start
> end
)) {
321 /* Check the other entries in the list: */
322 if (range
== CIRCLEQ_LAST(rangelist
)) {
326 if (CIRCLEQ_NEXT(range
, rl_link
) == range
) panic("rl_scan_from: circular range list?!");
328 *overlap
= range
= CIRCLEQ_NEXT(range
, rl_link
);
332 if ((range
->rl_start
== start
) && (range
->rl_end
== end
)) {
333 /* Case 1 (RL_MATCHINGOVERLAP) */
334 return RL_MATCHINGOVERLAP
;
337 if ((range
->rl_start
<= start
) &&
338 (end
!= RL_INFINITY
) &&
339 ((range
->rl_end
>= end
) || (range
->rl_end
== RL_INFINITY
))) {
340 /* Case 2 (RL_OVERLAPCONTAINSRANGE) */
341 return RL_OVERLAPCONTAINSRANGE
;
344 if ((start
<= range
->rl_start
) &&
345 ((end
== RL_INFINITY
) ||
346 ((range
->rl_end
!= RL_INFINITY
) && (end
>= range
->rl_end
)))) {
347 /* Case 3 (RL_OVERLAPISCONTAINED) */
348 return RL_OVERLAPISCONTAINED
;
351 if ((range
->rl_start
< start
) &&
352 ((range
->rl_end
>= start
) || (range
->rl_end
== RL_INFINITY
))) {
353 /* Case 4 (RL_OVERLAPSTARTSBEFORE) */
354 return RL_OVERLAPSTARTSBEFORE
;
357 if ((range
->rl_start
> start
) &&
358 (end
!= RL_INFINITY
) &&
359 ((range
->rl_end
> end
) || (range
->rl_end
== RL_INFINITY
))) {
360 /* Case 5 (RL_OVERLAPENDSAFTER) */
361 return RL_OVERLAPENDSAFTER
;
364 /* Control should never reach here... */
366 panic("rl_scan_from: unhandled overlap condition?!");
375 rl_collapse_forwards(struct rl_head
*rangelist
, struct rl_entry
*range
) {
376 struct rl_entry
*next_range
;
379 if (range
== CIRCLEQ_LAST(rangelist
)) return;
382 if (CIRCLEQ_NEXT(range
, rl_link
) == range
) panic("rl_collapse_forwards: circular range list?!");
384 next_range
= CIRCLEQ_NEXT(range
, rl_link
);
385 if ((range
->rl_end
!= RL_INFINITY
) && (range
->rl_end
< next_range
->rl_start
- 1)) return;
387 /* Expand this range to include the next range: */
388 range
->rl_end
= next_range
->rl_end
;
390 /* Remove the now covered range from the list: */
391 CIRCLEQ_REMOVE(rangelist
, next_range
, rl_link
);
392 FREE(next_range
, M_TEMP
);
395 rl_verify(rangelist
);
403 rl_collapse_backwards(struct rl_head
*rangelist
, struct rl_entry
*range
) {
404 struct rl_entry
*prev_range
;
407 if (range
== CIRCLEQ_FIRST(rangelist
)) return;
410 if (CIRCLEQ_PREV(range
, rl_link
) == range
) panic("rl_collapse_backwards: circular range list?!");
412 prev_range
= CIRCLEQ_PREV(range
, rl_link
);
413 if (prev_range
->rl_end
< range
->rl_start
- 1) {
415 rl_verify(rangelist
);
420 /* Expand this range to include the previous range: */
421 range
->rl_start
= prev_range
->rl_start
;
423 /* Remove the now covered range from the list: */
424 CIRCLEQ_REMOVE(rangelist
, prev_range
, rl_link
);
425 FREE(prev_range
, M_TEMP
);
432 rl_collapse_neighbors(struct rl_head
*rangelist
, struct rl_entry
*range
)
434 rl_collapse_forwards(rangelist
, range
);
435 rl_collapse_backwards(rangelist
, range
);