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.
31 void mmDumpMemInfo(const struct mem_block *heap)
33 drmMsg("Memory heap %p:\n", (void *)heap);
35 drmMsg(" heap == 0\n");
37 const struct mem_block *p;
39 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' : '.');
45 drmMsg("\nFree list:\n");
47 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' : '.');
54 drmMsg("End of memory blocks\n");
57 struct mem_block *mmInit(int ofs, int size)
59 struct mem_block *heap, *block;
64 heap = (struct mem_block *)calloc(1, sizeof(struct mem_block));
68 block = (struct mem_block *)calloc(1, sizeof(struct mem_block));
76 heap->next_free = block;
77 heap->prev_free = block;
82 block->next_free = heap;
83 block->prev_free = heap;
92 static struct mem_block *SliceBlock(struct mem_block *p,
93 int startofs, int size,
94 int reserved, int alignment)
96 struct mem_block *newblock;
98 /* break left [p, newblock, p->next], then p = newblock */
99 if (startofs > p->ofs) {
101 (struct mem_block *)calloc(1, sizeof(struct mem_block));
104 newblock->ofs = startofs;
105 newblock->size = p->size - (startofs - p->ofs);
107 newblock->heap = p->heap;
109 newblock->next = p->next;
111 p->next->prev = newblock;
114 newblock->next_free = p->next_free;
115 newblock->prev_free = p;
116 p->next_free->prev_free = newblock;
117 p->next_free = newblock;
119 p->size -= newblock->size;
123 /* break right, also [p, newblock, p->next] */
124 if (size < p->size) {
126 (struct mem_block *)calloc(1, sizeof(struct mem_block));
129 newblock->ofs = startofs + size;
130 newblock->size = p->size - size;
132 newblock->heap = p->heap;
134 newblock->next = p->next;
136 p->next->prev = newblock;
139 newblock->next_free = p->next_free;
140 newblock->prev_free = p;
141 p->next_free->prev_free = newblock;
142 p->next_free = newblock;
147 /* p = middle block */
150 /* Remove p from the free list:
152 p->next_free->prev_free = p->prev_free;
153 p->prev_free->next_free = p->next_free;
158 p->reserved = reserved;
162 struct mem_block *mmAllocMem(struct mem_block *heap, int size, int align2,
166 const int mask = (1 << align2) - 1;
170 if (!heap || align2 < 0 || size <= 0)
173 for (p = heap->next_free; p != heap; p = p->next_free) {
176 startofs = (p->ofs + mask) & ~mask;
177 if (startofs < startSearch) {
178 startofs = startSearch;
180 endofs = startofs + size;
181 if (endofs <= (p->ofs + p->size))
189 p = SliceBlock(p, startofs, size, 0, mask + 1);
194 struct mem_block *mmFindBlock(struct mem_block *heap, int start)
198 for (p = heap->next; p != heap; p = p->next) {
206 static int Join2Blocks(struct mem_block *p)
208 /* XXX there should be some assertions here */
210 /* NOTE: heap->free == 0 */
212 if (p->free && p->next->free) {
213 struct mem_block *q = p->next;
215 assert(p->ofs + p->size == q->ofs);
221 q->next_free->prev_free = q->prev_free;
222 q->prev_free->next_free = q->next_free;
230 int mmFreeMem(struct mem_block *b)
236 drmMsg("block already free\n");
240 drmMsg("block is reserved\n");
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;
251 if (b->prev != b->heap)
252 Join2Blocks(b->prev);
257 void mmDestroy(struct mem_block *heap)
264 for (p = heap->next; p != heap;) {
265 struct mem_block *next = p->next;