]>
git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/rangelist.c
2 * Copyright (c) 2001, 2006-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
31 #include <sys/param.h>
32 #include <mach/boolean.h>
34 #include <sys/malloc.h>
36 #include "rangelist.h"
38 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
);
39 static void rl_collapse_forwards(struct rl_head
*rangelist
, struct rl_entry
*range
);
40 static void rl_collapse_backwards(struct rl_head
*rangelist
, struct rl_entry
*range
);
41 static void rl_collapse_neighbors(struct rl_head
*rangelist
, struct rl_entry
*range
);
46 rl_verify(struct rl_head
*rangelist
) {
47 struct rl_entry
*entry
;
48 struct rl_entry
*next
;
51 TAILQ_FOREACH_SAFE(rangelist
, entry
, rl_link
, next
) {
52 if ((limit
> 0) && (entry
->rl_start
<= limit
)) panic("hfs: rl_verify: bad entry start?!");
53 if (entry
->rl_end
< entry
->rl_start
) panic("hfs: rl_verify: bad entry end?!");
54 limit
= entry
->rl_end
;
62 * Initialize a range list head
65 rl_init(struct rl_head
*rangelist
)
67 TAILQ_INIT(rangelist
);
73 * Add a range to the list
76 rl_add(off_t start
, off_t end
, struct rl_head
*rangelist
)
78 struct rl_entry
*range
;
79 struct rl_entry
*overlap
;
80 enum rl_overlaptype ovcase
;
83 if (end
< start
) panic("hfs: rl_add: end < start?!");
86 ovcase
= rl_scan(rangelist
, start
, end
, &overlap
);
92 * 2) overlap contains range
93 * 3) range contains overlap
94 * 4) overlap starts before range
95 * 5) overlap ends after range
98 case RL_NOOVERLAP
: /* 0: no overlap */
100 * If the list was empty 'prev' is undisturbed and 'overlap' == NULL;
101 * if the search hit a non-overlapping entry PAST the start of the
102 * new range, 'prev' points to ITS predecessor, and 'overlap' points
105 MALLOC(range
, struct rl_entry
*, sizeof(*range
), M_TEMP
, M_WAITOK
);
106 range
->rl_start
= start
;
109 /* Link in the new range: */
111 TAILQ_INSERT_AFTER(rangelist
, overlap
, range
, rl_link
);
113 TAILQ_INSERT_HEAD(rangelist
, range
, rl_link
);
116 /* Check to see if any ranges can be combined (possibly including the immediately
117 preceding range entry)
119 rl_collapse_neighbors(rangelist
, range
);
122 case RL_MATCHINGOVERLAP
: /* 1: overlap == range */
123 case RL_OVERLAPCONTAINSRANGE
: /* 2: overlap contains range */
124 range
= overlap
; /* for debug output below */
127 case RL_OVERLAPISCONTAINED
: /* 3: range contains overlap */
129 * Replace the overlap with the new, larger range:
131 overlap
->rl_start
= start
;
132 overlap
->rl_end
= end
;
133 rl_collapse_neighbors(rangelist
, overlap
);
134 range
= overlap
; /* for debug output below */
137 case RL_OVERLAPSTARTSBEFORE
: /* 4: overlap starts before range */
139 * Expand the overlap area to cover the new range:
141 overlap
->rl_end
= end
;
142 rl_collapse_forwards(rangelist
, overlap
);
143 range
= overlap
; /* for debug output below */
146 case RL_OVERLAPENDSAFTER
: /* 5: overlap ends after range */
148 * Expand the overlap area to cover the new range:
150 overlap
->rl_start
= start
;
151 rl_collapse_backwards(rangelist
, overlap
);
152 range
= overlap
; /* for debug output below */
157 rl_verify(rangelist
);
164 * Remove a range from a range list.
166 * Generally, find the range (or an overlap to that range)
167 * and remove it (or shrink it), then wakeup anyone we can.
170 rl_remove(off_t start
, off_t end
, struct rl_head
*rangelist
)
172 struct rl_entry
*range
, *next_range
, *overlap
, *splitrange
;
176 if (end
< start
) panic("hfs: rl_remove: end < start?!");
179 if (TAILQ_EMPTY(rangelist
)) {
183 range
= TAILQ_FIRST(rangelist
);
184 while ((ovcase
= rl_scan_from(rangelist
, start
, end
, &overlap
, range
))) {
187 case RL_MATCHINGOVERLAP
: /* 1: overlap == range */
188 TAILQ_REMOVE(rangelist
, overlap
, rl_link
);
189 FREE(overlap
, M_TEMP
);
192 case RL_OVERLAPCONTAINSRANGE
: /* 2: overlap contains range: split it */
193 if (overlap
->rl_start
== start
) {
194 overlap
->rl_start
= end
+ 1;
198 if (overlap
->rl_end
== end
) {
199 overlap
->rl_end
= start
- 1;
204 * Make a new range consisting of the last part of the encompassing range
206 MALLOC(splitrange
, struct rl_entry
*, sizeof *splitrange
, M_TEMP
, M_WAITOK
);
207 splitrange
->rl_start
= end
+ 1;
208 splitrange
->rl_end
= overlap
->rl_end
;
209 overlap
->rl_end
= start
- 1;
212 * Now link the new entry into the range list after the range from which it was split:
214 TAILQ_INSERT_AFTER(rangelist
, overlap
, splitrange
, rl_link
);
217 case RL_OVERLAPISCONTAINED
: /* 3: range contains overlap */
218 /* Check before discarding overlap entry */
219 next_range
= TAILQ_NEXT(overlap
, rl_link
);
220 TAILQ_REMOVE(rangelist
, overlap
, rl_link
);
221 FREE(overlap
, M_TEMP
);
228 case RL_OVERLAPSTARTSBEFORE
: /* 4: overlap starts before range */
229 overlap
->rl_end
= start
- 1;
230 range
= TAILQ_NEXT(overlap
, rl_link
);
236 case RL_OVERLAPENDSAFTER
: /* 5: overlap ends after range */
237 overlap
->rl_start
= (end
== RL_INFINITY
? RL_INFINITY
: end
+ 1);
244 rl_verify(rangelist
);
251 * Scan a range list for an entry in a specified range (if any):
253 * NOTE: this returns only the FIRST overlapping range.
254 * There may be more than one.
258 rl_scan(struct rl_head
*rangelist
,
261 struct rl_entry
**overlap
) {
263 if (TAILQ_EMPTY(rangelist
)) {
268 return rl_scan_from(rangelist
, start
, end
, overlap
, TAILQ_FIRST(rangelist
));
274 * Walk the list of ranges for an entry to
275 * find an overlapping range (if any).
277 * NOTE: this returns only the FIRST overlapping range.
278 * There may be more than one.
280 static enum rl_overlaptype
281 rl_scan_from(struct rl_head
*rangelist
,
284 struct rl_entry
**overlap
,
285 struct rl_entry
*range
)
287 if (TAILQ_EMPTY(rangelist
)) {
293 rl_verify(rangelist
);
300 * OK, check for overlap
303 * 0) no overlap (RL_NOOVERLAP)
304 * 1) overlap == range (RL_MATCHINGOVERLAP)
305 * 2) overlap contains range (RL_OVERLAPCONTAINSRANGE)
306 * 3) range contains overlap (RL_OVERLAPISCONTAINED)
307 * 4) overlap starts before range (RL_OVERLAPSTARTSBEFORE)
308 * 5) overlap ends after range (RL_OVERLAPENDSAFTER)
310 if (((range
->rl_end
!= RL_INFINITY
) && (start
> range
->rl_end
)) ||
311 ((end
!= RL_INFINITY
) && (range
->rl_start
> end
))) {
312 /* Case 0 (RL_NOOVERLAP), at least with the current entry: */
313 if ((end
!= RL_INFINITY
) && (range
->rl_start
> end
)) {
317 range
= TAILQ_NEXT(range
, rl_link
);
318 /* Check the other entries in the list: */
327 if ((range
->rl_start
== start
) && (range
->rl_end
== end
)) {
328 /* Case 1 (RL_MATCHINGOVERLAP) */
329 return RL_MATCHINGOVERLAP
;
332 if ((range
->rl_start
<= start
) &&
333 (end
!= RL_INFINITY
) &&
334 ((range
->rl_end
>= end
) || (range
->rl_end
== RL_INFINITY
))) {
335 /* Case 2 (RL_OVERLAPCONTAINSRANGE) */
336 return RL_OVERLAPCONTAINSRANGE
;
339 if ((start
<= range
->rl_start
) &&
340 ((end
== RL_INFINITY
) ||
341 ((range
->rl_end
!= RL_INFINITY
) && (end
>= range
->rl_end
)))) {
342 /* Case 3 (RL_OVERLAPISCONTAINED) */
343 return RL_OVERLAPISCONTAINED
;
346 if ((range
->rl_start
< start
) &&
347 ((range
->rl_end
>= start
) || (range
->rl_end
== RL_INFINITY
))) {
348 /* Case 4 (RL_OVERLAPSTARTSBEFORE) */
349 return RL_OVERLAPSTARTSBEFORE
;
352 if ((range
->rl_start
> start
) &&
353 (end
!= RL_INFINITY
) &&
354 ((range
->rl_end
> end
) || (range
->rl_end
== RL_INFINITY
))) {
355 /* Case 5 (RL_OVERLAPENDSAFTER) */
356 return RL_OVERLAPENDSAFTER
;
359 /* Control should never reach here... */
361 panic("hfs: rl_scan_from: unhandled overlap condition?!");
370 rl_collapse_forwards(struct rl_head
*rangelist
, struct rl_entry
*range
) {
371 struct rl_entry
*next_range
;
373 while ((next_range
= TAILQ_NEXT(range
, rl_link
))) {
374 if ((range
->rl_end
!= RL_INFINITY
) && (range
->rl_end
< next_range
->rl_start
- 1)) return;
376 /* Expand this range to include the next range: */
377 range
->rl_end
= next_range
->rl_end
;
379 /* Remove the now covered range from the list: */
380 TAILQ_REMOVE(rangelist
, next_range
, rl_link
);
381 FREE(next_range
, M_TEMP
);
384 rl_verify(rangelist
);
392 rl_collapse_backwards(struct rl_head
*rangelist
, struct rl_entry
*range
) {
393 struct rl_entry
*prev_range
;
395 while ((prev_range
= TAILQ_PREV(range
, rl_head
, rl_link
))) {
396 if (prev_range
->rl_end
< range
->rl_start
-1) {
398 rl_verify(rangelist
);
403 /* Expand this range to include the previous range: */
404 range
->rl_start
= prev_range
->rl_start
;
406 /* Remove the now covered range from the list: */
407 TAILQ_REMOVE(rangelist
, prev_range
, rl_link
);
408 FREE(prev_range
, M_TEMP
);
415 rl_collapse_neighbors(struct rl_head
*rangelist
, struct rl_entry
*range
)
417 rl_collapse_forwards(rangelist
, range
);
418 rl_collapse_backwards(rangelist
, range
);
421 #else /* not HFS - temp workaround until 4277828 is fixed */
422 /* stubs for exported routines that aren't present when we build kernel without HFS */
424 #include <sys/types.h>
426 void rl_add(off_t start
, off_t end
, void *rangelist
);
427 void rl_init(void *rangelist
);
428 void rl_remove(off_t start
, off_t end
, void *rangelist
);
429 int rl_scan(void *rangelist
, off_t start
, off_t end
, void **overlap
);
431 void rl_add(__unused off_t start
, __unused off_t end
, __unused
void *rangelist
)
436 void rl_init(__unused
void *rangelist
)
441 void rl_remove(__unused off_t start
, __unused off_t end
, __unused
void *rangelist
)
446 int rl_scan(__unused
void *rangelist
, __unused off_t start
, __unused off_t end
, __unused
void **overlap
)