]>
git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/rangelist.c
2 * Copyright (c) 2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <sys/param.h>
24 #include <mach/boolean.h>
26 #include <sys/malloc.h>
28 #include "rangelist.h"
30 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
);
31 static void rl_collapse_forwards(struct rl_head
*rangelist
, struct rl_entry
*range
);
32 static void rl_collapse_backwards(struct rl_head
*rangelist
, struct rl_entry
*range
);
33 static void rl_collapse_neighbors(struct rl_head
*rangelist
, struct rl_entry
*range
);
38 rl_verify(struct rl_head
*rangelist
) {
39 struct rl_entry
*entry
;
42 if (CIRCLEQ_EMPTY(rangelist
)) return;
43 entry
= CIRCLEQ_FIRST(rangelist
);
45 if (CIRCLEQ_NEXT(entry
, rl_link
) == entry
) panic("rl_verify: circular rangelist?!");
46 if ((limit
> 0) && (entry
->rl_start
<= limit
)) panic("rl_verify: bad entry start?!");
47 if (entry
->rl_end
< entry
->rl_start
) panic("rl_verify: bad entry end?!");
48 limit
= entry
->rl_end
;
49 if (entry
== CIRCLEQ_LAST(rangelist
)) return;
50 entry
= CIRCLEQ_NEXT(entry
, rl_link
);
58 * Initialize a range list head
61 rl_init(struct rl_head
*rangelist
)
63 CIRCLEQ_INIT(rangelist
);
69 * Add a range to the list
72 rl_add(off_t start
, off_t end
, struct rl_head
*rangelist
)
74 struct rl_entry
*range
;
75 struct rl_entry
*overlap
;
76 enum rl_overlaptype ovcase
;
79 if (end
< start
) panic("rl_add: end < start?!");
82 ovcase
= rl_scan(rangelist
, start
, end
, &overlap
);
88 * 2) overlap contains range
89 * 3) range contains overlap
90 * 4) overlap starts before range
91 * 5) overlap ends after range
94 case RL_NOOVERLAP
: /* 0: no overlap */
96 * If the list was empty 'prev' is undisturbed and 'overlap' == NULL;
97 * if the search hit a non-overlapping entry PAST the start of the
98 * new range, 'prev' points to ITS predecessor, and 'overlap' points
101 MALLOC(range
, struct rl_entry
*, sizeof(*range
), M_TEMP
, M_WAITOK
);
102 range
->rl_start
= start
;
105 /* Link in the new range: */
107 CIRCLEQ_INSERT_AFTER(rangelist
, overlap
, range
, rl_link
);
109 CIRCLEQ_INSERT_HEAD(rangelist
, range
, rl_link
);
112 /* Check to see if any ranges can be combined (possibly including the immediately
113 preceding range entry)
115 rl_collapse_neighbors(rangelist
, range
);
118 case RL_MATCHINGOVERLAP
: /* 1: overlap == range */
119 case RL_OVERLAPCONTAINSRANGE
: /* 2: overlap contains range */
120 range
= overlap
; /* for debug output below */
123 case RL_OVERLAPISCONTAINED
: /* 3: range contains overlap */
125 * Replace the overlap with the new, larger range:
127 overlap
->rl_start
= start
;
128 overlap
->rl_end
= end
;
129 rl_collapse_neighbors(rangelist
, overlap
);
130 range
= overlap
; /* for debug output below */
133 case RL_OVERLAPSTARTSBEFORE
: /* 4: overlap starts before range */
135 * Expand the overlap area to cover the new range:
137 overlap
->rl_end
= end
;
138 rl_collapse_forwards(rangelist
, overlap
);
139 range
= overlap
; /* for debug output below */
142 case RL_OVERLAPENDSAFTER
: /* 5: overlap ends after range */
144 * Expand the overlap area to cover the new range:
146 overlap
->rl_start
= start
;
147 rl_collapse_backwards(rangelist
, overlap
);
148 range
= overlap
; /* for debug output below */
153 rl_verify(rangelist
);
160 * Remove a range from a range list.
162 * Generally, find the range (or an overlap to that range)
163 * and remove it (or shrink it), then wakeup anyone we can.
166 rl_remove(off_t start
, off_t end
, struct rl_head
*rangelist
)
168 struct rl_entry
*range
, *next_range
, *overlap
, *splitrange
;
169 int ovcase
, moretotest
;
172 if (end
< start
) panic("rl_remove: end < start?!");
175 if (CIRCLEQ_EMPTY(rangelist
)) {
179 range
= CIRCLEQ_FIRST(rangelist
);
180 while ((ovcase
= rl_scan_from(rangelist
, start
, end
, &overlap
, range
))) {
183 case RL_MATCHINGOVERLAP
: /* 1: overlap == range */
184 CIRCLEQ_REMOVE(rangelist
, overlap
, rl_link
);
185 FREE(overlap
, M_TEMP
);
188 case RL_OVERLAPCONTAINSRANGE
: /* 2: overlap contains range: split it */
189 if (overlap
->rl_start
== start
) {
190 overlap
->rl_start
= end
+ 1;
194 if (overlap
->rl_end
== end
) {
195 overlap
->rl_end
= start
- 1;
200 * Make a new range consisting of the last part of the encompassing range
202 MALLOC(splitrange
, struct rl_entry
*, sizeof *splitrange
, M_TEMP
, M_WAITOK
);
203 splitrange
->rl_start
= end
+ 1;
204 splitrange
->rl_end
= overlap
->rl_end
;
205 overlap
->rl_end
= start
- 1;
208 * Now link the new entry into the range list after the range from which it was split:
210 CIRCLEQ_INSERT_AFTER(rangelist
, overlap
, splitrange
, rl_link
);
213 case RL_OVERLAPISCONTAINED
: /* 3: range contains overlap */
214 moretotest
= (overlap
!= CIRCLEQ_LAST(rangelist
));
216 if (CIRCLEQ_NEXT(overlap
, rl_link
) == overlap
) panic("rl_remove: circular range list?!");
218 next_range
= CIRCLEQ_NEXT(overlap
, rl_link
); /* Check before discarding overlap entry */
219 CIRCLEQ_REMOVE(rangelist
, overlap
, rl_link
);
220 FREE(overlap
, M_TEMP
);
227 case RL_OVERLAPSTARTSBEFORE
: /* 4: overlap starts before range */
228 moretotest
= (overlap
!= CIRCLEQ_LAST(rangelist
));
229 overlap
->rl_end
= start
- 1;
232 if (CIRCLEQ_NEXT(overlap
, rl_link
) == overlap
) panic("rl_remove: circular range list?!");
234 range
= CIRCLEQ_NEXT(overlap
, rl_link
);
239 case RL_OVERLAPENDSAFTER
: /* 5: overlap ends after range */
240 overlap
->rl_start
= (end
== RL_INFINITY
? RL_INFINITY
: end
+ 1);
247 rl_verify(rangelist
);
254 * Scan a range list for an entry in a specified range (if any):
256 * NOTE: this returns only the FIRST overlapping range.
257 * There may be more than one.
261 rl_scan(struct rl_head
*rangelist
,
264 struct rl_entry
**overlap
) {
266 if (CIRCLEQ_EMPTY(rangelist
)) {
271 return rl_scan_from(rangelist
, start
, end
, overlap
, CIRCLEQ_FIRST(rangelist
));
277 * Walk the list of ranges for an entry to
278 * find an overlapping range (if any).
280 * NOTE: this returns only the FIRST overlapping range.
281 * There may be more than one.
283 static enum rl_overlaptype
284 rl_scan_from(struct rl_head
*rangelist
,
287 struct rl_entry
**overlap
,
288 struct rl_entry
*range
)
290 if (CIRCLEQ_EMPTY(rangelist
)) {
296 rl_verify(rangelist
);
303 * OK, check for overlap
306 * 0) no overlap (RL_NOOVERLAP)
307 * 1) overlap == range (RL_MATCHINGOVERLAP)
308 * 2) overlap contains range (RL_OVERLAPCONTAINSRANGE)
309 * 3) range contains overlap (RL_OVERLAPISCONTAINED)
310 * 4) overlap starts before range (RL_OVERLAPSTARTSBEFORE)
311 * 5) overlap ends after range (RL_OVERLAPENDSAFTER)
313 if (((range
->rl_end
!= RL_INFINITY
) && (start
> range
->rl_end
)) ||
314 ((end
!= RL_INFINITY
) && (range
->rl_start
> end
))) {
315 /* Case 0 (RL_NOOVERLAP), at least with the current entry: */
316 if ((end
!= RL_INFINITY
) && (range
->rl_start
> end
)) {
320 /* Check the other entries in the list: */
321 if (range
== CIRCLEQ_LAST(rangelist
)) {
325 if (CIRCLEQ_NEXT(range
, rl_link
) == range
) panic("rl_scan_from: circular range list?!");
327 *overlap
= range
= CIRCLEQ_NEXT(range
, rl_link
);
331 if ((range
->rl_start
== start
) && (range
->rl_end
== end
)) {
332 /* Case 1 (RL_MATCHINGOVERLAP) */
333 return RL_MATCHINGOVERLAP
;
336 if ((range
->rl_start
<= start
) &&
337 (end
!= RL_INFINITY
) &&
338 ((range
->rl_end
>= end
) || (range
->rl_end
== RL_INFINITY
))) {
339 /* Case 2 (RL_OVERLAPCONTAINSRANGE) */
340 return RL_OVERLAPCONTAINSRANGE
;
343 if ((start
<= range
->rl_start
) &&
344 ((end
== RL_INFINITY
) ||
345 ((range
->rl_end
!= RL_INFINITY
) && (end
>= range
->rl_end
)))) {
346 /* Case 3 (RL_OVERLAPISCONTAINED) */
347 return RL_OVERLAPISCONTAINED
;
350 if ((range
->rl_start
< start
) &&
351 ((range
->rl_end
>= start
) || (range
->rl_end
== RL_INFINITY
))) {
352 /* Case 4 (RL_OVERLAPSTARTSBEFORE) */
353 return RL_OVERLAPSTARTSBEFORE
;
356 if ((range
->rl_start
> start
) &&
357 (end
!= RL_INFINITY
) &&
358 ((range
->rl_end
> end
) || (range
->rl_end
== RL_INFINITY
))) {
359 /* Case 5 (RL_OVERLAPENDSAFTER) */
360 return RL_OVERLAPENDSAFTER
;
363 /* Control should never reach here... */
365 panic("rl_scan_from: unhandled overlap condition?!");
374 rl_collapse_forwards(struct rl_head
*rangelist
, struct rl_entry
*range
) {
375 struct rl_entry
*next_range
;
378 if (range
== CIRCLEQ_LAST(rangelist
)) return;
381 if (CIRCLEQ_NEXT(range
, rl_link
) == range
) panic("rl_collapse_forwards: circular range list?!");
383 next_range
= CIRCLEQ_NEXT(range
, rl_link
);
384 if ((range
->rl_end
!= RL_INFINITY
) && (range
->rl_end
< next_range
->rl_start
- 1)) return;
386 /* Expand this range to include the next range: */
387 range
->rl_end
= next_range
->rl_end
;
389 /* Remove the now covered range from the list: */
390 CIRCLEQ_REMOVE(rangelist
, next_range
, rl_link
);
391 FREE(next_range
, M_TEMP
);
394 rl_verify(rangelist
);
402 rl_collapse_backwards(struct rl_head
*rangelist
, struct rl_entry
*range
) {
403 struct rl_entry
*prev_range
;
406 if (range
== CIRCLEQ_FIRST(rangelist
)) return;
409 if (CIRCLEQ_PREV(range
, rl_link
) == range
) panic("rl_collapse_backwards: circular range list?!");
411 prev_range
= CIRCLEQ_PREV(range
, rl_link
);
412 if (prev_range
->rl_end
< range
->rl_start
- 1) {
414 rl_verify(rangelist
);
419 /* Expand this range to include the previous range: */
420 range
->rl_start
= prev_range
->rl_start
;
422 /* Remove the now covered range from the list: */
423 CIRCLEQ_REMOVE(rangelist
, prev_range
, rl_link
);
424 FREE(prev_range
, M_TEMP
);
431 rl_collapse_neighbors(struct rl_head
*rangelist
, struct rl_entry
*range
)
433 rl_collapse_forwards(rangelist
, range
);
434 rl_collapse_backwards(rangelist
, range
);