1848b8605Smrg/*
2848b8605Smrg * Copyright 2007 Nouveau Project
3848b8605Smrg *
4848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5848b8605Smrg * copy of this software and associated documentation files (the "Software"),
6848b8605Smrg * to deal in the Software without restriction, including without limitation
7848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
9848b8605Smrg * Software is furnished to do so, subject to the following conditions:
10848b8605Smrg *
11848b8605Smrg * The above copyright notice and this permission notice shall be included in
12848b8605Smrg * all copies or substantial portions of the Software.
13848b8605Smrg *
14848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18848b8605Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19848b8605Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20848b8605Smrg * OTHER DEALINGS IN THE SOFTWARE.
21848b8605Smrg */
22848b8605Smrg
23848b8605Smrg#ifndef __NOUVEAU_HEAP_H__
24848b8605Smrg#define __NOUVEAU_HEAP_H__
25848b8605Smrg
26b8e80941Smrg/* This datastructure represents a memory allocation heap. Fundamentally, this
27b8e80941Smrg * is a doubly-linked list with a few properties, and a usage convention.
28b8e80941Smrg *
29b8e80941Smrg * On initial allocation, there is a single node with the full size that's
30b8e80941Smrg * marked as not in-use. As allocations are made, blocks are taken off the end
31b8e80941Smrg * of that first node, and inserted right after it. If the first node doesn't
32b8e80941Smrg * have enough free space, we look for free space down in the rest of the
33b8e80941Smrg * list. This can happen if an allocation is made and then freed.
34b8e80941Smrg *
35b8e80941Smrg * The first node will remain with in_use == 0 even if the whole heap is
36b8e80941Smrg * exhausted. Another invariant is that there will never be two sequential
37b8e80941Smrg * in_use == 0 nodes. If a node is freed and it has one (or both) adjacent
38b8e80941Smrg * free nodes, they are merged into one, and the relevant heap entries are
39b8e80941Smrg * freed.
40b8e80941Smrg *
41b8e80941Smrg * The pattern to free the whole heap is to start with the first node and then
42b8e80941Smrg * just free the "next" node, until there is no next node. This should assure
43b8e80941Smrg * that at the end the first (and only) node is not in use and contains the
44b8e80941Smrg * full size of the heap.
45b8e80941Smrg */
46848b8605Smrgstruct nouveau_heap {
47b8e80941Smrg   struct nouveau_heap *prev;
48b8e80941Smrg   struct nouveau_heap *next;
49848b8605Smrg
50b8e80941Smrg   void *priv;
51848b8605Smrg
52b8e80941Smrg   unsigned start;
53b8e80941Smrg   unsigned size;
54848b8605Smrg
55b8e80941Smrg   int in_use;
56848b8605Smrg};
57848b8605Smrg
58848b8605Smrgint
59848b8605Smrgnouveau_heap_init(struct nouveau_heap **heap, unsigned start,
60848b8605Smrg                  unsigned size);
61848b8605Smrg
62848b8605Smrgvoid
63848b8605Smrgnouveau_heap_destroy(struct nouveau_heap **heap);
64848b8605Smrg
65848b8605Smrgint
66848b8605Smrgnouveau_heap_alloc(struct nouveau_heap *heap, unsigned size, void *priv,
67848b8605Smrg                   struct nouveau_heap **);
68848b8605Smrg
69848b8605Smrgvoid
70848b8605Smrgnouveau_heap_free(struct nouveau_heap **);
71848b8605Smrg
72848b8605Smrg#endif
73