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