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.
34 mmDumpMemInfo(const struct mem_block *heap)
36 fprintf(stderr, "Memory heap %p:\n", (void *)heap);
38 fprintf(stderr, " heap == 0\n");
40 const struct mem_block *p;
42 for(p = heap->next; p != heap; p = p->next) {
43 fprintf(stderr, " Offset:%08x, Size:%08x, %c%c\n",p->ofs,p->size,
45 p->reserved ? 'R':'.');
48 fprintf(stderr, "\nFree list:\n");
50 for(p = heap->next_free; p != heap; p = p->next_free) {
51 fprintf(stderr, " FREE Offset:%08x, Size:%08x, %c%c\n",p->ofs,p->size,
53 p->reserved ? 'R':'.');
57 fprintf(stderr, "End of memory blocks\n");
61 mmInit(unsigned ofs, unsigned size)
63 struct mem_block *heap, *block;
68 heap = (struct mem_block *) calloc(1, sizeof(struct mem_block));
72 block = (struct mem_block *) calloc(1, sizeof(struct mem_block));
80 heap->next_free = block;
81 heap->prev_free = block;
86 block->next_free = heap;
87 block->prev_free = heap;
97 static struct mem_block *
98 SliceBlock(struct mem_block *p,
99 unsigned startofs, unsigned size,
100 unsigned reserved, unsigned alignment)
102 struct mem_block *newblock;
104 /* break left [p, newblock, p->next], then p = newblock */
105 if (startofs > p->ofs) {
106 newblock = (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) {
130 newblock = (struct mem_block*) calloc(1, sizeof(struct mem_block));
133 newblock->ofs = startofs + size;
134 newblock->size = p->size - size;
136 newblock->heap = p->heap;
138 newblock->next = p->next;
140 p->next->prev = newblock;
143 newblock->next_free = p->next_free;
144 newblock->prev_free = p;
145 p->next_free->prev_free = newblock;
146 p->next_free = newblock;
151 /* p = middle block */
154 /* Remove p from the free list:
156 p->next_free->prev_free = p->prev_free;
157 p->prev_free->next_free = p->next_free;
162 p->reserved = reserved;
168 mmAllocMem(struct mem_block *heap, unsigned size, unsigned align2, unsigned startSearch)
171 const unsigned mask = (1 << align2)-1;
172 unsigned startofs = 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);
201 mmFindBlock(struct mem_block *heap, unsigned start)
205 for (p = heap->next; p != heap; p = p->next) {
215 Join2Blocks(struct mem_block *p)
217 /* XXX there should be some assertions here */
219 /* NOTE: heap->free == 0 */
221 if (p->free && p->next->free) {
222 struct mem_block *q = p->next;
224 assert(p->ofs + p->size == q->ofs);
230 q->next_free->prev_free = q->prev_free;
231 q->prev_free->next_free = q->next_free;
240 mmFreeMem(struct mem_block *b)
246 fprintf(stderr, "block already free\n");
250 fprintf(stderr, "block is reserved\n");
255 b->next_free = b->heap->next_free;
256 b->prev_free = b->heap;
257 b->next_free->prev_free = b;
258 b->prev_free->next_free = b;
261 if (b->prev != b->heap)
262 Join2Blocks(b->prev);
269 mmDestroy(struct mem_block *heap)
276 for (p = heap->next; p != heap; ) {
277 struct mem_block *next = p->next;