2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
4 * Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sub license,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
29 extern unsigned int VIA_DEBUG
;
31 set_t
*via_setInit(void)
35 set
= (set_t
*) drm_alloc(sizeof(set_t
), DRM_MEM_DRIVER
);
36 for (i
= 0; i
< SET_SIZE
; i
++) {
37 set
->list
[i
].free_next
= i
+ 1;
38 set
->list
[i
].alloc_next
= -1;
40 set
->list
[SET_SIZE
- 1].free_next
= -1;
47 int via_setAdd(set_t
* set
, ITEM_TYPE item
)
49 int sfree
= set
->free
;
51 set
->list
[sfree
].val
= item
;
52 set
->free
= set
->list
[sfree
].free_next
;
56 set
->list
[sfree
].alloc_next
= set
->alloc
;
58 set
->list
[sfree
].free_next
= -1;
62 int via_setDel(set_t
* set
, ITEM_TYPE item
)
64 int alloc
= set
->alloc
;
68 if (set
->list
[alloc
].val
== item
) {
70 set
->list
[prev
].alloc_next
=
71 set
->list
[alloc
].alloc_next
;
73 set
->alloc
= set
->list
[alloc
].alloc_next
;
77 alloc
= set
->list
[alloc
].alloc_next
;
83 set
->list
[alloc
].free_next
= set
->free
;
85 set
->list
[alloc
].alloc_next
= -1;
90 /* setFirst -> setAdd -> setNext is wrong */
92 int via_setFirst(set_t
* set
, ITEM_TYPE
* item
)
97 *item
= set
->list
[set
->alloc
].val
;
98 set
->trace
= set
->list
[set
->alloc
].alloc_next
;
103 int via_setNext(set_t
* set
, ITEM_TYPE
* item
)
105 if (set
->trace
== -1)
108 *item
= set
->list
[set
->trace
].val
;
109 set
->trace
= set
->list
[set
->trace
].alloc_next
;
114 int via_setDestroy(set_t
* set
)
116 drm_free(set
, sizeof(set_t
), DRM_MEM_DRIVER
);
121 #define ISFREE(bptr) ((bptr)->free)
123 #define fprintf(fmt, arg...) do{}while(0)
125 memHeap_t
*via_mmInit(int ofs
, int size
)
132 blocks
= (TMemBlock
*) drm_calloc(1, sizeof(TMemBlock
), DRM_MEM_DRIVER
);
138 return (memHeap_t
*) blocks
;
143 static TMemBlock
*SliceBlock(TMemBlock
* p
,
144 int startofs
, int size
,
145 int reserved
, int alignment
)
150 if (startofs
> p
->ofs
) {
152 (TMemBlock
*) drm_calloc(1, sizeof(TMemBlock
),
154 newblock
->ofs
= startofs
;
155 newblock
->size
= p
->size
- (startofs
- p
->ofs
);
157 newblock
->next
= p
->next
;
158 p
->size
-= newblock
->size
;
164 if (size
< p
->size
) {
166 (TMemBlock
*) drm_calloc(1, sizeof(TMemBlock
),
168 newblock
->ofs
= startofs
+ size
;
169 newblock
->size
= p
->size
- size
;
171 newblock
->next
= p
->next
;
176 /* p = middle block */
177 p
->align
= alignment
;
179 p
->reserved
= reserved
;
183 PMemBlock
via_mmAllocMem(memHeap_t
* heap
, int size
, int align2
,
186 int mask
, startofs
, endofs
;
189 if (!heap
|| align2
< 0 || size
<= 0)
192 mask
= (1 << align2
) - 1;
194 p
= (TMemBlock
*) heap
;
198 startofs
= (p
->ofs
+ mask
) & ~mask
;
200 if (startofs
< startSearch
)
201 startofs
= startSearch
;
203 endofs
= startofs
+ size
;
205 if (endofs
<= (p
->ofs
+ p
->size
))
215 p
= SliceBlock(p
, startofs
, size
, 0, mask
+ 1);
221 static __inline__
int Join2Blocks(TMemBlock
* p
)
223 if (p
->free
&& p
->next
&& p
->next
->free
) {
224 TMemBlock
*q
= p
->next
;
227 drm_free(q
, sizeof(TMemBlock
), DRM_MEM_DRIVER
);
235 int via_mmFreeMem(PMemBlock b
)
243 fprintf(stderr
, "no heap\n");
251 while (p
&& p
!= b
) {
256 if (!p
|| p
->free
|| p
->reserved
) {
258 fprintf(stderr
, "block not found in heap\n");
260 fprintf(stderr
, "block already free\n");
262 fprintf(stderr
, "block is reserved\n");