1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/* 2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright 2009 Nicolai Hähnle <nhaehnle@gmail.com> 3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a 5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the "Software"), 6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * to deal in the Software without restriction, including without limitation 7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * on the rights to use, copy, modify, merge, publish, distribute, sub 8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * license, and/or sell copies of the Software, and to permit persons to whom 9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the Software is furnished to do so, subject to the following conditions: 10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the next 12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * paragraph) shall be included in all copies or substantial portions of the 13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software. 14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, 19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * USE OR OTHER DEALINGS IN THE SOFTWARE. */ 22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#ifndef MEMORY_POOL_H 24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define MEMORY_POOL_H 25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct memory_block; 27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/** 29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Provides a pool of memory that can quickly be allocated from, at the 30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * cost of being unable to explicitly free one of the allocated blocks. 31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Instead, the entire pool can be freed at once. 32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The idea is to allow one to quickly allocate a flexible amount of 34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * memory during operations like shader compilation while avoiding 35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * reference counting headaches. 36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */ 37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct memory_pool { 38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unsigned char * head; 39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unsigned char * end; 40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unsigned int total_allocated; 41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct memory_block * blocks; 42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid memory_pool_init(struct memory_pool * pool); 46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid memory_pool_destroy(struct memory_pool * pool); 47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid * memory_pool_malloc(struct memory_pool * pool, unsigned int bytes); 48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/** 51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Generic helper for growing an array that has separate size/count 52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * and reserved counters to accomodate up to num new element. 53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * type * Array; 55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * unsigned int Size; 56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * unsigned int Reserved; 57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * memory_pool_array_reserve(pool, type, Array, Size, Reserved, k); 59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * assert(Size + k < Reserved); 60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * \note Size is not changed by this macro. 62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * \warning Array, Size, Reserved have to be lvalues and may be evaluated 64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * several times. 65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */ 66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define memory_pool_array_reserve(pool, type, array, size, reserved, num) do { \ 67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unsigned int _num = (num); \ 68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if ((size) + _num > (reserved)) { \ 69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unsigned int newreserve = (reserved) * 2; \ 70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org type * newarray; \ 71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (newreserve < _num) \ 72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org newreserve = 4 * _num; /* arbitrary heuristic */ \ 73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org newarray = memory_pool_malloc((pool), newreserve * sizeof(type)); \ 74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org memcpy(newarray, (array), (size) * sizeof(type)); \ 75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org (array) = newarray; \ 76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org (reserved) = newreserve; \ 77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } \ 78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} while(0) 79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif /* MEMORY_POOL_H */ 81