7a657dc3cad42fd0dfa22a6abc12f1e2bc6397ae
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 mmDumpMemInfo(const struct mem_block
*heap
)
31 fprintf(stderr
, "Memory heap %p:\n", (void *)heap
);
33 fprintf(stderr
, " heap == 0\n");
35 const struct mem_block
*p
;
37 for(p
= heap
->next
; p
!= heap
; p
= p
->next
) {
38 fprintf(stderr
, " Offset:%08x, Size:%08x, %c%c\n",p
->ofs
,p
->size
,
40 p
->reserved
? 'R':'.');
43 fprintf(stderr
, "\nFree list:\n");
45 for(p
= heap
->next_free
; p
!= heap
; p
= p
->next_free
) {
46 fprintf(stderr
, " FREE Offset:%08x, Size:%08x, %c%c\n",p
->ofs
,p
->size
,
48 p
->reserved
? 'R':'.');
52 fprintf(stderr
, "End of memory blocks\n");
56 mmInit(int ofs
, int size
)
58 struct mem_block
*heap
, *block
;
63 heap
= (struct mem_block
*) _mesa_calloc(sizeof(struct mem_block
));
67 block
= (struct mem_block
*) _mesa_calloc(sizeof(struct mem_block
));
75 heap
->next_free
= block
;
76 heap
->prev_free
= block
;
81 block
->next_free
= heap
;
82 block
->prev_free
= heap
;
92 static struct mem_block
*
93 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
) {
101 newblock
= (struct mem_block
*) _mesa_calloc(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
) {
125 newblock
= (struct mem_block
*) _mesa_calloc(sizeof(struct mem_block
));
128 newblock
->ofs
= startofs
+ size
;
129 newblock
->size
= p
->size
- size
;
131 newblock
->heap
= p
->heap
;
133 newblock
->next
= p
->next
;
135 p
->next
->prev
= newblock
;
138 newblock
->next_free
= p
->next_free
;
139 newblock
->prev_free
= p
;
140 p
->next_free
->prev_free
= newblock
;
141 p
->next_free
= newblock
;
146 /* p = middle block */
149 /* Remove p from the free list:
151 p
->next_free
->prev_free
= p
->prev_free
;
152 p
->prev_free
->next_free
= p
->next_free
;
157 p
->reserved
= reserved
;
163 mmAllocMem(struct mem_block
*heap
, int size
, int align2
, int startSearch
)
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);
196 mmFindBlock(struct mem_block
*heap
, int start
)
200 for (p
= heap
->next
; p
!= heap
; p
= p
->next
) {
210 Join2Blocks(struct mem_block
*p
)
212 /* XXX there should be some assertions here */
214 /* NOTE: heap->free == 0 */
216 if (p
->free
&& p
->next
->free
) {
217 struct mem_block
*q
= p
->next
;
219 assert(p
->ofs
+ p
->size
== q
->ofs
);
225 q
->next_free
->prev_free
= q
->prev_free
;
226 q
->prev_free
->next_free
= q
->next_free
;
235 mmFreeMem(struct mem_block
*b
)
237 struct mem_block
*heap
= b
->heap
;
243 fprintf(stderr
, "block already free\n");
247 fprintf(stderr
, "block is reserved\n");
252 b
->next_free
= b
->heap
->next_free
;
253 b
->prev_free
= b
->heap
;
254 b
->next_free
->prev_free
= b
;
255 b
->prev_free
->next_free
= b
;
258 if (b
->prev
!= b
->heap
)
259 Join2Blocks(b
->prev
);
266 mmDestroy(struct mem_block
*heap
)
273 for (p
= heap
->next
; p
!= heap
; ) {
274 struct mem_block
*next
= p
->next
;