Sync usage with man page.
[netbsd-mini2440.git] / sys / external / bsd / drm / dist / libdrm / intel / mm.c
blob98146405a17c7d87b9c517354f772b8eabd3912f
1 /*
2 * GLX Hardware Device Driver common code
3 * Copyright (C) 1999 Wittawat Yamwong
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:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
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.
25 #include <stdlib.h>
26 #include <assert.h>
28 #include "xf86drm.h"
29 #include "mm.h"
31 void
32 mmDumpMemInfo(const struct mem_block *heap)
34 drmMsg("Memory heap %p:\n", (void *)heap);
35 if (heap == 0) {
36 drmMsg(" heap == 0\n");
37 } else {
38 const struct mem_block *p;
40 for(p = heap->next; p != heap; p = p->next) {
41 drmMsg(" Offset:%08x, Size:%08x, %c%c\n",p->ofs,p->size,
42 p->free ? 'F':'.',
43 p->reserved ? 'R':'.');
46 drmMsg("\nFree list:\n");
48 for(p = heap->next_free; p != heap; p = p->next_free) {
49 drmMsg(" FREE Offset:%08x, Size:%08x, %c%c\n",p->ofs,p->size,
50 p->free ? 'F':'.',
51 p->reserved ? 'R':'.');
55 drmMsg("End of memory blocks\n");
58 struct mem_block *
59 mmInit(int ofs, int size)
61 struct mem_block *heap, *block;
63 if (size <= 0)
64 return NULL;
66 heap = (struct mem_block *) calloc(1, sizeof(struct mem_block));
67 if (!heap)
68 return NULL;
70 block = (struct mem_block *) calloc(1, sizeof(struct mem_block));
71 if (!block) {
72 free(heap);
73 return NULL;
76 heap->next = block;
77 heap->prev = block;
78 heap->next_free = block;
79 heap->prev_free = block;
81 block->heap = heap;
82 block->next = heap;
83 block->prev = heap;
84 block->next_free = heap;
85 block->prev_free = heap;
87 block->ofs = ofs;
88 block->size = size;
89 block->free = 1;
91 return heap;
95 static struct mem_block *
96 SliceBlock(struct mem_block *p,
97 int startofs, int size,
98 int reserved, int alignment)
100 struct mem_block *newblock;
102 /* break left [p, newblock, p->next], then p = newblock */
103 if (startofs > p->ofs) {
104 newblock = (struct mem_block*) calloc(1, sizeof(struct mem_block));
105 if (!newblock)
106 return NULL;
107 newblock->ofs = startofs;
108 newblock->size = p->size - (startofs - p->ofs);
109 newblock->free = 1;
110 newblock->heap = p->heap;
112 newblock->next = p->next;
113 newblock->prev = p;
114 p->next->prev = newblock;
115 p->next = newblock;
117 newblock->next_free = p->next_free;
118 newblock->prev_free = p;
119 p->next_free->prev_free = newblock;
120 p->next_free = newblock;
122 p->size -= newblock->size;
123 p = newblock;
126 /* break right, also [p, newblock, p->next] */
127 if (size < p->size) {
128 newblock = (struct mem_block*) calloc(1, sizeof(struct mem_block));
129 if (!newblock)
130 return NULL;
131 newblock->ofs = startofs + size;
132 newblock->size = p->size - size;
133 newblock->free = 1;
134 newblock->heap = p->heap;
136 newblock->next = p->next;
137 newblock->prev = p;
138 p->next->prev = newblock;
139 p->next = newblock;
141 newblock->next_free = p->next_free;
142 newblock->prev_free = p;
143 p->next_free->prev_free = newblock;
144 p->next_free = newblock;
146 p->size = size;
149 /* p = middle block */
150 p->free = 0;
152 /* Remove p from the free list:
154 p->next_free->prev_free = p->prev_free;
155 p->prev_free->next_free = p->next_free;
157 p->next_free = 0;
158 p->prev_free = 0;
160 p->reserved = reserved;
161 return p;
165 struct mem_block *
166 mmAllocMem(struct mem_block *heap, int size, int align2, int startSearch)
168 struct mem_block *p;
169 const int mask = (1 << align2)-1;
170 int startofs = 0;
171 int endofs;
173 if (!heap || align2 < 0 || size <= 0)
174 return NULL;
176 for (p = heap->next_free; p != heap; p = p->next_free) {
177 assert(p->free);
179 startofs = (p->ofs + mask) & ~mask;
180 if ( startofs < startSearch ) {
181 startofs = startSearch;
183 endofs = startofs+size;
184 if (endofs <= (p->ofs+p->size))
185 break;
188 if (p == heap)
189 return NULL;
191 assert(p->free);
192 p = SliceBlock(p,startofs,size,0,mask+1);
194 return p;
198 struct mem_block *
199 mmFindBlock(struct mem_block *heap, int start)
201 struct mem_block *p;
203 for (p = heap->next; p != heap; p = p->next) {
204 if (p->ofs == start)
205 return p;
208 return NULL;
212 static int
213 Join2Blocks(struct mem_block *p)
215 /* XXX there should be some assertions here */
217 /* NOTE: heap->free == 0 */
219 if (p->free && p->next->free) {
220 struct mem_block *q = p->next;
222 assert(p->ofs + p->size == q->ofs);
223 p->size += q->size;
225 p->next = q->next;
226 q->next->prev = p;
228 q->next_free->prev_free = q->prev_free;
229 q->prev_free->next_free = q->next_free;
231 free(q);
232 return 1;
234 return 0;
238 mmFreeMem(struct mem_block *b)
240 if (!b)
241 return 0;
243 if (b->free) {
244 drmMsg("block already free\n");
245 return -1;
247 if (b->reserved) {
248 drmMsg("block is reserved\n");
249 return -1;
252 b->free = 1;
253 b->next_free = b->heap->next_free;
254 b->prev_free = b->heap;
255 b->next_free->prev_free = b;
256 b->prev_free->next_free = b;
258 Join2Blocks(b);
259 if (b->prev != b->heap)
260 Join2Blocks(b->prev);
262 return 0;
266 void
267 mmDestroy(struct mem_block *heap)
269 struct mem_block *p;
271 if (!heap)
272 return;
274 for (p = heap->next; p != heap; ) {
275 struct mem_block *next = p->next;
276 free(p);
277 p = next;
280 free(heap);