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.
32 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
,p
->size
,
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
,p
->size
,
51 p
->reserved
? 'R':'.');
55 drmMsg("End of memory blocks\n");
59 mmInit(int ofs
, int size
)
61 struct mem_block
*heap
, *block
;
66 heap
= (struct mem_block
*) calloc(1, sizeof(struct mem_block
));
70 block
= (struct mem_block
*) calloc(1, sizeof(struct mem_block
));
78 heap
->next_free
= block
;
79 heap
->prev_free
= block
;
84 block
->next_free
= heap
;
85 block
->prev_free
= heap
;
95 static struct mem_block
*
96 SliceBlock(struct mem_block
*p
,
97 int startofs
, int size
,
98 int reserved
, int alignment
)
100 struct mem_block
*newblock
;
102 /* break left [p, newblock, p->next], then p = newblock */
103 if (startofs
> p
->ofs
) {
104 newblock
= (struct mem_block
*) calloc(1, sizeof(struct mem_block
));
107 newblock
->ofs
= startofs
;
108 newblock
->size
= p
->size
- (startofs
- p
->ofs
);
110 newblock
->heap
= p
->heap
;
112 newblock
->next
= p
->next
;
114 p
->next
->prev
= newblock
;
117 newblock
->next_free
= p
->next_free
;
118 newblock
->prev_free
= p
;
119 p
->next_free
->prev_free
= newblock
;
120 p
->next_free
= newblock
;
122 p
->size
-= newblock
->size
;
126 /* break right, also [p, newblock, p->next] */
127 if (size
< p
->size
) {
128 newblock
= (struct mem_block
*) calloc(1, sizeof(struct mem_block
));
131 newblock
->ofs
= startofs
+ size
;
132 newblock
->size
= p
->size
- size
;
134 newblock
->heap
= p
->heap
;
136 newblock
->next
= p
->next
;
138 p
->next
->prev
= newblock
;
141 newblock
->next_free
= p
->next_free
;
142 newblock
->prev_free
= p
;
143 p
->next_free
->prev_free
= newblock
;
144 p
->next_free
= newblock
;
149 /* p = middle block */
152 /* Remove p from the free list:
154 p
->next_free
->prev_free
= p
->prev_free
;
155 p
->prev_free
->next_free
= p
->next_free
;
160 p
->reserved
= reserved
;
166 mmAllocMem(struct mem_block
*heap
, int size
, int align2
, int startSearch
)
169 const int mask
= (1 << align2
)-1;
173 if (!heap
|| align2
< 0 || size
<= 0)
176 for (p
= heap
->next_free
; p
!= heap
; p
= p
->next_free
) {
179 startofs
= (p
->ofs
+ mask
) & ~mask
;
180 if ( startofs
< startSearch
) {
181 startofs
= startSearch
;
183 endofs
= startofs
+size
;
184 if (endofs
<= (p
->ofs
+p
->size
))
192 p
= SliceBlock(p
,startofs
,size
,0,mask
+1);
199 mmFindBlock(struct mem_block
*heap
, int start
)
203 for (p
= heap
->next
; p
!= heap
; p
= p
->next
) {
213 Join2Blocks(struct mem_block
*p
)
215 /* XXX there should be some assertions here */
217 /* NOTE: heap->free == 0 */
219 if (p
->free
&& p
->next
->free
) {
220 struct mem_block
*q
= p
->next
;
222 assert(p
->ofs
+ p
->size
== q
->ofs
);
228 q
->next_free
->prev_free
= q
->prev_free
;
229 q
->prev_free
->next_free
= q
->next_free
;
238 mmFreeMem(struct mem_block
*b
)
244 drmMsg("block already free\n");
248 drmMsg("block is reserved\n");
253 b
->next_free
= b
->heap
->next_free
;
254 b
->prev_free
= b
->heap
;
255 b
->next_free
->prev_free
= b
;
256 b
->prev_free
->next_free
= b
;
259 if (b
->prev
!= b
->heap
)
260 Join2Blocks(b
->prev
);
267 mmDestroy(struct mem_block
*heap
)
274 for (p
= heap
->next
; p
!= heap
; ) {
275 struct mem_block
*next
= p
->next
;