Path: blob/21.2-virgl/src/gallium/drivers/r300/compiler/memory_pool.h
4574 views
/*1* Copyright 2009 Nicolai Hähnle <[email protected]>2*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#ifndef MEMORY_POOL_H23#define MEMORY_POOL_H2425struct memory_block;2627/**28* Provides a pool of memory that can quickly be allocated from, at the29* cost of being unable to explicitly free one of the allocated blocks.30* Instead, the entire pool can be freed at once.31*32* The idea is to allow one to quickly allocate a flexible amount of33* memory during operations like shader compilation while avoiding34* reference counting headaches.35*/36struct memory_pool {37unsigned char * head;38unsigned char * end;39unsigned int total_allocated;40struct memory_block * blocks;41};424344void memory_pool_init(struct memory_pool * pool);45void memory_pool_destroy(struct memory_pool * pool);46void * memory_pool_malloc(struct memory_pool * pool, unsigned int bytes);474849/**50* Generic helper for growing an array that has separate size/count51* and reserved counters to accommodate up to num new element.52*53* type * Array;54* unsigned int Size;55* unsigned int Reserved;56*57* memory_pool_array_reserve(pool, type, Array, Size, Reserved, k);58* assert(Size + k < Reserved);59*60* \note Size is not changed by this macro.61*62* \warning Array, Size, Reserved have to be lvalues and may be evaluated63* several times.64*/65#define memory_pool_array_reserve(pool, type, array, size, reserved, num) do { \66unsigned int _num = (num); \67if ((size) + _num > (reserved)) { \68unsigned int newreserve = (reserved) * 2; \69type * newarray; \70if (newreserve < _num) \71newreserve = 4 * _num; /* arbitrary heuristic */ \72newarray = memory_pool_malloc((pool), newreserve * sizeof(type)); \73memcpy(newarray, (array), (size) * sizeof(type)); \74(array) = newarray; \75(reserved) = newreserve; \76} \77} while(0)7879#endif /* MEMORY_POOL_H */808182