aboutsummaryrefslogtreecommitdiff
path: root/src/lib9p/req.c
blob: 8e1aaab5480d6fab92a9d4c5c3687dd855386bb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <u.h>
#include <libc.h>
#include <auth.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>

static void
increqref(void *v)
{
	Req *r;

	r = v;
	if(r){
if(chatty9p > 1)
	fprint(2, "increfreq %p %ld\n", r, r->ref.ref);
		incref(&r->ref);
	}
}

Reqpool*
allocreqpool(void (*destroy)(Req*))
{
	Reqpool *f;

	f = emalloc9p(sizeof *f);
	f->map = allocmap(increqref);
	f->destroy = destroy;
	return f;
}

void
freereqpool(Reqpool *p)
{
	freemap(p->map, (void(*)(void*))p->destroy);
	free(p);
}	

Req*
allocreq(Reqpool *pool, ulong tag)
{
	Req *r;

	r = emalloc9p(sizeof *r);
	r->tag = tag;
	r->pool = pool;

	increqref(r);
	increqref(r);
	if(caninsertkey(pool->map, tag, r) == 0){
		closereq(r);
		return nil;
	}

	return r;
}

Req*
lookupreq(Reqpool *pool, ulong tag)
{
if(chatty9p > 1)
	fprint(2, "lookupreq %lud\n", tag);
	return lookupkey(pool->map, tag);
}

void
closereq(Req *r)
{
	int i;

	if(r == nil)
		return;

if(chatty9p > 1)
	fprint(2, "closereq %p %ld\n", r, r->ref.ref);

	if(decref(&r->ref) == 0){
		if(r->fid)
			closefid(r->fid);
		if(r->newfid)
			closefid(r->newfid);
		if(r->afid)
			closefid(r->afid);
		if(r->oldreq)
			closereq(r->oldreq);
		for(i=0; i<r->nflush; i++)
			respond(r->flush[i], nil);
		free(r->flush);
		switch(r->ifcall.type){
		case Tstat:
			free(r->ofcall.stat);
			free(r->d.name);
			free(r->d.uid);
			free(r->d.gid);
			free(r->d.muid);
			break;
		}
		if(r->pool->destroy)
			r->pool->destroy(r);
		free(r->buf);
		free(r->rbuf);
		free(r);
	}
}

Req*
removereq(Reqpool *pool, ulong tag)
{
if(chatty9p > 1)
	fprint(2, "removereq %lud\n", tag);
	return deletekey(pool->map, tag);
}