req.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. static void
  8. increqref(void *v)
  9. {
  10. Req *r;
  11. r = v;
  12. if(r){
  13. if(chatty9p > 1)
  14. fprint(2, "increfreq %p %ld\n", r, r->ref.ref);
  15. incref(&r->ref);
  16. }
  17. }
  18. Reqpool*
  19. allocreqpool(void (*destroy)(Req*))
  20. {
  21. Reqpool *f;
  22. f = emalloc9p(sizeof *f);
  23. f->map = allocmap(increqref);
  24. f->destroy = destroy;
  25. return f;
  26. }
  27. void
  28. freereqpool(Reqpool *p)
  29. {
  30. freemap(p->map, (void(*)(void*))p->destroy);
  31. free(p);
  32. }
  33. Req*
  34. allocreq(Reqpool *pool, ulong tag)
  35. {
  36. Req *r;
  37. r = emalloc9p(sizeof *r);
  38. r->tag = tag;
  39. r->pool = pool;
  40. increqref(r);
  41. increqref(r);
  42. if(caninsertkey(pool->map, tag, r) == 0){
  43. closereq(r);
  44. closereq(r);
  45. return nil;
  46. }
  47. return r;
  48. }
  49. Req*
  50. lookupreq(Reqpool *pool, ulong tag)
  51. {
  52. if(chatty9p > 1)
  53. fprint(2, "lookupreq %lud\n", tag);
  54. return lookupkey(pool->map, tag);
  55. }
  56. void
  57. closereq(Req *r)
  58. {
  59. int i;
  60. if(r == nil)
  61. return;
  62. if(chatty9p > 1)
  63. fprint(2, "closereq %p %ld\n", r, r->ref.ref);
  64. if(decref(&r->ref) == 0){
  65. if(r->fid)
  66. closefid(r->fid);
  67. if(r->newfid)
  68. closefid(r->newfid);
  69. if(r->afid)
  70. closefid(r->afid);
  71. if(r->oldreq)
  72. closereq(r->oldreq);
  73. for(i=0; i<r->nflush; i++)
  74. respond(r->flush[i], nil);
  75. free(r->flush);
  76. switch(r->ifcall.type){
  77. case Tstat:
  78. free(r->ofcall.stat);
  79. free(r->d.name);
  80. free(r->d.uid);
  81. free(r->d.gid);
  82. free(r->d.muid);
  83. break;
  84. }
  85. if(r->pool->destroy)
  86. r->pool->destroy(r);
  87. free(r->buf);
  88. free(r->rbuf);
  89. free(r);
  90. }
  91. }
  92. Req*
  93. removereq(Reqpool *pool, ulong tag)
  94. {
  95. if(chatty9p > 1)
  96. fprint(2, "removereq %lud\n", tag);
  97. return deletekey(pool->map, tag);
  98. }