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.
33 #include "libdrm_macros.h"
36 drm_private void mmDumpMemInfo(const struct mem_block *heap)
38 drmMsg("Memory heap %p:\n", (void *)heap);
40 drmMsg(" heap == 0\n");
42 const struct mem_block *p;
44 for (p = heap->next; p != heap; p = p->next) {
45 drmMsg(" Offset:%08x, Size:%08x, %c%c\n", p->ofs,
46 p->size, p->free ? 'F' : '.',
47 p->reserved ? 'R' : '.');
50 drmMsg("\nFree list:\n");
52 for (p = heap->next_free; p != heap; p = p->next_free) {
53 drmMsg(" FREE Offset:%08x, Size:%08x, %c%c\n", p->ofs,
54 p->size, p->free ? 'F' : '.',
55 p->reserved ? 'R' : '.');
59 drmMsg("End of memory blocks\n");
62 drm_private struct mem_block *mmInit(int ofs, int size)
64 struct mem_block *heap, *block;
69 heap = (struct mem_block *)calloc(1, sizeof(struct mem_block));
73 block = (struct mem_block *)calloc(1, sizeof(struct mem_block));
81 heap->next_free = block;
82 heap->prev_free = block;
87 block->next_free = heap;
88 block->prev_free = heap;
97 static struct mem_block *SliceBlock(struct mem_block *p,
98 int startofs, int size,
99 int reserved, int alignment)
101 struct mem_block *newblock;
103 /* break left [p, newblock, p->next], then p = newblock */
104 if (startofs > p->ofs) {
106 (struct mem_block *)calloc(1, sizeof(struct mem_block));
109 newblock->ofs = startofs;
110 newblock->size = p->size - (startofs - p->ofs);
112 newblock->heap = p->heap;
114 newblock->next = p->next;
116 p->next->prev = newblock;
119 newblock->next_free = p->next_free;
120 newblock->prev_free = p;
121 p->next_free->prev_free = newblock;
122 p->next_free = newblock;
124 p->size -= newblock->size;
128 /* break right, also [p, newblock, p->next] */
129 if (size < p->size) {
131 (struct mem_block *)calloc(1, sizeof(struct mem_block));
134 newblock->ofs = startofs + size;
135 newblock->size = p->size - size;
137 newblock->heap = p->heap;
139 newblock->next = p->next;
141 p->next->prev = newblock;
144 newblock->next_free = p->next_free;
145 newblock->prev_free = p;
146 p->next_free->prev_free = newblock;
147 p->next_free = newblock;
152 /* p = middle block */
155 /* Remove p from the free list:
157 p->next_free->prev_free = p->prev_free;
158 p->prev_free->next_free = p->next_free;
163 p->reserved = reserved;
167 drm_private struct mem_block *mmAllocMem(struct mem_block *heap, int size,
168 int align2, int startSearch)
171 const int mask = (1 << align2) - 1;
175 if (!heap || align2 < 0 || size <= 0)
178 for (p = heap->next_free; p != heap; p = p->next_free) {
181 startofs = (p->ofs + mask) & ~mask;
182 if (startofs < startSearch) {
183 startofs = startSearch;
185 endofs = startofs + size;
186 if (endofs <= (p->ofs + p->size))
194 p = SliceBlock(p, startofs, size, 0, mask + 1);
199 static int Join2Blocks(struct mem_block *p)
201 /* XXX there should be some assertions here */
203 /* NOTE: heap->free == 0 */
205 if (p->free && p->next->free) {
206 struct mem_block *q = p->next;
208 assert(p->ofs + p->size == q->ofs);
214 q->next_free->prev_free = q->prev_free;
215 q->prev_free->next_free = q->next_free;
223 drm_private int mmFreeMem(struct mem_block *b)
229 drmMsg("block already free\n");
233 drmMsg("block is reserved\n");
238 b->next_free = b->heap->next_free;
239 b->prev_free = b->heap;
240 b->next_free->prev_free = b;
241 b->prev_free->next_free = b;
244 if (b->prev != b->heap)
245 Join2Blocks(b->prev);
250 drm_private void mmDestroy(struct mem_block *heap)
257 for (p = heap->next; p != heap;) {
258 struct mem_block *next = p->next;