No empty .Rs/.Re
[netbsd-mini2440.git] / sys / external / bsd / drm / dist / shared-core / via_ds.c
blob9a79341bff8ced96d2be5a7fca3ac72804eadff2
1 /*
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
15 * of the Software.
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.
26 #include "drmP.h"
28 #include "via_ds.h"
29 extern unsigned int VIA_DEBUG;
31 set_t *via_setInit(void)
33 int i;
34 set_t *set;
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;
41 set->free = 0;
42 set->alloc = -1;
43 set->trace = -1;
44 return set;
47 int via_setAdd(set_t * set, ITEM_TYPE item)
49 int sfree = set->free;
50 if (sfree != -1) {
51 set->list[sfree].val = item;
52 set->free = set->list[sfree].free_next;
53 } else {
54 return 0;
56 set->list[sfree].alloc_next = set->alloc;
57 set->alloc = sfree;
58 set->list[sfree].free_next = -1;
59 return 1;
62 int via_setDel(set_t * set, ITEM_TYPE item)
64 int alloc = set->alloc;
65 int prev = -1;
67 while (alloc != -1) {
68 if (set->list[alloc].val == item) {
69 if (prev != -1)
70 set->list[prev].alloc_next =
71 set->list[alloc].alloc_next;
72 else
73 set->alloc = set->list[alloc].alloc_next;
74 break;
76 prev = alloc;
77 alloc = set->list[alloc].alloc_next;
80 if (alloc == -1)
81 return 0;
83 set->list[alloc].free_next = set->free;
84 set->free = alloc;
85 set->list[alloc].alloc_next = -1;
87 return 1;
90 /* setFirst -> setAdd -> setNext is wrong */
92 int via_setFirst(set_t * set, ITEM_TYPE * item)
94 if (set->alloc == -1)
95 return 0;
97 *item = set->list[set->alloc].val;
98 set->trace = set->list[set->alloc].alloc_next;
100 return 1;
103 int via_setNext(set_t * set, ITEM_TYPE * item)
105 if (set->trace == -1)
106 return 0;
108 *item = set->list[set->trace].val;
109 set->trace = set->list[set->trace].alloc_next;
111 return 1;
114 int via_setDestroy(set_t * set)
116 drm_free(set, sizeof(set_t), DRM_MEM_DRIVER);
118 return 1;
121 #define ISFREE(bptr) ((bptr)->free)
123 #define fprintf(fmt, arg...) do{}while(0)
125 memHeap_t *via_mmInit(int ofs, int size)
127 PMemBlock blocks;
129 if (size <= 0)
130 return NULL;
132 blocks = (TMemBlock *) drm_calloc(1, sizeof(TMemBlock), DRM_MEM_DRIVER);
134 if (blocks) {
135 blocks->ofs = ofs;
136 blocks->size = size;
137 blocks->free = 1;
138 return (memHeap_t *) blocks;
139 } else
140 return NULL;
143 static TMemBlock *SliceBlock(TMemBlock * p,
144 int startofs, int size,
145 int reserved, int alignment)
147 TMemBlock *newblock;
149 /* break left */
150 if (startofs > p->ofs) {
151 newblock =
152 (TMemBlock *) drm_calloc(1, sizeof(TMemBlock),
153 DRM_MEM_DRIVER);
154 newblock->ofs = startofs;
155 newblock->size = p->size - (startofs - p->ofs);
156 newblock->free = 1;
157 newblock->next = p->next;
158 p->size -= newblock->size;
159 p->next = newblock;
160 p = newblock;
163 /* break right */
164 if (size < p->size) {
165 newblock =
166 (TMemBlock *) drm_calloc(1, sizeof(TMemBlock),
167 DRM_MEM_DRIVER);
168 newblock->ofs = startofs + size;
169 newblock->size = p->size - size;
170 newblock->free = 1;
171 newblock->next = p->next;
172 p->size = size;
173 p->next = newblock;
176 /* p = middle block */
177 p->align = alignment;
178 p->free = 0;
179 p->reserved = reserved;
180 return p;
183 PMemBlock via_mmAllocMem(memHeap_t * heap, int size, int align2,
184 int startSearch)
186 int mask, startofs, endofs;
187 TMemBlock *p;
189 if (!heap || align2 < 0 || size <= 0)
190 return NULL;
192 mask = (1 << align2) - 1;
193 startofs = 0;
194 p = (TMemBlock *) heap;
196 while (p) {
197 if (ISFREE(p)) {
198 startofs = (p->ofs + mask) & ~mask;
200 if (startofs < startSearch)
201 startofs = startSearch;
203 endofs = startofs + size;
205 if (endofs <= (p->ofs + p->size))
206 break;
209 p = p->next;
212 if (!p)
213 return NULL;
215 p = SliceBlock(p, startofs, size, 0, mask + 1);
216 p->heap = heap;
218 return p;
221 static __inline__ int Join2Blocks(TMemBlock * p)
223 if (p->free && p->next && p->next->free) {
224 TMemBlock *q = p->next;
225 p->size += q->size;
226 p->next = q->next;
227 drm_free(q, sizeof(TMemBlock), DRM_MEM_DRIVER);
229 return 1;
232 return 0;
235 int via_mmFreeMem(PMemBlock b)
237 TMemBlock *p, *prev;
239 if (!b)
240 return 0;
242 if (!b->heap) {
243 fprintf(stderr, "no heap\n");
245 return -1;
248 p = b->heap;
249 prev = NULL;
251 while (p && p != b) {
252 prev = p;
253 p = p->next;
256 if (!p || p->free || p->reserved) {
257 if (!p)
258 fprintf(stderr, "block not found in heap\n");
259 else if (p->free)
260 fprintf(stderr, "block already free\n");
261 else
262 fprintf(stderr, "block is reserved\n");
264 return -1;
267 p->free = 1;
268 Join2Blocks(p);
270 if (prev)
271 Join2Blocks(prev);
273 return 0;