blob: 79d8719dbba881c8310218921c49276ab732df30 [file] [log] [blame]
Eric Anholtc4857422008-06-03 10:20:49 -07001/*
2 * GLX Hardware Device Driver common code
3 * Copyright (C) 1999 Wittawat Yamwong
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * WITTAWAT YAMWONG, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
21 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 */
24
25#include <stdlib.h>
26#include <assert.h>
27
28#include "xf86drm.h"
Emil Velikov42465fe2015-04-05 15:51:59 +010029#include "libdrm_macros.h"
Eric Anholtc4857422008-06-03 10:20:49 -070030#include "mm.h"
31
Emil Velikov5966d372015-03-31 21:12:14 +010032drm_private void mmDumpMemInfo(const struct mem_block *heap)
Eric Anholtc4857422008-06-03 10:20:49 -070033{
Eric Anholtd70d6052009-10-06 12:40:42 -070034 drmMsg("Memory heap %p:\n", (void *)heap);
35 if (heap == 0) {
36 drmMsg(" heap == 0\n");
37 } else {
38 const struct mem_block *p;
Eric Anholtc4857422008-06-03 10:20:49 -070039
Eric Anholtd70d6052009-10-06 12:40:42 -070040 for (p = heap->next; p != heap; p = p->next) {
41 drmMsg(" Offset:%08x, Size:%08x, %c%c\n", p->ofs,
42 p->size, p->free ? 'F' : '.',
43 p->reserved ? 'R' : '.');
44 }
Eric Anholtc4857422008-06-03 10:20:49 -070045
Eric Anholtd70d6052009-10-06 12:40:42 -070046 drmMsg("\nFree list:\n");
Eric Anholtc4857422008-06-03 10:20:49 -070047
Eric Anholtd70d6052009-10-06 12:40:42 -070048 for (p = heap->next_free; p != heap; p = p->next_free) {
49 drmMsg(" FREE Offset:%08x, Size:%08x, %c%c\n", p->ofs,
50 p->size, p->free ? 'F' : '.',
51 p->reserved ? 'R' : '.');
52 }
Eric Anholtc4857422008-06-03 10:20:49 -070053
Eric Anholtd70d6052009-10-06 12:40:42 -070054 }
55 drmMsg("End of memory blocks\n");
Eric Anholtc4857422008-06-03 10:20:49 -070056}
57
Emil Velikov5966d372015-03-31 21:12:14 +010058drm_private struct mem_block *mmInit(int ofs, int size)
Eric Anholtc4857422008-06-03 10:20:49 -070059{
Eric Anholtd70d6052009-10-06 12:40:42 -070060 struct mem_block *heap, *block;
Eric Anholtc4857422008-06-03 10:20:49 -070061
Eric Anholtd70d6052009-10-06 12:40:42 -070062 if (size <= 0)
63 return NULL;
Eric Anholtc4857422008-06-03 10:20:49 -070064
Eric Anholtd70d6052009-10-06 12:40:42 -070065 heap = (struct mem_block *)calloc(1, sizeof(struct mem_block));
66 if (!heap)
67 return NULL;
Eric Anholtc4857422008-06-03 10:20:49 -070068
Eric Anholtd70d6052009-10-06 12:40:42 -070069 block = (struct mem_block *)calloc(1, sizeof(struct mem_block));
70 if (!block) {
71 free(heap);
72 return NULL;
73 }
Eric Anholtc4857422008-06-03 10:20:49 -070074
Eric Anholtd70d6052009-10-06 12:40:42 -070075 heap->next = block;
76 heap->prev = block;
77 heap->next_free = block;
78 heap->prev_free = block;
Eric Anholtc4857422008-06-03 10:20:49 -070079
Eric Anholtd70d6052009-10-06 12:40:42 -070080 block->heap = heap;
81 block->next = heap;
82 block->prev = heap;
83 block->next_free = heap;
84 block->prev_free = heap;
85
86 block->ofs = ofs;
87 block->size = size;
88 block->free = 1;
89
90 return heap;
Eric Anholtc4857422008-06-03 10:20:49 -070091}
92
Eric Anholtd70d6052009-10-06 12:40:42 -070093static struct mem_block *SliceBlock(struct mem_block *p,
94 int startofs, int size,
95 int reserved, int alignment)
Eric Anholtc4857422008-06-03 10:20:49 -070096{
Eric Anholtd70d6052009-10-06 12:40:42 -070097 struct mem_block *newblock;
Eric Anholtc4857422008-06-03 10:20:49 -070098
Eric Anholtd70d6052009-10-06 12:40:42 -070099 /* break left [p, newblock, p->next], then p = newblock */
100 if (startofs > p->ofs) {
101 newblock =
102 (struct mem_block *)calloc(1, sizeof(struct mem_block));
103 if (!newblock)
104 return NULL;
105 newblock->ofs = startofs;
106 newblock->size = p->size - (startofs - p->ofs);
107 newblock->free = 1;
108 newblock->heap = p->heap;
Eric Anholtc4857422008-06-03 10:20:49 -0700109
Eric Anholtd70d6052009-10-06 12:40:42 -0700110 newblock->next = p->next;
111 newblock->prev = p;
112 p->next->prev = newblock;
113 p->next = newblock;
Eric Anholtc4857422008-06-03 10:20:49 -0700114
Eric Anholtd70d6052009-10-06 12:40:42 -0700115 newblock->next_free = p->next_free;
116 newblock->prev_free = p;
117 p->next_free->prev_free = newblock;
118 p->next_free = newblock;
Eric Anholtc4857422008-06-03 10:20:49 -0700119
Eric Anholtd70d6052009-10-06 12:40:42 -0700120 p->size -= newblock->size;
121 p = newblock;
122 }
Eric Anholtc4857422008-06-03 10:20:49 -0700123
Eric Anholtd70d6052009-10-06 12:40:42 -0700124 /* break right, also [p, newblock, p->next] */
125 if (size < p->size) {
126 newblock =
127 (struct mem_block *)calloc(1, sizeof(struct mem_block));
128 if (!newblock)
129 return NULL;
130 newblock->ofs = startofs + size;
131 newblock->size = p->size - size;
132 newblock->free = 1;
133 newblock->heap = p->heap;
Eric Anholtc4857422008-06-03 10:20:49 -0700134
Eric Anholtd70d6052009-10-06 12:40:42 -0700135 newblock->next = p->next;
136 newblock->prev = p;
137 p->next->prev = newblock;
138 p->next = newblock;
Eric Anholtc4857422008-06-03 10:20:49 -0700139
Eric Anholtd70d6052009-10-06 12:40:42 -0700140 newblock->next_free = p->next_free;
141 newblock->prev_free = p;
142 p->next_free->prev_free = newblock;
143 p->next_free = newblock;
Eric Anholtc4857422008-06-03 10:20:49 -0700144
Eric Anholtd70d6052009-10-06 12:40:42 -0700145 p->size = size;
146 }
Eric Anholtc4857422008-06-03 10:20:49 -0700147
Eric Anholtd70d6052009-10-06 12:40:42 -0700148 /* p = middle block */
149 p->free = 0;
Eric Anholtc4857422008-06-03 10:20:49 -0700150
Eric Anholtd70d6052009-10-06 12:40:42 -0700151 /* Remove p from the free list:
152 */
153 p->next_free->prev_free = p->prev_free;
154 p->prev_free->next_free = p->next_free;
Eric Anholtc4857422008-06-03 10:20:49 -0700155
Eric Anholtd70d6052009-10-06 12:40:42 -0700156 p->next_free = 0;
157 p->prev_free = 0;
158
159 p->reserved = reserved;
160 return p;
Eric Anholtc4857422008-06-03 10:20:49 -0700161}
162
Emil Velikov5966d372015-03-31 21:12:14 +0100163drm_private struct mem_block *mmAllocMem(struct mem_block *heap, int size,
164 int align2, int startSearch)
Eric Anholtc4857422008-06-03 10:20:49 -0700165{
Eric Anholtd70d6052009-10-06 12:40:42 -0700166 struct mem_block *p;
167 const int mask = (1 << align2) - 1;
168 int startofs = 0;
169 int endofs;
Eric Anholtc4857422008-06-03 10:20:49 -0700170
Eric Anholtd70d6052009-10-06 12:40:42 -0700171 if (!heap || align2 < 0 || size <= 0)
172 return NULL;
Eric Anholtc4857422008-06-03 10:20:49 -0700173
Eric Anholtd70d6052009-10-06 12:40:42 -0700174 for (p = heap->next_free; p != heap; p = p->next_free) {
175 assert(p->free);
Eric Anholtc4857422008-06-03 10:20:49 -0700176
Eric Anholtd70d6052009-10-06 12:40:42 -0700177 startofs = (p->ofs + mask) & ~mask;
178 if (startofs < startSearch) {
179 startofs = startSearch;
180 }
181 endofs = startofs + size;
182 if (endofs <= (p->ofs + p->size))
183 break;
184 }
Eric Anholtc4857422008-06-03 10:20:49 -0700185
Eric Anholtd70d6052009-10-06 12:40:42 -0700186 if (p == heap)
187 return NULL;
Eric Anholtc4857422008-06-03 10:20:49 -0700188
Eric Anholtd70d6052009-10-06 12:40:42 -0700189 assert(p->free);
190 p = SliceBlock(p, startofs, size, 0, mask + 1);
Eric Anholtc4857422008-06-03 10:20:49 -0700191
Eric Anholtd70d6052009-10-06 12:40:42 -0700192 return p;
Eric Anholtc4857422008-06-03 10:20:49 -0700193}
194
Eric Anholtd70d6052009-10-06 12:40:42 -0700195static int Join2Blocks(struct mem_block *p)
Eric Anholtc4857422008-06-03 10:20:49 -0700196{
Eric Anholtd70d6052009-10-06 12:40:42 -0700197 /* XXX there should be some assertions here */
Eric Anholtc4857422008-06-03 10:20:49 -0700198
Eric Anholtd70d6052009-10-06 12:40:42 -0700199 /* NOTE: heap->free == 0 */
Eric Anholtc4857422008-06-03 10:20:49 -0700200
Eric Anholtd70d6052009-10-06 12:40:42 -0700201 if (p->free && p->next->free) {
202 struct mem_block *q = p->next;
Eric Anholtc4857422008-06-03 10:20:49 -0700203
Eric Anholtd70d6052009-10-06 12:40:42 -0700204 assert(p->ofs + p->size == q->ofs);
205 p->size += q->size;
Eric Anholtc4857422008-06-03 10:20:49 -0700206
Eric Anholtd70d6052009-10-06 12:40:42 -0700207 p->next = q->next;
208 q->next->prev = p;
Eric Anholtc4857422008-06-03 10:20:49 -0700209
Eric Anholtd70d6052009-10-06 12:40:42 -0700210 q->next_free->prev_free = q->prev_free;
211 q->prev_free->next_free = q->next_free;
212
213 free(q);
214 return 1;
215 }
216 return 0;
Eric Anholtc4857422008-06-03 10:20:49 -0700217}
218
Emil Velikov5966d372015-03-31 21:12:14 +0100219drm_private int mmFreeMem(struct mem_block *b)
Eric Anholtc4857422008-06-03 10:20:49 -0700220{
Eric Anholtd70d6052009-10-06 12:40:42 -0700221 if (!b)
222 return 0;
Eric Anholtc4857422008-06-03 10:20:49 -0700223
Eric Anholtd70d6052009-10-06 12:40:42 -0700224 if (b->free) {
225 drmMsg("block already free\n");
226 return -1;
227 }
228 if (b->reserved) {
229 drmMsg("block is reserved\n");
230 return -1;
231 }
Eric Anholtc4857422008-06-03 10:20:49 -0700232
Eric Anholtd70d6052009-10-06 12:40:42 -0700233 b->free = 1;
234 b->next_free = b->heap->next_free;
235 b->prev_free = b->heap;
236 b->next_free->prev_free = b;
237 b->prev_free->next_free = b;
Eric Anholtc4857422008-06-03 10:20:49 -0700238
Eric Anholtd70d6052009-10-06 12:40:42 -0700239 Join2Blocks(b);
240 if (b->prev != b->heap)
241 Join2Blocks(b->prev);
Eric Anholtc4857422008-06-03 10:20:49 -0700242
Eric Anholtd70d6052009-10-06 12:40:42 -0700243 return 0;
Eric Anholtc4857422008-06-03 10:20:49 -0700244}
245
Emil Velikov5966d372015-03-31 21:12:14 +0100246drm_private void mmDestroy(struct mem_block *heap)
Eric Anholtc4857422008-06-03 10:20:49 -0700247{
Eric Anholtd70d6052009-10-06 12:40:42 -0700248 struct mem_block *p;
Eric Anholtc4857422008-06-03 10:20:49 -0700249
Eric Anholtd70d6052009-10-06 12:40:42 -0700250 if (!heap)
251 return;
Eric Anholtc4857422008-06-03 10:20:49 -0700252
Eric Anholtd70d6052009-10-06 12:40:42 -0700253 for (p = heap->next; p != heap;) {
254 struct mem_block *next = p->next;
255 free(p);
256 p = next;
257 }
Eric Anholtc4857422008-06-03 10:20:49 -0700258
Eric Anholtd70d6052009-10-06 12:40:42 -0700259 free(heap);
Eric Anholtc4857422008-06-03 10:20:49 -0700260}