kref.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * kref.h - library routines for handling generic reference counted objects
  3. *
  4. * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
  5. * Copyright (C) 2004 IBM Corp.
  6. *
  7. * based on kobject.h which was:
  8. * Copyright (C) 2002-2003 Patrick Mochel <mochel@osdl.org>
  9. * Copyright (C) 2002-2003 Open Source Development Labs
  10. *
  11. * This file is released under the GPLv2.
  12. *
  13. */
  14. #ifndef _KREF_H_
  15. #define _KREF_H_
  16. #include <linux/bug.h>
  17. #include <linux/atomic.h>
  18. #include <linux/kernel.h>
  19. struct kref {
  20. atomic_t refcount;
  21. };
  22. /**
  23. * kref_init - initialize object.
  24. * @kref: object in question.
  25. */
  26. static inline void kref_init(struct kref *kref)
  27. {
  28. atomic_set(&kref->refcount, 1);
  29. }
  30. /**
  31. * kref_get - increment refcount for object.
  32. * @kref: object.
  33. */
  34. static inline void kref_get(struct kref *kref)
  35. {
  36. WARN_ON(!atomic_read(&kref->refcount));
  37. atomic_inc(&kref->refcount);
  38. }
  39. /**
  40. * kref_sub - subtract a number of refcounts for object.
  41. * @kref: object.
  42. * @count: Number of recounts to subtract.
  43. * @release: pointer to the function that will clean up the object when the
  44. * last reference to the object is released.
  45. * This pointer is required, and it is not acceptable to pass kfree
  46. * in as this function. If the caller does pass kfree to this
  47. * function, you will be publicly mocked mercilessly by the kref
  48. * maintainer, and anyone else who happens to notice it. You have
  49. * been warned.
  50. *
  51. * Subtract @count from the refcount, and if 0, call release().
  52. * Return 1 if the object was removed, otherwise return 0. Beware, if this
  53. * function returns 0, you still can not count on the kref from remaining in
  54. * memory. Only use the return value if you want to see if the kref is now
  55. * gone, not present.
  56. */
  57. static inline int kref_sub(struct kref *kref, unsigned int count,
  58. void (*release)(struct kref *kref))
  59. {
  60. WARN_ON(release == NULL);
  61. if (atomic_sub_and_test((int) count, &kref->refcount)) {
  62. release(kref);
  63. return 1;
  64. }
  65. return 0;
  66. }
  67. /**
  68. * kref_put - decrement refcount for object.
  69. * @kref: object.
  70. * @release: pointer to the function that will clean up the object when the
  71. * last reference to the object is released.
  72. * This pointer is required, and it is not acceptable to pass kfree
  73. * in as this function. If the caller does pass kfree to this
  74. * function, you will be publicly mocked mercilessly by the kref
  75. * maintainer, and anyone else who happens to notice it. You have
  76. * been warned.
  77. *
  78. * Decrement the refcount, and if 0, call release().
  79. * Return 1 if the object was removed, otherwise return 0. Beware, if this
  80. * function returns 0, you still can not count on the kref from remaining in
  81. * memory. Only use the return value if you want to see if the kref is now
  82. * gone, not present.
  83. */
  84. static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref))
  85. {
  86. return kref_sub(kref, 1, release);
  87. }
  88. #endif /* _KREF_H_ */