req.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. return nil;
  45. }
  46. return r;
  47. }
  48. Req*
  49. lookupreq(Reqpool *pool, ulong tag)
  50. {
  51. if(chatty9p > 1)
  52. fprint(2, "lookupreq %lud\n", tag);
  53. return lookupkey(pool->map, tag);
  54. }
  55. void
  56. closereq(Req *r)
  57. {
  58. int i;
  59. if(r == nil)
  60. return;
  61. if(chatty9p > 1)
  62. fprint(2, "closereq %p %ld\n", r, r->ref.ref);
  63. if(decref(&r->ref) == 0){
  64. if(r->fid)
  65. closefid(r->fid);
  66. if(r->newfid)
  67. closefid(r->newfid);
  68. if(r->afid)
  69. closefid(r->afid);
  70. if(r->oldreq)
  71. closereq(r->oldreq);
  72. for(i=0; i<r->nflush; i++)
  73. respond(r->flush[i], nil);
  74. free(r->flush);
  75. switch(r->ifcall.type){
  76. case Tstat:
  77. free(r->ofcall.stat);
  78. free(r->d.name);
  79. free(r->d.uid);
  80. free(r->d.gid);
  81. free(r->d.muid);
  82. break;
  83. }
  84. if(r->pool->destroy)
  85. r->pool->destroy(r);
  86. free(r->buf);
  87. free(r->rbuf);
  88. free(r);
  89. }
  90. }
  91. Req*
  92. removereq(Reqpool *pool, ulong tag)
  93. {
  94. if(chatty9p > 1)
  95. fprint(2, "removereq %lud\n", tag);
  96. return deletekey(pool->map, tag);
  97. }