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