e4c96821732d03da9d05166939f9d100e8a09b3e
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 memHeap_t
*heap
)
33 fprintf(stderr
, "Memory heap %p:\n", (void *)heap
);
35 fprintf(stderr
, " heap == 0\n");
37 p
= (TMemBlock
*)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
)
56 blocks
= (TMemBlock
*) _mesa_calloc(sizeof(TMemBlock
));
61 return (memHeap_t
*)blocks
;
70 SliceBlock(TMemBlock
*p
,
71 int startofs
, int size
,
72 int reserved
, int alignment
)
77 if (startofs
> p
->ofs
) {
78 newblock
= (TMemBlock
*) _mesa_calloc(sizeof(TMemBlock
));
81 newblock
->ofs
= startofs
;
82 newblock
->size
= p
->size
- (startofs
- p
->ofs
);
84 newblock
->next
= p
->next
;
85 p
->size
-= newblock
->size
;
92 newblock
= (TMemBlock
*) _mesa_calloc(sizeof(TMemBlock
));
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(memHeap_t
*heap
, int size
, int align2
, int startSearch
)
114 int mask
,startofs
,endofs
;
117 if (!heap
|| align2
< 0 || size
<= 0)
119 mask
= (1 << align2
)-1;
121 p
= (TMemBlock
*)heap
;
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(memHeap_t
*heap
, int start
)
145 TMemBlock
*p
= (TMemBlock
*)heap
;
148 if (p
->ofs
== start
&& p
->free
)
159 Join2Blocks(TMemBlock
*p
)
161 /* XXX there should be some assertions here */
162 if (p
->free
&& p
->next
&& p
->next
->free
) {
163 TMemBlock
*q
= p
->next
;
173 mmFreeMem(PMemBlock b
)
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(memHeap_t
*heap
)
214 p
= (TMemBlock
*) heap
;
216 TMemBlock
*next
= p
->next
;