1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright 2007 Nouveau Project 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 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 12bf215546Sopenharmony_ci * all copies or substantial portions of the Software. 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 18bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 21bf215546Sopenharmony_ci */ 22bf215546Sopenharmony_ci 23bf215546Sopenharmony_ci#ifndef __NOUVEAU_HEAP_H__ 24bf215546Sopenharmony_ci#define __NOUVEAU_HEAP_H__ 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_ci/* This datastructure represents a memory allocation heap. Fundamentally, this 27bf215546Sopenharmony_ci * is a doubly-linked list with a few properties, and a usage convention. 28bf215546Sopenharmony_ci * 29bf215546Sopenharmony_ci * On initial allocation, there is a single node with the full size that's 30bf215546Sopenharmony_ci * marked as not in-use. As allocations are made, blocks are taken off the end 31bf215546Sopenharmony_ci * of that first node, and inserted right after it. If the first node doesn't 32bf215546Sopenharmony_ci * have enough free space, we look for free space down in the rest of the 33bf215546Sopenharmony_ci * list. This can happen if an allocation is made and then freed. 34bf215546Sopenharmony_ci * 35bf215546Sopenharmony_ci * The first node will remain with in_use == 0 even if the whole heap is 36bf215546Sopenharmony_ci * exhausted. Another invariant is that there will never be two sequential 37bf215546Sopenharmony_ci * in_use == 0 nodes. If a node is freed and it has one (or both) adjacent 38bf215546Sopenharmony_ci * free nodes, they are merged into one, and the relevant heap entries are 39bf215546Sopenharmony_ci * freed. 40bf215546Sopenharmony_ci * 41bf215546Sopenharmony_ci * The pattern to free the whole heap is to start with the first node and then 42bf215546Sopenharmony_ci * just free the "next" node, until there is no next node. This should assure 43bf215546Sopenharmony_ci * that at the end the first (and only) node is not in use and contains the 44bf215546Sopenharmony_ci * full size of the heap. 45bf215546Sopenharmony_ci */ 46bf215546Sopenharmony_cistruct nouveau_heap { 47bf215546Sopenharmony_ci struct nouveau_heap *prev; 48bf215546Sopenharmony_ci struct nouveau_heap *next; 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_ci void *priv; 51bf215546Sopenharmony_ci 52bf215546Sopenharmony_ci unsigned start; 53bf215546Sopenharmony_ci unsigned size; 54bf215546Sopenharmony_ci 55bf215546Sopenharmony_ci int in_use; 56bf215546Sopenharmony_ci}; 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_ciint 59bf215546Sopenharmony_cinouveau_heap_init(struct nouveau_heap **heap, unsigned start, 60bf215546Sopenharmony_ci unsigned size); 61bf215546Sopenharmony_ci 62bf215546Sopenharmony_civoid 63bf215546Sopenharmony_cinouveau_heap_destroy(struct nouveau_heap **heap); 64bf215546Sopenharmony_ci 65bf215546Sopenharmony_ciint 66bf215546Sopenharmony_cinouveau_heap_alloc(struct nouveau_heap *heap, unsigned size, void *priv, 67bf215546Sopenharmony_ci struct nouveau_heap **); 68bf215546Sopenharmony_ci 69bf215546Sopenharmony_civoid 70bf215546Sopenharmony_cinouveau_heap_free(struct nouveau_heap **); 71bf215546Sopenharmony_ci 72bf215546Sopenharmony_ci#endif 73