xref: /plan9-contrib/sys/src/lib9p/req.c (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
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);
47 	}
48 //print("closereq %p: %d refs\n", r, n ? r->ref.ref : n);
49 	return n;
50 }
51 
52 void
53 freereq(Req *r)
54 {
55 	Req *nr;
56 
57 	if(r == nil)
58 		return;
59 
60 	nr = deletekey(r->pool->map, r->tag);
61 	assert(r == nr);
62 
63 	if(closereq(r) == 0)	/* intmap reference */
64 		abort();
65 	closereq(r);
66 }
67 
68 Req*
69 lookupreq(Reqpool *pool, ulong req)
70 {
71 	return lookupkey(pool->map, req);
72 }
73 
74 Reqpool*
75 allocreqpool(void)
76 {
77 	Reqpool *f;
78 
79 	if((f = mallocz(sizeof *f, 1)) == nil)
80 		return nil;
81 	if((f->map = allocmap(increqref)) == nil){
82 		free(f);
83 		return nil;
84 	}
85 	return f;
86 }
87 
88