Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <auth.h>
4 #include <fcall.h>
5 #include <thread.h>
6 #include <9p.h>
8 static void
9 increqref(void *v)
10 {
11 Req *r;
13 r = v;
14 if(r){
15 if(chatty9p > 1)
16 fprint(2, "increfreq %p %ld\n", r, r->ref.ref);
17 incref(&r->ref);
18 }
19 }
21 Reqpool*
22 allocreqpool(void (*destroy)(Req*))
23 {
24 Reqpool *f;
26 f = emalloc9p(sizeof *f);
27 f->map = allocmap(increqref);
28 f->destroy = destroy;
29 return f;
30 }
32 void
33 freereqpool(Reqpool *p)
34 {
35 freemap(p->map, (void(*)(void*))p->destroy);
36 free(p);
37 }
39 Req*
40 allocreq(Reqpool *pool, ulong tag)
41 {
42 Req *r;
44 r = emalloc9p(sizeof *r);
45 r->tag = tag;
46 r->pool = pool;
48 increqref(r);
49 increqref(r);
50 if(caninsertkey(pool->map, tag, r) == 0){
51 closereq(r);
52 closereq(r);
53 return nil;
54 }
56 return r;
57 }
59 Req*
60 lookupreq(Reqpool *pool, ulong tag)
61 {
62 if(chatty9p > 1)
63 fprint(2, "lookupreq %lud\n", tag);
64 return lookupkey(pool->map, tag);
65 }
67 void
68 closereq(Req *r)
69 {
70 if(r == nil)
71 return;
73 if(chatty9p > 1)
74 fprint(2, "closereq %p %ld\n", r, r->ref.ref);
76 if(decref(&r->ref) == 0){
77 if(r->fid)
78 closefid(r->fid);
79 if(r->newfid)
80 closefid(r->newfid);
81 if(r->afid)
82 closefid(r->afid);
83 if(r->oldreq)
84 closereq(r->oldreq);
85 if(r->nflush)
86 fprint(2, "closereq: flushes remaining\n");
87 free(r->flush);
88 switch(r->ifcall.type){
89 case Tstat:
90 free(r->ofcall.stat);
91 free(r->d.name);
92 free(r->d.uid);
93 free(r->d.gid);
94 free(r->d.muid);
95 break;
96 }
97 if(r->pool->destroy)
98 r->pool->destroy(r);
99 free(r->buf);
100 free(r->rbuf);
101 free(r);
105 Req*
106 removereq(Reqpool *pool, ulong tag)
108 if(chatty9p > 1)
109 fprint(2, "removereq %lud\n", tag);
110 return deletekey(pool->map, tag);