xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/nouveau/nouveau_heap.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2007 Nouveau Project
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include <stdlib.h>
24 #include <errno.h>
25 
26 #include "nouveau_heap.h"
27 
28 int
nouveau_heap_init(struct nouveau_heap ** heap,unsigned start,unsigned size)29 nouveau_heap_init(struct nouveau_heap **heap,
30                   unsigned start, unsigned size)
31 {
32    struct nouveau_heap *r;
33 
34    r = calloc(1, sizeof(struct nouveau_heap));
35    if (!r)
36       return 1;
37 
38    r->start = start;
39    r->size  = size;
40    *heap = r;
41    return 0;
42 }
43 
44 void
nouveau_heap_destroy(struct nouveau_heap ** heap)45 nouveau_heap_destroy(struct nouveau_heap **heap)
46 {
47    struct nouveau_heap *current = *heap;
48 
49    while (current) {
50       struct nouveau_heap *const next = current->next;
51       free(current);
52       current = next;
53    }
54 
55    *heap = NULL;
56 }
57 
58 int
nouveau_heap_alloc(struct nouveau_heap * heap,unsigned size,void * priv,struct nouveau_heap ** res)59 nouveau_heap_alloc(struct nouveau_heap *heap, unsigned size, void *priv,
60                    struct nouveau_heap **res)
61 {
62    struct nouveau_heap *r;
63 
64    if (!heap || !size || !res || *res)
65       return 1;
66 
67    while (heap) {
68       if (!heap->in_use && heap->size >= size) {
69          r = calloc(1, sizeof(struct nouveau_heap));
70          if (!r)
71             return 1;
72 
73          r->start  = (heap->start + heap->size) - size;
74          r->size   = size;
75          r->in_use = 1;
76          r->priv   = priv;
77 
78          heap->size -= size;
79 
80          r->next = heap->next;
81          if (heap->next)
82             heap->next->prev = r;
83          r->prev = heap;
84          heap->next = r;
85 
86          *res = r;
87          return 0;
88       }
89 
90       heap = heap->next;
91    }
92 
93    return 1;
94 }
95 
96 void
nouveau_heap_free(struct nouveau_heap ** res)97 nouveau_heap_free(struct nouveau_heap **res)
98 {
99    struct nouveau_heap *r;
100 
101    if (!res || !*res)
102       return;
103    r = *res;
104    *res = NULL;
105 
106    r->in_use = 0;
107 
108    if (r->next && !r->next->in_use) {
109       struct nouveau_heap *new = r->next;
110 
111       new->prev = r->prev;
112       if (r->prev)
113          r->prev->next = new;
114       new->size += r->size;
115       new->start = r->start;
116 
117       free(r);
118       r = new;
119    }
120 
121    if (r->prev && !r->prev->in_use) {
122       r->prev->next = r->next;
123       if (r->next)
124          r->next->prev = r->prev;
125       r->prev->size += r->size;
126       free(r);
127    }
128 }
129