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#include <stdlib.h>
24bf215546Sopenharmony_ci#include <errno.h>
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "nouveau_heap.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ciint
29bf215546Sopenharmony_cinouveau_heap_init(struct nouveau_heap **heap,
30bf215546Sopenharmony_ci                  unsigned start, unsigned size)
31bf215546Sopenharmony_ci{
32bf215546Sopenharmony_ci   struct nouveau_heap *r;
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci   r = calloc(1, sizeof(struct nouveau_heap));
35bf215546Sopenharmony_ci   if (!r)
36bf215546Sopenharmony_ci      return 1;
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci   r->start = start;
39bf215546Sopenharmony_ci   r->size  = size;
40bf215546Sopenharmony_ci   *heap = r;
41bf215546Sopenharmony_ci   return 0;
42bf215546Sopenharmony_ci}
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_civoid
45bf215546Sopenharmony_cinouveau_heap_destroy(struct nouveau_heap **heap)
46bf215546Sopenharmony_ci{
47bf215546Sopenharmony_ci   if (!*heap)
48bf215546Sopenharmony_ci      return;
49bf215546Sopenharmony_ci   free(*heap);
50bf215546Sopenharmony_ci   *heap = NULL;
51bf215546Sopenharmony_ci}
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ciint
54bf215546Sopenharmony_cinouveau_heap_alloc(struct nouveau_heap *heap, unsigned size, void *priv,
55bf215546Sopenharmony_ci                   struct nouveau_heap **res)
56bf215546Sopenharmony_ci{
57bf215546Sopenharmony_ci   struct nouveau_heap *r;
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci   if (!heap || !size || !res || *res)
60bf215546Sopenharmony_ci      return 1;
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci   while (heap) {
63bf215546Sopenharmony_ci      if (!heap->in_use && heap->size >= size) {
64bf215546Sopenharmony_ci         r = calloc(1, sizeof(struct nouveau_heap));
65bf215546Sopenharmony_ci         if (!r)
66bf215546Sopenharmony_ci            return 1;
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci         r->start  = (heap->start + heap->size) - size;
69bf215546Sopenharmony_ci         r->size   = size;
70bf215546Sopenharmony_ci         r->in_use = 1;
71bf215546Sopenharmony_ci         r->priv   = priv;
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci         heap->size -= size;
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci         r->next = heap->next;
76bf215546Sopenharmony_ci         if (heap->next)
77bf215546Sopenharmony_ci            heap->next->prev = r;
78bf215546Sopenharmony_ci         r->prev = heap;
79bf215546Sopenharmony_ci         heap->next = r;
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci         *res = r;
82bf215546Sopenharmony_ci         return 0;
83bf215546Sopenharmony_ci      }
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci      heap = heap->next;
86bf215546Sopenharmony_ci   }
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   return 1;
89bf215546Sopenharmony_ci}
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_civoid
92bf215546Sopenharmony_cinouveau_heap_free(struct nouveau_heap **res)
93bf215546Sopenharmony_ci{
94bf215546Sopenharmony_ci   struct nouveau_heap *r;
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   if (!res || !*res)
97bf215546Sopenharmony_ci      return;
98bf215546Sopenharmony_ci   r = *res;
99bf215546Sopenharmony_ci   *res = NULL;
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   r->in_use = 0;
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci   if (r->next && !r->next->in_use) {
104bf215546Sopenharmony_ci      struct nouveau_heap *new = r->next;
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci      new->prev = r->prev;
107bf215546Sopenharmony_ci      if (r->prev)
108bf215546Sopenharmony_ci         r->prev->next = new;
109bf215546Sopenharmony_ci      new->size += r->size;
110bf215546Sopenharmony_ci      new->start = r->start;
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci      free(r);
113bf215546Sopenharmony_ci      r = new;
114bf215546Sopenharmony_ci   }
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   if (r->prev && !r->prev->in_use) {
117bf215546Sopenharmony_ci      r->prev->next = r->next;
118bf215546Sopenharmony_ci      if (r->next)
119bf215546Sopenharmony_ci         r->next->prev = r->prev;
120bf215546Sopenharmony_ci      r->prev->size += r->size;
121bf215546Sopenharmony_ci      free(r);
122bf215546Sopenharmony_ci   }
123bf215546Sopenharmony_ci}
124