]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/port/allocb.c
merge
[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         Tlrspc          = 16,           /* extra room at the end for pad/crc/mac */
12         Bdead           = 0x51494F42,   /* "QIOB" */
13 };
14
15 static Block*
16 _allocb(int size)
17 {
18         Block *b;
19         uintptr addr;
20
21         size += Tlrspc;
22         size = ROUND(size, BLOCKALIGN);
23         if((b = mallocz(sizeof(Block)+BLOCKALIGN+Hdrspc+size, 0)) == nil)
24                 return nil;
25
26         b->next = nil;
27         b->list = nil;
28         b->free = nil;
29         b->flag = 0;
30
31         /* align start of data portion by rounding up */
32         addr = (uintptr)b;
33         addr = ROUND(addr + sizeof(Block), BLOCKALIGN);
34         b->base = (uchar*)addr;
35
36         /* align end of data portion by rounding down */
37         b->lim = (uchar*)b + msize(b);
38         addr = (uintptr)b->lim;
39         addr &= ~(BLOCKALIGN-1);
40         b->lim = (uchar*)addr;
41
42         /* leave room at beginning for added headers */
43         b->wp = b->rp = b->lim - size;
44
45         return b;
46 }
47
48 Block*
49 allocb(int size)
50 {
51         Block *b;
52
53         /*
54          * Check in a process and wait until successful.
55          */
56         if(up == nil)
57                 panic("allocb without up: %#p", getcallerpc(&size));
58         while((b = _allocb(size)) == nil){
59                 if(up->nlocks || m->ilockdepth || !islo()){
60                         xsummary();
61                         mallocsummary();
62                         panic("allocb: no memory for %d bytes", size);
63                 }
64                 if(!waserror()){
65                         resrcwait("no memory for allocb");
66                         poperror();
67                 }
68         }
69         setmalloctag(b, getcallerpc(&size));
70
71         return b;
72 }
73
74 Block*
75 iallocb(int size)
76 {
77         Block *b;
78
79         if((b = _allocb(size)) == nil){
80                 static ulong nerr;
81                 if((nerr++%10000)==0){
82                         if(nerr > 10000000){
83                                 xsummary();
84                                 mallocsummary();
85                                 panic("iallocb: out of memory");
86                         }
87                         iprint("iallocb: no memory for %d bytes\n", size);
88                 }
89                 return nil;
90         }
91         setmalloctag(b, getcallerpc(&size));
92         b->flag = BINTR;
93
94         return b;
95 }
96
97 void
98 freeb(Block *b)
99 {
100         void *dead = (void*)Bdead;
101
102         if(b == nil)
103                 return;
104
105         /*
106          * drivers which perform non cache coherent DMA manage their own buffer
107          * pool of uncached buffers and provide their own free routine.
108          */
109         if(b->free != nil) {
110                 b->free(b);
111                 return;
112         }
113
114         /* poison the block in case someone is still holding onto it */
115         b->next = dead;
116         b->rp = dead;
117         b->wp = dead;
118         b->lim = dead;
119         b->base = dead;
120
121         free(b);
122 }
123
124 void
125 checkb(Block *b, char *msg)
126 {
127         void *dead = (void*)Bdead;
128
129         if(b == dead)
130                 panic("checkb b %s %#p", msg, b);
131         if(b->base == dead || b->lim == dead || b->next == dead
132           || b->rp == dead || b->wp == dead){
133                 print("checkb: base %#p lim %#p next %#p\n",
134                         b->base, b->lim, b->next);
135                 print("checkb: rp %#p wp %#p\n", b->rp, b->wp);
136                 panic("checkb dead: %s", msg);
137         }
138
139         if(b->base > b->lim)
140                 panic("checkb 0 %s %#p %#p", msg, b->base, b->lim);
141         if(b->rp < b->base)
142                 panic("checkb 1 %s %#p %#p", msg, b->base, b->rp);
143         if(b->wp < b->base)
144                 panic("checkb 2 %s %#p %#p", msg, b->base, b->wp);
145         if(b->rp > b->lim)
146                 panic("checkb 3 %s %#p %#p", msg, b->rp, b->lim);
147         if(b->wp > b->lim)
148                 panic("checkb 4 %s %#p %#p", msg, b->wp, b->lim);
149 }