fid.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <auth.h>
  12. #include <fcall.h>
  13. #include <thread.h>
  14. #include "9p.h"
  15. static void
  16. incfidref(void *v)
  17. {
  18. Fid *f;
  19. f = v;
  20. if(f)
  21. incref(&f->ref);
  22. }
  23. Fidpool*
  24. allocfidpool(void (*destroy)(Fid*))
  25. {
  26. Fidpool *f;
  27. f = emalloc9p(sizeof *f);
  28. f->map = allocmap(incfidref);
  29. f->destroy = destroy;
  30. return f;
  31. }
  32. void
  33. freefidpool(Fidpool *p)
  34. {
  35. freemap(p->map, (void(*)(void*))p->destroy);
  36. free(p);
  37. }
  38. Fid*
  39. allocfid(Fidpool *pool, uint32_t fid)
  40. {
  41. Fid *f;
  42. f = emalloc9p(sizeof *f);
  43. f->fid = fid;
  44. f->omode = -1;
  45. f->pool = pool;
  46. incfidref(f);
  47. incfidref(f);
  48. if(caninsertkey(pool->map, fid, f) == 0){
  49. closefid(f);
  50. closefid(f);
  51. return nil;
  52. }
  53. return f;
  54. }
  55. Fid*
  56. lookupfid(Fidpool *pool, uint32_t fid)
  57. {
  58. return lookupkey(pool->map, fid);
  59. }
  60. void
  61. closefid(Fid *f)
  62. {
  63. if(decref(&f->ref) == 0) {
  64. if(f->rdir)
  65. closedirfile(f->rdir);
  66. if(f->pool->destroy)
  67. f->pool->destroy(f);
  68. if(f->file)
  69. closefile(f->file);
  70. free(f->uid);
  71. free(f);
  72. }
  73. }
  74. Fid*
  75. removefid(Fidpool *pool, uint32_t fid)
  76. {
  77. return deletekey(pool->map, fid);
  78. }