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.
24 /* $XFree86: xc/lib/GL/mesa/src/drv/common/mm.c,v 1.4 2002/10/30 12:51:27 alanh Exp $ */
32 void mmDumpMemInfo( memHeap_t
*heap
)
36 fprintf(stderr
, "Memory heap %p:\n", (void *)heap
);
38 fprintf(stderr
, " heap == 0\n");
40 p
= (TMemBlock
*)heap
;
42 fprintf(stderr
, " Offset:%08x, Size:%08x, %c%c\n",p
->ofs
,p
->size
,
44 p
->reserved
? 'R':'.');
48 fprintf(stderr
, "End of memory blocks\n");
51 memHeap_t
*mmInit(int ofs
,
59 blocks
= (TMemBlock
*) calloc(1,sizeof(TMemBlock
));
64 return (memHeap_t
*)blocks
;
70 static TMemBlock
* SliceBlock(TMemBlock
*p
,
71 int startofs
, int size
,
72 int reserved
, int alignment
)
77 if (startofs
> p
->ofs
) {
78 newblock
= (TMemBlock
*) calloc(1,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
*) calloc(1,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
;
110 PMemBlock
mmAllocMem( memHeap_t
*heap
, int size
, int align2
, int startSearch
)
112 int mask
,startofs
,endofs
;
115 if (!heap
|| align2
< 0 || size
<= 0)
117 mask
= (1 << align2
)-1;
119 p
= (TMemBlock
*)heap
;
122 startofs
= (p
->ofs
+ mask
) & ~mask
;
123 if ( startofs
< startSearch
) {
124 startofs
= startSearch
;
126 endofs
= startofs
+size
;
127 if (endofs
<= (p
->ofs
+p
->size
))
134 p
= SliceBlock(p
,startofs
,size
,0,mask
+1);
139 static __inline__
int Join2Blocks(TMemBlock
*p
)
141 if (p
->free
&& p
->next
&& p
->next
->free
) {
142 TMemBlock
*q
= p
->next
;
151 int mmFreeMem(PMemBlock b
)
158 fprintf(stderr
, "no heap\n");
163 while (p
&& p
!= b
) {
167 if (!p
|| p
->free
|| p
->reserved
) {
169 fprintf(stderr
, "block not found in heap\n");
171 fprintf(stderr
, "block already free\n");
173 fprintf(stderr
, "block is reserved\n");
184 void mmDestroy(memHeap_t
*heap
)
190 p
= (TMemBlock
*)heap
;