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.
28 extern unsigned int VIA_DEBUG;
30 set_t *via_setInit(void)
34 set = (set_t *) drm_alloc(sizeof(set_t), DRM_MEM_DRIVER);
35 for (i = 0; i < SET_SIZE; i++) {
36 set->list[i].free_next = i + 1;
37 set->list[i].alloc_next = -1;
39 set->list[SET_SIZE - 1].free_next = -1;
46 int via_setAdd(set_t * set, ITEM_TYPE item)
50 set->list[free].val = item;
51 set->free = set->list[free].free_next;
55 set->list[free].alloc_next = set->alloc;
57 set->list[free].free_next = -1;
61 int via_setDel(set_t * set, ITEM_TYPE item)
63 int alloc = set->alloc;
67 if (set->list[alloc].val == item) {
69 set->list[prev].alloc_next =
70 set->list[alloc].alloc_next;
72 set->alloc = set->list[alloc].alloc_next;
76 alloc = set->list[alloc].alloc_next;
82 set->list[alloc].free_next = set->free;
84 set->list[alloc].alloc_next = -1;
89 /* setFirst -> setAdd -> setNext is wrong */
91 int via_setFirst(set_t * set, ITEM_TYPE * item)
96 *item = set->list[set->alloc].val;
97 set->trace = set->list[set->alloc].alloc_next;
102 int via_setNext(set_t * set, ITEM_TYPE * item)
104 if (set->trace == -1)
107 *item = set->list[set->trace].val;
108 set->trace = set->list[set->trace].alloc_next;
113 int via_setDestroy(set_t * set)
115 drm_free(set, sizeof(set_t), DRM_MEM_DRIVER);
120 #define ISFREE(bptr) ((bptr)->free)
122 #define fprintf(fmt, arg...) do{}while(0)
124 memHeap_t *via_mmInit(int ofs, int size)
131 blocks = (TMemBlock *) drm_calloc(1, sizeof(TMemBlock), DRM_MEM_DRIVER);
137 return (memHeap_t *) blocks;
142 static TMemBlock *SliceBlock(TMemBlock * p,
143 int startofs, int size,
144 int reserved, int alignment)
149 if (startofs > p->ofs) {
151 (TMemBlock *) drm_calloc(1, sizeof(TMemBlock),
153 newblock->ofs = startofs;
154 newblock->size = p->size - (startofs - p->ofs);
156 newblock->next = p->next;
157 p->size -= newblock->size;
163 if (size < p->size) {
165 (TMemBlock *) drm_calloc(1, sizeof(TMemBlock),
167 newblock->ofs = startofs + size;
168 newblock->size = p->size - size;
170 newblock->next = p->next;
175 /* p = middle block */
176 p->align = alignment;
178 p->reserved = reserved;
182 PMemBlock via_mmAllocMem(memHeap_t * heap, int size, int align2,
185 int mask, startofs, endofs;
188 if (!heap || align2 < 0 || size <= 0)
191 mask = (1 << align2) - 1;
193 p = (TMemBlock *) heap;
197 startofs = (p->ofs + mask) & ~mask;
199 if (startofs < startSearch)
200 startofs = startSearch;
202 endofs = startofs + size;
204 if (endofs <= (p->ofs + p->size))
214 p = SliceBlock(p, startofs, size, 0, mask + 1);
220 static __inline__ int Join2Blocks(TMemBlock * p)
222 if (p->free && p->next && p->next->free) {
223 TMemBlock *q = p->next;
226 drm_free(q, sizeof(TMemBlock), DRM_MEM_DRIVER);
234 int via_mmFreeMem(PMemBlock b)
242 fprintf(stderr, "no heap\n");
250 while (p && p != b) {
255 if (!p || p->free || p->reserved) {
257 fprintf(stderr, "block not found in heap\n");
259 fprintf(stderr, "block already free\n");
261 fprintf(stderr, "block is reserved\n");