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