Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ring.h 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*****************************************************************************
  2. Copyright (c) 2003 Zope Foundation and Contributors.
  3. All Rights Reserved.
  4. This software is subject to the provisions of the Zope Public License,
  5. Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  7. WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  8. WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  9. FOR A PARTICULAR PURPOSE
  10. ****************************************************************************/
  11. /* Support routines for the doubly-linked list of cached objects.
  12. The cache stores a headed, doubly-linked, circular list of persistent
  13. objects, with space for the pointers allocated in the objects themselves.
  14. The cache stores the distinguished head of the list, which is not a valid
  15. persistent object. The other list members are non-ghost persistent
  16. objects, linked in LRU (least-recently used) order.
  17. The r_next pointers traverse the ring starting with the least recently used
  18. object. The r_prev pointers traverse the ring starting with the most
  19. recently used object.
  20. Obscure: While each object is pointed at twice by list pointers (once by
  21. its predecessor's r_next, again by its successor's r_prev), the refcount
  22. on the object is bumped only by 1. This leads to some possibly surprising
  23. sequences of incref and decref code. Note that since the refcount is
  24. bumped at least once, the list does hold a strong reference to each
  25. object in it.
  26. */
  27. typedef struct CPersistentRing_struct
  28. {
  29. struct CPersistentRing_struct *r_prev;
  30. struct CPersistentRing_struct *r_next;
  31. } CPersistentRing;
  32. /* The list operations here take constant time independent of the
  33. * number of objects in the list:
  34. */
  35. /* Add elt as the most recently used object. elt must not already be
  36. * in the list, although this isn't checked.
  37. */
  38. void ring_add(CPersistentRing *ring, CPersistentRing *elt);
  39. /* Remove elt from the list. elt must already be in the list, although
  40. * this isn't checked.
  41. */
  42. void ring_del(CPersistentRing *elt);
  43. /* elt must already be in the list, although this isn't checked. It's
  44. * unlinked from its current position, and relinked into the list as the
  45. * most recently used object (which is arguably the tail of the list
  46. * instead of the head -- but the name of this function could be argued
  47. * either way). This is equivalent to
  48. *
  49. * ring_del(elt);
  50. * ring_add(ring, elt);
  51. *
  52. * but may be a little quicker.
  53. */
  54. void ring_move_to_head(CPersistentRing *ring, CPersistentRing *elt);