xref: /plan9/sys/src/lib9p/req.c (revision 59cc4ca53493a3c6d2349fe2b7f7c40f7dce7294)
1 #include <u.h>
2 #include <libc.h>
3 #include <auth.h>
4 #include <fcall.h>
5 #include <thread.h>
6 #include "9p.h"
7 #include "impl.h"
8 
9 static void
10 increqref(void *v)
11 {
12 	Req *r;
13 
14 	r = v;
15 	if(r)
16 		incref(&r->ref);
17 }
18 
19 Req*
20 allocreq(Reqpool *pool, ulong tag)
21 {
22 	Req *r;
23 
24 	if((r = mallocz(sizeof *r, 1)) == nil)
25 		return nil;
26 
27 	r->tag = tag;
28 	r->pool = pool;
29 
30 	incref(&r->ref);
31 	if(caninsertkey(pool->map, tag, r) == 0){
32 		closereq(r);
33 		return nil;
34 	}
35 
36 	return r;
37 }
38 
39 int
40 closereq(Req *r)
41 {
42 	int n;
43 
44 	if((n = decref(&r->ref)) == 0){
45 		free(r->buf);
46 		free(r->rbuf);
47 		free(r);
48 	}
49 //print("closereq %p: %d refs\n", r, n ? r->ref.ref : n);
50 	return n;
51 }
52 
53 void
54 freereq(Req *r)
55 {
56 	Req *nr;
57 
58 	if(r == nil)
59 		return;
60 
61 	nr = deletekey(r->pool->map, r->tag);
62 if(r != nr)
63 	fprint(2, "r %p nr %p\n", r, nr);
64 
65 	assert(r == nr);
66 
67 	if(closereq(r) == 0)	/* intmap reference */
68 		abort();
69 	closereq(r);
70 }
71 
72 Req*
73 lookupreq(Reqpool *pool, ulong req)
74 {
75 	return lookupkey(pool->map, req);
76 }
77 
78 Reqpool*
79 allocreqpool(void)
80 {
81 	Reqpool *f;
82 
83 	if((f = mallocz(sizeof *f, 1)) == nil)
84 		return nil;
85 	if((f->map = allocmap(increqref)) == nil){
86 		free(f);
87 		return nil;
88 	}
89 	return f;
90 }
91 
92