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 const struct mem_block
*p
;
33 fprintf(stderr
, "Memory heap %p:\n", (void *)heap
);
35 fprintf(stderr
, " heap == 0\n");
37 p
= (struct mem_block
*)heap
;
39 fprintf(stderr
, " Offset:%08x, Size:%08x, %c%c\n",p
->ofs
,p
->size
,
41 p
->reserved
? 'R':'.');
45 fprintf(stderr
, "End of memory blocks\n");
49 mmInit(int ofs
, int size
)
51 struct mem_block
*blocks
;
56 blocks
= (struct mem_block
*) _mesa_calloc(sizeof(struct mem_block
));
61 return (struct mem_block
*)blocks
;
69 static struct mem_block
*
70 SliceBlock(struct mem_block
*p
,
71 int startofs
, int size
,
72 int reserved
, int alignment
)
74 struct mem_block
*newblock
;
77 if (startofs
> p
->ofs
) {
78 newblock
= (struct mem_block
*) _mesa_calloc(sizeof(struct mem_block
));
81 newblock
->ofs
= startofs
;
82 newblock
->size
= p
->size
- (startofs
- p
->ofs
);
84 newblock
->next
= p
->next
;
85 p
->size
-= newblock
->size
;
92 newblock
= (struct mem_block
*) _mesa_calloc(sizeof(struct mem_block
));
95 newblock
->ofs
= startofs
+ size
;
96 newblock
->size
= p
->size
- size
;
98 newblock
->next
= p
->next
;
103 /* p = middle block */
104 p
->align
= alignment
;
106 p
->reserved
= reserved
;
112 mmAllocMem(struct mem_block
*heap
, int size
, int align2
, int startSearch
)
114 struct mem_block
*p
= heap
;
115 int mask
= (1 << align2
)-1;
119 if (!heap
|| align2
< 0 || size
<= 0)
124 startofs
= (p
->ofs
+ mask
) & ~mask
;
125 if ( startofs
< startSearch
) {
126 startofs
= startSearch
;
128 endofs
= startofs
+size
;
129 if (endofs
<= (p
->ofs
+p
->size
))
136 p
= SliceBlock(p
,startofs
,size
,0,mask
+1);
143 mmFindBlock(struct mem_block
*heap
, int start
)
145 struct mem_block
*p
= (struct mem_block
*)heap
;
148 if (p
->ofs
== start
&& p
->free
)
159 Join2Blocks(struct mem_block
*p
)
161 /* XXX there should be some assertions here */
162 if (p
->free
&& p
->next
&& p
->next
->free
) {
163 struct mem_block
*q
= p
->next
;
173 mmFreeMem(struct mem_block
*b
)
175 struct mem_block
*p
,*prev
;
180 fprintf(stderr
, "no heap\n");
185 while (p
&& p
!= b
) {
189 if (!p
|| p
->free
|| p
->reserved
) {
191 fprintf(stderr
, "block not found in heap\n");
193 fprintf(stderr
, "block already free\n");
195 fprintf(stderr
, "block is reserved\n");
207 mmDestroy(struct mem_block
*heap
)
214 p
= (struct mem_block
*) heap
;
216 struct mem_block
*next
= p
->next
;