blob: 106974525b5c03c96321b34b5c06e5ebac5121f1 [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"
29#include "mm.h"
30
Eric Anholtd70d6052009-10-06 12:40:42 -070031void mmDumpMemInfo(const struct mem_block *heap)
Eric Anholtc4857422008-06-03 10:20:49 -070032{
Eric Anholtd70d6052009-10-06 12:40:42 -070033 drmMsg("Memory heap %p:\n", (void *)heap);
34 if (heap == 0) {
35 drmMsg(" heap == 0\n");
36 } else {
37 const struct mem_block *p;
Eric Anholtc4857422008-06-03 10:20:49 -070038
Eric Anholtd70d6052009-10-06 12:40:42 -070039 for (p = heap->next; p != heap; p = p->next) {
40 drmMsg(" Offset:%08x, Size:%08x, %c%c\n", p->ofs,
41 p->size, p->free ? 'F' : '.',
42 p->reserved ? 'R' : '.');
43 }
Eric Anholtc4857422008-06-03 10:20:49 -070044
Eric Anholtd70d6052009-10-06 12:40:42 -070045 drmMsg("\nFree list:\n");
Eric Anholtc4857422008-06-03 10:20:49 -070046
Eric Anholtd70d6052009-10-06 12:40:42 -070047 for (p = heap->next_free; p != heap; p = p->next_free) {
48 drmMsg(" FREE Offset:%08x, Size:%08x, %c%c\n", p->ofs,
49 p->size, p->free ? 'F' : '.',
50 p->reserved ? 'R' : '.');
51 }
Eric Anholtc4857422008-06-03 10:20:49 -070052
Eric Anholtd70d6052009-10-06 12:40:42 -070053 }
54 drmMsg("End of memory blocks\n");
Eric Anholtc4857422008-06-03 10:20:49 -070055}
56
Eric Anholtd70d6052009-10-06 12:40:42 -070057struct mem_block *mmInit(int ofs, int size)
Eric Anholtc4857422008-06-03 10:20:49 -070058{
Eric Anholtd70d6052009-10-06 12:40:42 -070059 struct mem_block *heap, *block;
Eric Anholtc4857422008-06-03 10:20:49 -070060
Eric Anholtd70d6052009-10-06 12:40:42 -070061 if (size <= 0)
62 return NULL;
Eric Anholtc4857422008-06-03 10:20:49 -070063
Eric Anholtd70d6052009-10-06 12:40:42 -070064 heap = (struct mem_block *)calloc(1, sizeof(struct mem_block));
65 if (!heap)
66 return NULL;
Eric Anholtc4857422008-06-03 10:20:49 -070067
Eric Anholtd70d6052009-10-06 12:40:42 -070068 block = (struct mem_block *)calloc(1, sizeof(struct mem_block));
69 if (!block) {
70 free(heap);
71 return NULL;
72 }
Eric Anholtc4857422008-06-03 10:20:49 -070073
Eric Anholtd70d6052009-10-06 12:40:42 -070074 heap->next = block;
75 heap->prev = block;
76 heap->next_free = block;
77 heap->prev_free = block;
Eric Anholtc4857422008-06-03 10:20:49 -070078
Eric Anholtd70d6052009-10-06 12:40:42 -070079 block->heap = heap;
80 block->next = heap;
81 block->prev = heap;
82 block->next_free = heap;
83 block->prev_free = heap;
84
85 block->ofs = ofs;
86 block->size = size;
87 block->free = 1;
88
89 return heap;
Eric Anholtc4857422008-06-03 10:20:49 -070090}
91
Eric Anholtd70d6052009-10-06 12:40:42 -070092static struct mem_block *SliceBlock(struct mem_block *p,
93 int startofs, int size,
94 int reserved, int alignment)
Eric Anholtc4857422008-06-03 10:20:49 -070095{
Eric Anholtd70d6052009-10-06 12:40:42 -070096 struct mem_block *newblock;
Eric Anholtc4857422008-06-03 10:20:49 -070097
Eric Anholtd70d6052009-10-06 12:40:42 -070098 /* break left [p, newblock, p->next], then p = newblock */
99 if (startofs > p->ofs) {
100 newblock =
101 (struct mem_block *)calloc(1, sizeof(struct mem_block));
102 if (!newblock)
103 return NULL;
104 newblock->ofs = startofs;
105 newblock->size = p->size - (startofs - p->ofs);
106 newblock->free = 1;
107 newblock->heap = p->heap;
Eric Anholtc4857422008-06-03 10:20:49 -0700108
Eric Anholtd70d6052009-10-06 12:40:42 -0700109 newblock->next = p->next;
110 newblock->prev = p;
111 p->next->prev = newblock;
112 p->next = newblock;
Eric Anholtc4857422008-06-03 10:20:49 -0700113
Eric Anholtd70d6052009-10-06 12:40:42 -0700114 newblock->next_free = p->next_free;
115 newblock->prev_free = p;
116 p->next_free->prev_free = newblock;
117 p->next_free = newblock;
Eric Anholtc4857422008-06-03 10:20:49 -0700118
Eric Anholtd70d6052009-10-06 12:40:42 -0700119 p->size -= newblock->size;
120 p = newblock;
121 }
Eric Anholtc4857422008-06-03 10:20:49 -0700122
Eric Anholtd70d6052009-10-06 12:40:42 -0700123 /* break right, also [p, newblock, p->next] */
124 if (size < p->size) {
125 newblock =
126 (struct mem_block *)calloc(1, sizeof(struct mem_block));
127 if (!newblock)
128 return NULL;
129 newblock->ofs = startofs + size;
130 newblock->size = p->size - size;
131 newblock->free = 1;
132 newblock->heap = p->heap;
Eric Anholtc4857422008-06-03 10:20:49 -0700133
Eric Anholtd70d6052009-10-06 12:40:42 -0700134 newblock->next = p->next;
135 newblock->prev = p;
136 p->next->prev = newblock;
137 p->next = newblock;
Eric Anholtc4857422008-06-03 10:20:49 -0700138
Eric Anholtd70d6052009-10-06 12:40:42 -0700139 newblock->next_free = p->next_free;
140 newblock->prev_free = p;
141 p->next_free->prev_free = newblock;
142 p->next_free = newblock;
Eric Anholtc4857422008-06-03 10:20:49 -0700143
Eric Anholtd70d6052009-10-06 12:40:42 -0700144 p->size = size;
145 }
Eric Anholtc4857422008-06-03 10:20:49 -0700146
Eric Anholtd70d6052009-10-06 12:40:42 -0700147 /* p = middle block */
148 p->free = 0;
Eric Anholtc4857422008-06-03 10:20:49 -0700149
Eric Anholtd70d6052009-10-06 12:40:42 -0700150 /* Remove p from the free list:
151 */
152 p->next_free->prev_free = p->prev_free;
153 p->prev_free->next_free = p->next_free;
Eric Anholtc4857422008-06-03 10:20:49 -0700154
Eric Anholtd70d6052009-10-06 12:40:42 -0700155 p->next_free = 0;
156 p->prev_free = 0;
157
158 p->reserved = reserved;
159 return p;
Eric Anholtc4857422008-06-03 10:20:49 -0700160}
161
Eric Anholtd70d6052009-10-06 12:40:42 -0700162struct mem_block *mmAllocMem(struct mem_block *heap, int size, int align2,
163 int startSearch)
Eric Anholtc4857422008-06-03 10:20:49 -0700164{
Eric Anholtd70d6052009-10-06 12:40:42 -0700165 struct mem_block *p;
166 const int mask = (1 << align2) - 1;
167 int startofs = 0;
168 int endofs;
Eric Anholtc4857422008-06-03 10:20:49 -0700169
Eric Anholtd70d6052009-10-06 12:40:42 -0700170 if (!heap || align2 < 0 || size <= 0)
171 return NULL;
Eric Anholtc4857422008-06-03 10:20:49 -0700172
Eric Anholtd70d6052009-10-06 12:40:42 -0700173 for (p = heap->next_free; p != heap; p = p->next_free) {
174 assert(p->free);
Eric Anholtc4857422008-06-03 10:20:49 -0700175
Eric Anholtd70d6052009-10-06 12:40:42 -0700176 startofs = (p->ofs + mask) & ~mask;
177 if (startofs < startSearch) {
178 startofs = startSearch;
179 }
180 endofs = startofs + size;
181 if (endofs <= (p->ofs + p->size))
182 break;
183 }
Eric Anholtc4857422008-06-03 10:20:49 -0700184
Eric Anholtd70d6052009-10-06 12:40:42 -0700185 if (p == heap)
186 return NULL;
Eric Anholtc4857422008-06-03 10:20:49 -0700187
Eric Anholtd70d6052009-10-06 12:40:42 -0700188 assert(p->free);
189 p = SliceBlock(p, startofs, size, 0, mask + 1);
Eric Anholtc4857422008-06-03 10:20:49 -0700190
Eric Anholtd70d6052009-10-06 12:40:42 -0700191 return p;
Eric Anholtc4857422008-06-03 10:20:49 -0700192}
193
Eric Anholtd70d6052009-10-06 12:40:42 -0700194struct mem_block *mmFindBlock(struct mem_block *heap, int start)
Eric Anholtc4857422008-06-03 10:20:49 -0700195{
Eric Anholtd70d6052009-10-06 12:40:42 -0700196 struct mem_block *p;
Eric Anholtc4857422008-06-03 10:20:49 -0700197
Eric Anholtd70d6052009-10-06 12:40:42 -0700198 for (p = heap->next; p != heap; p = p->next) {
199 if (p->ofs == start)
200 return p;
201 }
Eric Anholtc4857422008-06-03 10:20:49 -0700202
Eric Anholtd70d6052009-10-06 12:40:42 -0700203 return NULL;
Eric Anholtc4857422008-06-03 10:20:49 -0700204}
205
Eric Anholtd70d6052009-10-06 12:40:42 -0700206static int Join2Blocks(struct mem_block *p)
Eric Anholtc4857422008-06-03 10:20:49 -0700207{
Eric Anholtd70d6052009-10-06 12:40:42 -0700208 /* XXX there should be some assertions here */
Eric Anholtc4857422008-06-03 10:20:49 -0700209
Eric Anholtd70d6052009-10-06 12:40:42 -0700210 /* NOTE: heap->free == 0 */
Eric Anholtc4857422008-06-03 10:20:49 -0700211
Eric Anholtd70d6052009-10-06 12:40:42 -0700212 if (p->free && p->next->free) {
213 struct mem_block *q = p->next;
Eric Anholtc4857422008-06-03 10:20:49 -0700214
Eric Anholtd70d6052009-10-06 12:40:42 -0700215 assert(p->ofs + p->size == q->ofs);
216 p->size += q->size;
Eric Anholtc4857422008-06-03 10:20:49 -0700217
Eric Anholtd70d6052009-10-06 12:40:42 -0700218 p->next = q->next;
219 q->next->prev = p;
Eric Anholtc4857422008-06-03 10:20:49 -0700220
Eric Anholtd70d6052009-10-06 12:40:42 -0700221 q->next_free->prev_free = q->prev_free;
222 q->prev_free->next_free = q->next_free;
223
224 free(q);
225 return 1;
226 }
227 return 0;
Eric Anholtc4857422008-06-03 10:20:49 -0700228}
229
Eric Anholtd70d6052009-10-06 12:40:42 -0700230int mmFreeMem(struct mem_block *b)
Eric Anholtc4857422008-06-03 10:20:49 -0700231{
Eric Anholtd70d6052009-10-06 12:40:42 -0700232 if (!b)
233 return 0;
Eric Anholtc4857422008-06-03 10:20:49 -0700234
Eric Anholtd70d6052009-10-06 12:40:42 -0700235 if (b->free) {
236 drmMsg("block already free\n");
237 return -1;
238 }
239 if (b->reserved) {
240 drmMsg("block is reserved\n");
241 return -1;
242 }
Eric Anholtc4857422008-06-03 10:20:49 -0700243
Eric Anholtd70d6052009-10-06 12:40:42 -0700244 b->free = 1;
245 b->next_free = b->heap->next_free;
246 b->prev_free = b->heap;
247 b->next_free->prev_free = b;
248 b->prev_free->next_free = b;
Eric Anholtc4857422008-06-03 10:20:49 -0700249
Eric Anholtd70d6052009-10-06 12:40:42 -0700250 Join2Blocks(b);
251 if (b->prev != b->heap)
252 Join2Blocks(b->prev);
Eric Anholtc4857422008-06-03 10:20:49 -0700253
Eric Anholtd70d6052009-10-06 12:40:42 -0700254 return 0;
Eric Anholtc4857422008-06-03 10:20:49 -0700255}
256
Eric Anholtd70d6052009-10-06 12:40:42 -0700257void mmDestroy(struct mem_block *heap)
Eric Anholtc4857422008-06-03 10:20:49 -0700258{
Eric Anholtd70d6052009-10-06 12:40:42 -0700259 struct mem_block *p;
Eric Anholtc4857422008-06-03 10:20:49 -0700260
Eric Anholtd70d6052009-10-06 12:40:42 -0700261 if (!heap)
262 return;
Eric Anholtc4857422008-06-03 10:20:49 -0700263
Eric Anholtd70d6052009-10-06 12:40:42 -0700264 for (p = heap->next; p != heap;) {
265 struct mem_block *next = p->next;
266 free(p);
267 p = next;
268 }
Eric Anholtc4857422008-06-03 10:20:49 -0700269
Eric Anholtd70d6052009-10-06 12:40:42 -0700270 free(heap);
Eric Anholtc4857422008-06-03 10:20:49 -0700271}