This source file includes following definitions.
- BufPoolInit
- BufPoolAdd
- BufPoolFree
- BufPoolGet
- BufPoolRelease
- BufQueueLink
- BufQueueLinkFront
- BufQueueUnlink
- BufQueueInit
- BufQueueRelease
- BufQueueLength
- BufQueueDiscard
- Sfree
- Smalloc
1
2
3
4
5
6
7
8
9 #define __NO_VERSION__
10 #include "teles.h"
11 #include <linux/mm.h>
12 #include <linux/malloc.h>
13
14
15 void
16 BufPoolInit(struct BufPool *bp, int order, int bpps,
17 int maxpages)
18 {
19 #ifdef DEBUG_MAGIC
20 generateerror
21 bp->magic = 010167;
22 #endif
23
24 #if 0
25 printk(KERN_DEBUG "BufPoolInit bp %x\n", bp);
26 #endif
27
28 bp->freelist = NULL;
29 bp->pageslist = NULL;
30 bp->pageorder = order;
31 bp->pagescount = 0;
32 bp->bpps = bpps;
33 bp->bufsize = BUFFER_SIZE(order, bpps);
34 bp->maxpages = maxpages;
35 }
36
37 int
38 BufPoolAdd(struct BufPool *bp, int priority)
39 {
40 struct Pages *ptr;
41 long flags;
42 byte *bptr;
43 int i;
44 struct BufHeader *bh = NULL, *prev, *first;
45
46 #if 0
47 printk(KERN_DEBUG "BufPoolAdd bp %x\n", bp);
48 #endif
49
50 ptr = (struct Pages *) __get_free_pages(priority,bp->pageorder,0);
51 if (!ptr) {
52 printk(KERN_WARNING "BufPoolAdd couldn't get pages!\n");
53 return (-1);
54 }
55 #if 0
56 printk(KERN_DEBUG "Order %d pages allocated at %x\n", bp->pageorder, ptr);
57 #endif
58
59 ptr->next = bp->pageslist;
60 bp->pageslist = ptr;
61 bp->pagescount++;
62
63 bptr = (byte *) ptr + sizeof(struct Pages *);
64
65 i = bp->bpps;
66 first = (struct BufHeader *) bptr;
67 prev = NULL;
68 while (i--) {
69 bh = (struct BufHeader *) bptr;
70 #ifdef DEBUG_MAGIC
71 bh->magic = 020167;
72 #endif
73 bh->next = prev;
74 prev = bh;
75 bh->bp = bp;
76 bptr += PART_SIZE(bp->pageorder, bp->bpps);
77 }
78
79 save_flags(flags);
80 cli();
81 first->next = bp->freelist;
82 bp->freelist = bh;
83 restore_flags(flags);
84 return (0);
85 }
86
87 void
88 BufPoolFree(struct BufPool *bp)
89 {
90 struct Pages *p;
91
92 #if 0
93 printk(KERN_DEBUG "BufPoolFree bp %x\n", bp);
94 #endif
95
96 while (bp->pagescount--) {
97 p = bp->pageslist->next;
98 free_pages((unsigned long) bp->pageslist, bp->pageorder);
99 #if 0
100 printk(KERN_DEBUG "Free pages %x order %d\n", bp->pageslist, bp->pageorder);
101 #endif
102 bp->pageslist = p;
103 }
104 }
105
106 int
107 BufPoolGet(struct BufHeader **bh,
108 struct BufPool *bp, int priority, void *heldby, int where)
109 {
110 long flags;
111 int i;
112
113 #ifdef DEBUG_MAGIC
114 if (bp->magic != 010167) {
115 printk(KERN_DEBUG "BufPoolGet: not a BufHeader\n");
116 return (-1);
117 }
118 #endif
119
120 save_flags(flags);
121 cli();
122 i = 0;
123 while (!0) {
124 if (bp->freelist) {
125 *bh = bp->freelist;
126 bp->freelist = bp->freelist->next;
127 (*bh)->heldby = heldby;
128 (*bh)->where = where;
129 restore_flags(flags);
130 return (0);
131 }
132 if ((i == 0) && (bp->pagescount < bp->maxpages)) {
133 if (BufPoolAdd(bp, priority)) {
134 restore_flags(flags);
135 return -1;
136 }
137 i++;
138 } else {
139 *bh = NULL;
140 restore_flags(flags);
141 return (-1);
142 }
143 }
144
145 }
146
147 void
148 BufPoolRelease(struct BufHeader *bh)
149 {
150 struct BufPool *bp;
151 long flags;
152
153 #ifdef DEBUG_MAGIC
154 if (bh->magic != 020167) {
155 printk(KERN_DEBUG "BufPoolRelease: not a BufHeader\n");
156 printk(KERN_DEBUG "called from %x\n", __builtin_return_address(0));
157 return;
158 }
159 #endif
160
161 bp = bh->bp;
162
163 #ifdef DEBUG_MAGIC
164 if (bp->magic != 010167) {
165 printk(KERN_DEBUG "BufPoolRelease: not a BufPool\n");
166 return;
167 }
168 #endif
169
170 save_flags(flags);
171 cli();
172 bh->next = bp->freelist;
173 bp->freelist = bh;
174 restore_flags(flags);
175 }
176
177 void
178 BufQueueLink(struct BufQueue *bq,
179 struct BufHeader *bh)
180 {
181 unsigned long flags;
182
183 save_flags(flags);
184 cli();
185 if (!bq->head)
186 bq->head = bh;
187 if (bq->tail)
188 bq->tail->next = bh;
189 bq->tail = bh;
190 bh->next = NULL;
191 restore_flags(flags);
192 }
193
194 void
195 BufQueueLinkFront(struct BufQueue *bq,
196 struct BufHeader *bh)
197 {
198 unsigned long flags;
199
200 save_flags(flags);
201 cli();
202 bh->next = bq->head;
203 bq->head = bh;
204 if (!bq->tail)
205 bq->tail = bh;
206 restore_flags(flags);
207 }
208
209 int
210 BufQueueUnlink(struct BufHeader **bh, struct BufQueue *bq)
211 {
212 long flags;
213
214 save_flags(flags);
215 cli();
216
217 if (bq->head) {
218 if (bq->tail == bq->head)
219 bq->tail = NULL;
220 *bh = bq->head;
221 bq->head = (*bh)->next;
222 restore_flags(flags);
223 return (0);
224 } else {
225 restore_flags(flags);
226 return (-1);
227 }
228 }
229
230 void
231 BufQueueInit(struct BufQueue *bq)
232 {
233 #ifdef DEBUG_MAGIC
234 bq->magic = 030167;
235 #endif
236 bq->head = NULL;
237 bq->tail = NULL;
238 }
239
240 void
241 BufQueueRelease(struct BufQueue *bq)
242 {
243 struct BufHeader *bh;
244
245 while (bq->head) {
246 BufQueueUnlink(&bh, bq);
247 BufPoolRelease(bh);
248 }
249 }
250
251 int
252 BufQueueLength(struct BufQueue *bq)
253 {
254 int i = 0;
255 struct BufHeader *bh;
256
257 bh = bq->head;
258 while (bh) {
259 i++;
260 bh = bh->next;
261 }
262 return (i);
263 }
264
265 void
266 BufQueueDiscard(struct BufQueue *q, int pr, void *heldby,
267 int releasetoo)
268 {
269 long flags;
270 struct BufHeader *sp;
271
272 save_flags(flags);
273 cli();
274
275 while (!0) {
276 sp = q->head;
277 if (!sp)
278 break;
279 if ((sp->primitive == pr) && (sp->heldby == heldby)) {
280 q->head = sp->next;
281 if (q->tail == sp)
282 q->tail = NULL;
283 if (releasetoo)
284 BufPoolRelease(sp);
285 } else
286 break;
287 }
288
289 sp = q->head;
290 if (sp)
291 while (sp->next) {
292 if ((sp->next->primitive == pr) && (sp->next->heldby == heldby)) {
293 if (q->tail == sp->next)
294 q->tail = sp;
295 if (releasetoo)
296 BufPoolRelease(sp->next);
297 sp->next = sp->next->next;
298 } else
299 sp = sp->next;
300 }
301 restore_flags(flags);
302 }
303
304 void
305 Sfree(byte * ptr)
306 {
307 #if 0
308 printk(KERN_DEBUG "Sfree %x\n", ptr);
309 #endif
310 kfree(ptr);
311 }
312
313 byte *
314 Smalloc(int size, int pr, char *why)
315 {
316 byte *p;
317
318 p = (byte *) kmalloc(size, pr);
319 #if 0
320 printk(KERN_DEBUG "Smalloc %s size %d res %x\n", why, size, p);
321 #endif
322 return (p);
323 }