req.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. if(r == nil)
  59. return;
  60. if(chatty9p > 1)
  61. fprint(2, "closereq %p %ld\n", r, r->ref.ref);
  62. if(decref(&r->ref) == 0){
  63. if(r->fid)
  64. closefid(r->fid);
  65. if(r->newfid)
  66. closefid(r->newfid);
  67. if(r->oldreq)
  68. closereq(r->oldreq);
  69. switch(r->ifcall.type){
  70. case Tstat:
  71. free(r->ofcall.stat);
  72. free(r->d.name);
  73. free(r->d.uid);
  74. free(r->d.gid);
  75. free(r->d.muid);
  76. break;
  77. }
  78. if(r->pool->destroy)
  79. r->pool->destroy(r);
  80. free(r->buf);
  81. free(r->rbuf);
  82. free(r);
  83. }
  84. }
  85. Req*
  86. removereq(Reqpool *pool, ulong tag)
  87. {
  88. if(chatty9p > 1)
  89. fprint(2, "removereq %lud\n", tag);
  90. return deletekey(pool->map, tag);
  91. }