lru.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. /*
  10. * lru lists are circular with a list head
  11. * pointing to the start and end of the list
  12. */
  13. #include <u.h>
  14. #include "lru.h"
  15. /*
  16. * Create an lru chain of buffers
  17. */
  18. void
  19. lruinit(Lru *h)
  20. {
  21. h->lprev = h->lnext = h;
  22. }
  23. /*
  24. * Add a member to an lru chain
  25. */
  26. void
  27. lruadd(Lru *h, Lru *m)
  28. {
  29. h->lprev->lnext = m;
  30. m->lprev = h->lprev;
  31. h->lprev = m;
  32. m->lnext = h;
  33. }
  34. /*
  35. * Move to end of lru list
  36. */
  37. void
  38. lruref(Lru *h, Lru *m)
  39. {
  40. if(h->lprev == m)
  41. return; /* alread at end of list */
  42. /*
  43. * remove from list
  44. */
  45. m->lprev->lnext = m->lnext;
  46. m->lnext->lprev = m->lprev;
  47. /*
  48. * add in at end
  49. */
  50. h->lprev->lnext = m;
  51. m->lprev = h->lprev;
  52. h->lprev = m;
  53. m->lnext = h;
  54. }
  55. /*
  56. * Move to head of lru list
  57. */
  58. void
  59. lruderef(Lru *h, Lru *m)
  60. {
  61. if(h->lnext == m)
  62. return; /* alread at head of list */
  63. /*
  64. * remove from list
  65. */
  66. m->lprev->lnext = m->lnext;
  67. m->lnext->lprev = m->lprev;
  68. /*
  69. * add in at head
  70. */
  71. h->lnext->lprev = m;
  72. m->lnext = h->lnext;
  73. h->lnext = m;
  74. m->lprev = h;
  75. }