]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/port/allocb.c
kernel: implement separate wait queues for page allocation
[plan9front.git] / sys / src / 9 / port / allocb.c
1 #include        "u.h"
2 #include        "../port/lib.h"
3 #include        "mem.h"
4 #include        "dat.h"
5 #include        "fns.h"
6 #include        "error.h"
7
8 enum
9 {
10         Hdrspc          = 64,           /* leave room for high-level headers */
11         Bdead           = 0x51494F42,   /* "QIOB" */
12 };
13
14 struct
15 {
16         Lock;
17         ulong   bytes;
18 } ialloc;
19
20 static Block*
21 _allocb(int size)
22 {
23         Block *b;
24         uintptr addr;
25
26         if((b = mallocz(sizeof(Block)+size+Hdrspc, 0)) == nil)
27                 return nil;
28
29         b->next = nil;
30         b->list = nil;
31         b->free = nil;
32         b->flag = 0;
33
34         /* align start of data portion by rounding up */
35         addr = (uintptr)b;
36         addr = ROUND(addr + sizeof(Block), BLOCKALIGN);
37         b->base = (uchar*)addr;
38
39         /* align end of data portion by rounding down */
40         b->lim = (uchar*)b + msize(b);
41         addr = (uintptr)b->lim;
42         addr &= ~(BLOCKALIGN-1);
43         b->lim = (uchar*)addr;
44
45         /* leave sluff at beginning for added headers */
46         b->rp = b->lim - ROUND(size, BLOCKALIGN);
47         if(b->rp < b->base)
48                 panic("_allocb");
49         b->wp = b->rp;
50
51         return b;
52 }
53
54 Block*
55 allocb(int size)
56 {
57         Block *b;
58
59         /*
60          * Check in a process and wait until successful.
61          * Can still error out of here, though.
62          */
63         if(up == nil)
64                 panic("allocb without up: %#p", getcallerpc(&size));
65         while((b = _allocb(size)) == nil){
66                 if(up->nlocks || m->ilockdepth || !islo()){
67                         xsummary();
68                         mallocsummary();
69                         panic("allocb: no memory for %d bytes", size);
70                 }
71                 if(!waserror()){
72                         resrcwait("no memory for allocb");
73                         poperror();
74                 }
75         }
76         setmalloctag(b, getcallerpc(&size));
77
78         return b;
79 }
80
81 Block*
82 iallocb(int size)
83 {
84         Block *b;
85         static int m1, m2, mp;
86
87         if(ialloc.bytes > conf.ialloc){
88                 if((m1++%10000)==0){
89                         if(mp++ > 1000){
90                                 active.exiting = 1;
91                                 exit(0);
92                         }
93                         iprint("iallocb: limited %lud/%lud\n",
94                                 ialloc.bytes, conf.ialloc);
95                 }
96                 return nil;
97         }
98
99         if((b = _allocb(size)) == nil){
100                 if((m2++%10000)==0){
101                         if(mp++ > 1000){
102                                 active.exiting = 1;
103                                 exit(0);
104                         }
105                         iprint("iallocb: no memory %lud/%lud\n",
106                                 ialloc.bytes, conf.ialloc);
107                 }
108                 return nil;
109         }
110         setmalloctag(b, getcallerpc(&size));
111         b->flag = BINTR;
112
113         ilock(&ialloc);
114         ialloc.bytes += b->lim - b->base;
115         iunlock(&ialloc);
116
117         return b;
118 }
119
120 void
121 freeb(Block *b)
122 {
123         void *dead = (void*)Bdead;
124
125         if(b == nil)
126                 return;
127
128         /*
129          * drivers which perform non cache coherent DMA manage their own buffer
130          * pool of uncached buffers and provide their own free routine.
131          */
132         if(b->free != nil) {
133                 b->free(b);
134                 return;
135         }
136         if(b->flag & BINTR) {
137                 ilock(&ialloc);
138                 ialloc.bytes -= b->lim - b->base;
139                 iunlock(&ialloc);
140         }
141
142         /* poison the block in case someone is still holding onto it */
143         b->next = dead;
144         b->rp = dead;
145         b->wp = dead;
146         b->lim = dead;
147         b->base = dead;
148
149         free(b);
150 }
151
152 void
153 checkb(Block *b, char *msg)
154 {
155         void *dead = (void*)Bdead;
156
157         if(b == dead)
158                 panic("checkb b %s %#p", msg, b);
159         if(b->base == dead || b->lim == dead || b->next == dead
160           || b->rp == dead || b->wp == dead){
161                 print("checkb: base %#p lim %#p next %#p\n",
162                         b->base, b->lim, b->next);
163                 print("checkb: rp %#p wp %#p\n", b->rp, b->wp);
164                 panic("checkb dead: %s", msg);
165         }
166
167         if(b->base > b->lim)
168                 panic("checkb 0 %s %#p %#p", msg, b->base, b->lim);
169         if(b->rp < b->base)
170                 panic("checkb 1 %s %#p %#p", msg, b->base, b->rp);
171         if(b->wp < b->base)
172                 panic("checkb 2 %s %#p %#p", msg, b->base, b->wp);
173         if(b->rp > b->lim)
174                 panic("checkb 3 %s %#p %#p", msg, b->rp, b->lim);
175         if(b->wp > b->lim)
176                 panic("checkb 4 %s %#p %#p", msg, b->wp, b->lim);
177 }
178
179 void
180 iallocsummary(void)
181 {
182         print("ialloc %lud/%lud\n", ialloc.bytes, conf.ialloc);
183 }