1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright 2013 Christoph Bumiller 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub 8bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom 9bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, 19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 20bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. */ 22bf215546Sopenharmony_ci 23bf215546Sopenharmony_ci#include "nine_helpers.h" 24bf215546Sopenharmony_ci 25bf215546Sopenharmony_cistatic struct nine_range * 26bf215546Sopenharmony_cinine_range_pool_more(struct nine_range_pool *pool) 27bf215546Sopenharmony_ci{ 28bf215546Sopenharmony_ci struct nine_range *r = MALLOC(64 * sizeof(struct nine_range)); 29bf215546Sopenharmony_ci int i; 30bf215546Sopenharmony_ci assert(!pool->free); 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci if (pool->num_slabs == pool->num_slabs_max) { 33bf215546Sopenharmony_ci unsigned p = pool->num_slabs_max; 34bf215546Sopenharmony_ci unsigned n = pool->num_slabs_max * 2; 35bf215546Sopenharmony_ci if (!n) 36bf215546Sopenharmony_ci n = 4; 37bf215546Sopenharmony_ci pool->slabs = REALLOC(pool->slabs, 38bf215546Sopenharmony_ci p * sizeof(struct nine_range *), 39bf215546Sopenharmony_ci n * sizeof(struct nine_range *)); 40bf215546Sopenharmony_ci pool->num_slabs_max = n; 41bf215546Sopenharmony_ci } 42bf215546Sopenharmony_ci pool->free = pool->slabs[pool->num_slabs++] = r; 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ci for (i = 0; i < 63; ++i, r = r->next) 45bf215546Sopenharmony_ci r->next = (struct nine_range *) 46bf215546Sopenharmony_ci ((uint8_t *)r + sizeof(struct nine_range)); 47bf215546Sopenharmony_ci r->next = NULL; 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_ci return pool->free; 50bf215546Sopenharmony_ci} 51bf215546Sopenharmony_ci 52bf215546Sopenharmony_cistatic inline struct nine_range * 53bf215546Sopenharmony_cinine_range_pool_get(struct nine_range_pool *pool, int16_t bgn, int16_t end) 54bf215546Sopenharmony_ci{ 55bf215546Sopenharmony_ci struct nine_range *r = pool->free; 56bf215546Sopenharmony_ci if (!r) 57bf215546Sopenharmony_ci r = nine_range_pool_more(pool); 58bf215546Sopenharmony_ci assert(r); 59bf215546Sopenharmony_ci pool->free = r->next; 60bf215546Sopenharmony_ci r->bgn = bgn; 61bf215546Sopenharmony_ci r->end = end; 62bf215546Sopenharmony_ci return r; 63bf215546Sopenharmony_ci} 64bf215546Sopenharmony_ci 65bf215546Sopenharmony_cistatic inline void 66bf215546Sopenharmony_cinine_ranges_coalesce(struct nine_range *r, struct nine_range_pool *pool) 67bf215546Sopenharmony_ci{ 68bf215546Sopenharmony_ci struct nine_range *n; 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_ci while (r->next && r->end >= r->next->bgn) { 71bf215546Sopenharmony_ci n = r->next->next; 72bf215546Sopenharmony_ci r->end = (r->end >= r->next->end) ? r->end : r->next->end; 73bf215546Sopenharmony_ci nine_range_pool_put(pool, r->next); 74bf215546Sopenharmony_ci r->next = n; 75bf215546Sopenharmony_ci } 76bf215546Sopenharmony_ci} 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_civoid 79bf215546Sopenharmony_cinine_ranges_insert(struct nine_range **head, int16_t bgn, int16_t end, 80bf215546Sopenharmony_ci struct nine_range_pool *pool) 81bf215546Sopenharmony_ci{ 82bf215546Sopenharmony_ci struct nine_range *r, **pn = head; 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_ci for (r = *head; r && bgn > r->end; pn = &r->next, r = r->next); 85bf215546Sopenharmony_ci 86bf215546Sopenharmony_ci if (!r || end < r->bgn) { 87bf215546Sopenharmony_ci *pn = nine_range_pool_get(pool, bgn, end); 88bf215546Sopenharmony_ci (*pn)->next = r; 89bf215546Sopenharmony_ci } else 90bf215546Sopenharmony_ci if (bgn < r->bgn) { 91bf215546Sopenharmony_ci r->bgn = bgn; 92bf215546Sopenharmony_ci if (end > r->end) 93bf215546Sopenharmony_ci r->end = end; 94bf215546Sopenharmony_ci nine_ranges_coalesce(r, pool); 95bf215546Sopenharmony_ci } else 96bf215546Sopenharmony_ci if (end > r->end) { 97bf215546Sopenharmony_ci r->end = end; 98bf215546Sopenharmony_ci nine_ranges_coalesce(r, pool); 99bf215546Sopenharmony_ci } 100bf215546Sopenharmony_ci} 101