blob: 1a859ee8301e4ac0282af588460c06ab6d5ed786 [file] [log] [blame]
rsc2277c5d2004-03-21 04:33:13 +00001#include <u.h>
2#include <libc.h>
3#include <auth.h>
4#include <fcall.h>
5#include <thread.h>
6#include <9p.h>
7
8static void
9increqref(void *v)
10{
11 Req *r;
12
13 r = v;
14 if(r){
15if(chatty9p > 1)
16 fprint(2, "increfreq %p %ld\n", r, r->ref.ref);
17 incref(&r->ref);
18 }
19}
20
21Reqpool*
22allocreqpool(void (*destroy)(Req*))
23{
24 Reqpool *f;
25
26 f = emalloc9p(sizeof *f);
27 f->map = allocmap(increqref);
28 f->destroy = destroy;
29 return f;
30}
31
32void
33freereqpool(Reqpool *p)
34{
35 freemap(p->map, (void(*)(void*))p->destroy);
36 free(p);
37}
38
39Req*
40allocreq(Reqpool *pool, ulong tag)
41{
42 Req *r;
43
44 r = emalloc9p(sizeof *r);
45 r->tag = tag;
46 r->pool = pool;
47
48 increqref(r);
49 increqref(r);
50 if(caninsertkey(pool->map, tag, r) == 0){
51 closereq(r);
rsc5c8a0422004-12-26 21:46:26 +000052 closereq(r);
rsc2277c5d2004-03-21 04:33:13 +000053 return nil;
54 }
55
56 return r;
57}
58
59Req*
60lookupreq(Reqpool *pool, ulong tag)
61{
62if(chatty9p > 1)
63 fprint(2, "lookupreq %lud\n", tag);
64 return lookupkey(pool->map, tag);
65}
66
67void
68closereq(Req *r)
69{
rsc2277c5d2004-03-21 04:33:13 +000070 if(r == nil)
71 return;
72
73if(chatty9p > 1)
74 fprint(2, "closereq %p %ld\n", r, r->ref.ref);
75
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);
rsc90a35652006-04-19 22:04:00 +000085 if(r->nflush)
86 fprint(2, "closereq: flushes remaining\n");
rsc2277c5d2004-03-21 04:33:13 +000087 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);
102 }
103}
104
105Req*
106removereq(Reqpool *pool, ulong tag)
107{
108if(chatty9p > 1)
109 fprint(2, "removereq %lud\n", tag);
110 return deletekey(pool->map, tag);
111}