Path: blob/21.2-virgl/src/gallium/frontends/nine/nine_helpers.c
4561 views
/*1* Copyright 2013 Christoph Bumiller2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* on the rights to use, copy, modify, merge, publish, distribute, sub7* license, and/or sell copies of the Software, and to permit persons to whom8* the Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL17* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,18* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR19* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE20* USE OR OTHER DEALINGS IN THE SOFTWARE. */2122#include "nine_helpers.h"2324static struct nine_range *25nine_range_pool_more(struct nine_range_pool *pool)26{27struct nine_range *r = MALLOC(64 * sizeof(struct nine_range));28int i;29assert(!pool->free);3031if (pool->num_slabs == pool->num_slabs_max) {32unsigned p = pool->num_slabs_max;33unsigned n = pool->num_slabs_max * 2;34if (!n)35n = 4;36pool->slabs = REALLOC(pool->slabs,37p * sizeof(struct nine_range *),38n * sizeof(struct nine_range *));39pool->num_slabs_max = n;40}41pool->free = pool->slabs[pool->num_slabs++] = r;4243for (i = 0; i < 63; ++i, r = r->next)44r->next = (struct nine_range *)45((uint8_t *)r + sizeof(struct nine_range));46r->next = NULL;4748return pool->free;49}5051static inline struct nine_range *52nine_range_pool_get(struct nine_range_pool *pool, int16_t bgn, int16_t end)53{54struct nine_range *r = pool->free;55if (!r)56r = nine_range_pool_more(pool);57assert(r);58pool->free = r->next;59r->bgn = bgn;60r->end = end;61return r;62}6364static inline void65nine_ranges_coalesce(struct nine_range *r, struct nine_range_pool *pool)66{67struct nine_range *n;6869while (r->next && r->end >= r->next->bgn) {70n = r->next->next;71r->end = (r->end >= r->next->end) ? r->end : r->next->end;72nine_range_pool_put(pool, r->next);73r->next = n;74}75}7677void78nine_ranges_insert(struct nine_range **head, int16_t bgn, int16_t end,79struct nine_range_pool *pool)80{81struct nine_range *r, **pn = head;8283for (r = *head; r && bgn > r->end; pn = &r->next, r = r->next);8485if (!r || end < r->bgn) {86*pn = nine_range_pool_get(pool, bgn, end);87(*pn)->next = r;88} else89if (bgn < r->bgn) {90r->bgn = bgn;91if (end > r->end)92r->end = end;93nine_ranges_coalesce(r, pool);94} else95if (end > r->end) {96r->end = end;97nine_ranges_coalesce(r, pool);98}99}100101102