]>
git.saurik.com Git - apple/hfs.git/blob - livefiles_hfs_plugin/lf_hfs_rangelist.c
1 /* Copyright © 2017-2018 Apple Inc. All rights reserved.
6 * Created by Yakov Ben Zaken on 19/03/2018.
9 #include "lf_hfs_rangelist.h"
10 #include "lf_hfs_vfsutils.h"
12 static void rl_collapse_forwards(struct rl_head
*rangelist
, struct rl_entry
*range
);
13 static void rl_collapse_backwards(struct rl_head
*rangelist
, struct rl_entry
*range
);
14 static void rl_collapse_neighbors(struct rl_head
*rangelist
, struct rl_entry
*range
);
17 * Walk the list of ranges for an entry to
18 * find an overlapping range (if any).
20 * NOTE: this returns only the FIRST overlapping range.
21 * There may be more than one.
23 static enum rl_overlaptype
24 rl_scan_from(struct rl_head
*rangelist __unused
,
27 struct rl_entry
**overlap
,
28 struct rl_entry
*range
)
33 enum rl_overlaptype ot
= rl_overlap(range
, start
, end
);
35 if (ot
!= RL_NOOVERLAP
|| range
->rl_start
> end
)
41 range
= TAILQ_NEXT(range
, rl_link
);
49 rl_init(struct rl_head
*rangelist
)
51 TAILQ_INIT(rangelist
);
55 rl_overlap(const struct rl_entry
*range
, off_t start
, off_t end
)
58 * OK, check for overlap
61 * 0) no overlap (RL_NOOVERLAP)
62 * 1) overlap == range (RL_MATCHINGOVERLAP)
63 * 2) overlap contains range (RL_OVERLAPCONTAINSRANGE)
64 * 3) range contains overlap (RL_OVERLAPISCONTAINED)
65 * 4) overlap starts before range (RL_OVERLAPSTARTSBEFORE)
66 * 5) overlap ends after range (RL_OVERLAPENDSAFTER)
68 if (start
> range
->rl_end
|| range
->rl_start
> end
)
70 /* Case 0 (RL_NOOVERLAP) */
74 if (range
->rl_start
== start
&& range
->rl_end
== end
)
76 /* Case 1 (RL_MATCHINGOVERLAP) */
77 return RL_MATCHINGOVERLAP
;
80 if (range
->rl_start
<= start
&& range
->rl_end
>= end
)
82 /* Case 2 (RL_OVERLAPCONTAINSRANGE) */
83 return RL_OVERLAPCONTAINSRANGE
;
86 if (start
<= range
->rl_start
&& end
>= range
->rl_end
)
88 /* Case 3 (RL_OVERLAPISCONTAINED) */
89 return RL_OVERLAPISCONTAINED
;
92 if (range
->rl_start
< start
&& range
->rl_end
< end
)
94 /* Case 4 (RL_OVERLAPSTARTSBEFORE) */
95 return RL_OVERLAPSTARTSBEFORE
;
98 /* Case 5 (RL_OVERLAPENDSAFTER) */
99 // range->rl_start > start && range->rl_end > end
100 return RL_OVERLAPENDSAFTER
;
104 * Remove a range from a range list.
106 * Generally, find the range (or an overlap to that range)
107 * and remove it (or shrink it), then wakeup anyone we can.
110 rl_remove(off_t start
, off_t end
, struct rl_head
*rangelist
)
112 struct rl_entry
*range
, *next_range
, *overlap
, *splitrange
;
115 if (TAILQ_EMPTY(rangelist
))
120 range
= TAILQ_FIRST(rangelist
);
121 while ((ovcase
= rl_scan_from(rangelist
, start
, end
, &overlap
, range
)))
126 case RL_MATCHINGOVERLAP
: /* 1: overlap == range */
127 TAILQ_REMOVE(rangelist
, overlap
, rl_link
);
131 case RL_OVERLAPCONTAINSRANGE
: /* 2: overlap contains range: split it */
132 if (overlap
->rl_start
== start
) {
133 overlap
->rl_start
= end
+ 1;
137 if (overlap
->rl_end
== end
) {
138 overlap
->rl_end
= start
- 1;
143 * Make a new range consisting of the last part of the encompassing range
145 splitrange
= hfs_malloc(sizeof(struct rl_entry
));
146 splitrange
->rl_start
= end
+ 1;
147 splitrange
->rl_end
= overlap
->rl_end
;
148 overlap
->rl_end
= start
- 1;
151 * Now link the new entry into the range list after the range from which it was split:
153 TAILQ_INSERT_AFTER(rangelist
, overlap
, splitrange
, rl_link
);
156 case RL_OVERLAPISCONTAINED
: /* 3: range contains overlap */
157 /* Check before discarding overlap entry */
158 next_range
= TAILQ_NEXT(overlap
, rl_link
);
159 TAILQ_REMOVE(rangelist
, overlap
, rl_link
);
168 case RL_OVERLAPSTARTSBEFORE
: /* 4: overlap starts before range */
169 overlap
->rl_end
= start
- 1;
170 range
= TAILQ_NEXT(overlap
, rl_link
);
176 case RL_OVERLAPENDSAFTER
: /* 5: overlap ends after range */
177 overlap
->rl_start
= (end
== RL_INFINITY
? RL_INFINITY
: end
+ 1);
184 off_t
rl_len(const struct rl_entry
*range
)
186 return range
->rl_end
- range
->rl_start
+ 1;
189 void rl_remove_all(struct rl_head
*rangelist
)
191 struct rl_entry
*r
, *nextr
;
192 TAILQ_FOREACH_SAFE(r
, rangelist
, rl_link
, nextr
){
195 TAILQ_INIT(rangelist
);
199 * Add a range to the list
202 rl_add(off_t start
, off_t end
, struct rl_head
*rangelist
)
204 struct rl_entry
*range
;
205 struct rl_entry
*overlap
;
206 enum rl_overlaptype ovcase
;
211 LFHFS_LOG(LEVEL_ERROR
, "rl_add: end < start?!");
216 ovcase
= rl_scan(rangelist
, start
, end
, &overlap
);
221 * 1) overlap == range
222 * 2) overlap contains range
223 * 3) range contains overlap
224 * 4) overlap starts before range
225 * 5) overlap ends after range
228 case RL_NOOVERLAP
: /* 0: no overlap */
230 * overlap points to the entry we should insert before, or
231 * if NULL, we should insert at the end.
233 range
= hfs_mallocz(sizeof(*range
));
234 range
->rl_start
= start
;
237 /* Link in the new range: */
239 TAILQ_INSERT_BEFORE(overlap
, range
, rl_link
);
241 TAILQ_INSERT_TAIL(rangelist
, range
, rl_link
);
244 /* Check to see if any ranges can be combined (possibly including the immediately
245 preceding range entry)
247 rl_collapse_neighbors(rangelist
, range
);
250 case RL_MATCHINGOVERLAP
: /* 1: overlap == range */
251 case RL_OVERLAPCONTAINSRANGE
: /* 2: overlap contains range */
254 case RL_OVERLAPISCONTAINED
: /* 3: range contains overlap */
256 * Replace the overlap with the new, larger range:
258 overlap
->rl_start
= start
;
259 overlap
->rl_end
= end
;
260 rl_collapse_neighbors(rangelist
, overlap
);
263 case RL_OVERLAPSTARTSBEFORE
: /* 4: overlap starts before range */
265 * Expand the overlap area to cover the new range:
267 overlap
->rl_end
= end
;
268 rl_collapse_forwards(rangelist
, overlap
);
271 case RL_OVERLAPENDSAFTER
: /* 5: overlap ends after range */
273 * Expand the overlap area to cover the new range:
275 overlap
->rl_start
= start
;
276 rl_collapse_backwards(rangelist
, overlap
);
281 rl_verify(rangelist
);
286 * Scan a range list for an entry in a specified range (if any):
288 * NOTE: this returns only the FIRST overlapping range.
289 * There may be more than one.
293 rl_scan(struct rl_head
*rangelist
, off_t start
, off_t end
, struct rl_entry
**overlap
)
295 return rl_scan_from(rangelist
, start
, end
, overlap
, TAILQ_FIRST(rangelist
));
299 rl_collapse_forwards(struct rl_head
*rangelist
, struct rl_entry
*range
) {
300 struct rl_entry
*next_range
;
302 while ((next_range
= TAILQ_NEXT(range
, rl_link
))) {
303 if ((range
->rl_end
!= RL_INFINITY
) && (range
->rl_end
< next_range
->rl_start
- 1)) return;
305 /* Expand this range to include the next range: */
306 range
->rl_end
= next_range
->rl_end
;
308 /* Remove the now covered range from the list: */
309 TAILQ_REMOVE(rangelist
, next_range
, rl_link
);
310 hfs_free(next_range
);
313 rl_verify(rangelist
);
319 rl_collapse_backwards(struct rl_head
*rangelist
, struct rl_entry
*range
) {
320 struct rl_entry
*prev_range
;
322 while ((prev_range
= TAILQ_PREV(range
, rl_head
, rl_link
))) {
323 if (prev_range
->rl_end
< range
->rl_start
-1) {
325 rl_verify(rangelist
);
330 /* Expand this range to include the previous range: */
331 range
->rl_start
= prev_range
->rl_start
;
333 /* Remove the now covered range from the list: */
334 TAILQ_REMOVE(rangelist
, prev_range
, rl_link
);
335 hfs_free(prev_range
);
340 rl_collapse_neighbors(struct rl_head
*rangelist
, struct rl_entry
*range
)
342 rl_collapse_forwards(rangelist
, range
);
343 rl_collapse_backwards(rangelist
, range
);
347 * In the case where b is contained by a, we return the the largest part
348 * remaining. The result is stored in a.
350 void rl_subtract(struct rl_entry
*a
, const struct rl_entry
*b
)
352 switch (rl_overlap(b
, a
->rl_start
, a
->rl_end
)) {
353 case RL_MATCHINGOVERLAP
:
354 case RL_OVERLAPCONTAINSRANGE
:
355 a
->rl_end
= a
->rl_start
- 1;
357 case RL_OVERLAPISCONTAINED
:
358 // Keep the bigger part
359 if (b
->rl_start
- a
->rl_start
>= a
->rl_end
- b
->rl_end
) {
361 a
->rl_end
= b
->rl_start
- 1;
364 a
->rl_start
= b
->rl_end
+ 1;
367 case RL_OVERLAPSTARTSBEFORE
:
368 a
->rl_start
= b
->rl_end
+ 1;
370 case RL_OVERLAPENDSAFTER
:
371 a
->rl_end
= b
->rl_start
- 1;
378 struct rl_entry
rl_make(off_t start
, off_t end
)
380 return (struct rl_entry
){ .rl_start
= start
, .rl_end
= end
};