rsc | 2277c5d | 2004-03-21 04:33:13 +0000 | [diff] [blame] | 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 | |
| 8 | static void |
| 9 | increqref(void *v) |
| 10 | { |
| 11 | Req *r; |
| 12 | |
| 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 | } |
| 20 | |
| 21 | Reqpool* |
| 22 | allocreqpool(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 | |
| 32 | void |
| 33 | freereqpool(Reqpool *p) |
| 34 | { |
| 35 | freemap(p->map, (void(*)(void*))p->destroy); |
| 36 | free(p); |
| 37 | } |
| 38 | |
| 39 | Req* |
| 40 | allocreq(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); |
rsc | 5c8a042 | 2004-12-26 21:46:26 +0000 | [diff] [blame] | 52 | closereq(r); |
rsc | 2277c5d | 2004-03-21 04:33:13 +0000 | [diff] [blame] | 53 | return nil; |
| 54 | } |
| 55 | |
| 56 | return r; |
| 57 | } |
| 58 | |
| 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 | } |
| 66 | |
| 67 | void |
| 68 | closereq(Req *r) |
| 69 | { |
rsc | 2277c5d | 2004-03-21 04:33:13 +0000 | [diff] [blame] | 70 | if(r == nil) |
| 71 | return; |
| 72 | |
| 73 | if(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); |
rsc | 90a3565 | 2006-04-19 22:04:00 +0000 | [diff] [blame] | 85 | if(r->nflush) |
| 86 | fprint(2, "closereq: flushes remaining\n"); |
rsc | 2277c5d | 2004-03-21 04:33:13 +0000 | [diff] [blame] | 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); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | Req* |
| 106 | removereq(Reqpool *pool, ulong tag) |
| 107 | { |
| 108 | if(chatty9p > 1) |
| 109 | fprint(2, "removereq %lud\n", tag); |
| 110 | return deletekey(pool->map, tag); |
| 111 | } |