2 * Copyright 2013 Christoph Bumiller
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23 #include "nine_helpers.h"
25 static struct nine_range
*
26 nine_range_pool_more(struct nine_range_pool
*pool
)
28 struct nine_range
*r
= MALLOC(64 * sizeof(struct nine_range
));
32 if (pool
->num_slabs
== pool
->num_slabs_max
) {
33 unsigned p
= pool
->num_slabs_max
;
34 unsigned n
= pool
->num_slabs_max
* 2;
37 pool
->slabs
= REALLOC(pool
->slabs
,
38 p
* sizeof(struct nine_range
*),
39 n
* sizeof(struct nine_range
*));
40 pool
->num_slabs_max
= n
;
42 pool
->free
= pool
->slabs
[pool
->num_slabs
++] = r
;
44 for (i
= 0; i
< 63; ++i
, r
= r
->next
)
45 r
->next
= (struct nine_range
*)
46 ((uint8_t *)r
+ sizeof(struct nine_range
));
52 static inline struct nine_range
*
53 nine_range_pool_get(struct nine_range_pool
*pool
, int16_t bgn
, int16_t end
)
55 struct nine_range
*r
= pool
->free
;
57 r
= nine_range_pool_more(pool
);
66 nine_ranges_coalesce(struct nine_range
*r
, struct nine_range_pool
*pool
)
70 while (r
->next
&& r
->end
>= r
->next
->bgn
) {
72 r
->end
= (r
->end
>= r
->next
->end
) ? r
->end
: r
->next
->end
;
73 nine_range_pool_put(pool
, r
->next
);
79 nine_ranges_insert(struct nine_range
**head
, int16_t bgn
, int16_t end
,
80 struct nine_range_pool
*pool
)
82 struct nine_range
*r
, **pn
= head
;
84 for (r
= *head
; r
&& bgn
> r
->end
; pn
= &r
->next
, r
= r
->next
);
86 if (!r
|| end
< r
->bgn
) {
87 *pn
= nine_range_pool_get(pool
, bgn
, end
);
94 nine_ranges_coalesce(r
, pool
);
98 nine_ranges_coalesce(r
, pool
);