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.
29 #include "libdrm_macros.h"
32 drm_private void mmDumpMemInfo(const struct mem_block *heap)
34 drmMsg("Memory heap %p:\n", (void *)heap);
36 drmMsg(" heap == 0\n");
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,
42 p->size, 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,
50 p->size, p->free ? 'F' : '.',
51 p->reserved ? 'R' : '.');
55 drmMsg("End of memory blocks\n");
58 drm_private struct mem_block *mmInit(int ofs, int size)
60 struct mem_block *heap, *block;
65 heap = (struct mem_block *)calloc(1, sizeof(struct mem_block));
69 block = (struct mem_block *)calloc(1, sizeof(struct mem_block));
77 heap->next_free = block;
78 heap->prev_free = block;
83 block->next_free = heap;
84 block->prev_free = heap;
93 static struct mem_block *SliceBlock(struct mem_block *p,
94 int startofs, int size,
95 int reserved, int alignment)
97 struct mem_block *newblock;
99 /* break left [p, newblock, p->next], then p = newblock */
100 if (startofs > p->ofs) {
102 (struct mem_block *)calloc(1, sizeof(struct mem_block));
105 newblock->ofs = startofs;
106 newblock->size = p->size - (startofs - p->ofs);
108 newblock->heap = p->heap;
110 newblock->next = p->next;
112 p->next->prev = newblock;
115 newblock->next_free = p->next_free;
116 newblock->prev_free = p;
117 p->next_free->prev_free = newblock;
118 p->next_free = newblock;
120 p->size -= newblock->size;
124 /* break right, also [p, newblock, p->next] */
125 if (size < p->size) {
127 (struct mem_block *)calloc(1, sizeof(struct mem_block));
130 newblock->ofs = startofs + size;
131 newblock->size = p->size - size;
133 newblock->heap = p->heap;
135 newblock->next = p->next;
137 p->next->prev = newblock;
140 newblock->next_free = p->next_free;
141 newblock->prev_free = p;
142 p->next_free->prev_free = newblock;
143 p->next_free = newblock;
148 /* p = middle block */
151 /* Remove p from the free list:
153 p->next_free->prev_free = p->prev_free;
154 p->prev_free->next_free = p->next_free;
159 p->reserved = reserved;
163 drm_private struct mem_block *mmAllocMem(struct mem_block *heap, int size,
164 int align2, int startSearch)
167 const int mask = (1 << align2) - 1;
171 if (!heap || align2 < 0 || size <= 0)
174 for (p = heap->next_free; p != heap; p = p->next_free) {
177 startofs = (p->ofs + mask) & ~mask;
178 if (startofs < startSearch) {
179 startofs = startSearch;
181 endofs = startofs + size;
182 if (endofs <= (p->ofs + p->size))
190 p = SliceBlock(p, startofs, size, 0, mask + 1);
195 static int Join2Blocks(struct mem_block *p)
197 /* XXX there should be some assertions here */
199 /* NOTE: heap->free == 0 */
201 if (p->free && p->next->free) {
202 struct mem_block *q = p->next;
204 assert(p->ofs + p->size == q->ofs);
210 q->next_free->prev_free = q->prev_free;
211 q->prev_free->next_free = q->next_free;
219 drm_private int mmFreeMem(struct mem_block *b)
225 drmMsg("block already free\n");
229 drmMsg("block is reserved\n");
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;
240 if (b->prev != b->heap)
241 Join2Blocks(b->prev);
246 drm_private void mmDestroy(struct mem_block *heap)
253 for (p = heap->next; p != heap;) {
254 struct mem_block *next = p->next;