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.

refcnt.c 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdlib.h>
  20. #include <gpxe/refcnt.h>
  21. /** @file
  22. *
  23. * Reference counting
  24. *
  25. */
  26. /**
  27. * Increment reference count
  28. *
  29. * @v refcnt Reference counter, or NULL
  30. * @ret refcnt Reference counter
  31. *
  32. * If @c refcnt is NULL, no action is taken.
  33. */
  34. struct refcnt * ref_get ( struct refcnt *refcnt ) {
  35. if ( refcnt ) {
  36. refcnt->refcnt++;
  37. DBGC2 ( refcnt, "REFCNT %p incremented to %d\n",
  38. refcnt, refcnt->refcnt );
  39. }
  40. return refcnt;
  41. }
  42. /**
  43. * Decrement reference count
  44. *
  45. * @v refcnt Reference counter, or NULL
  46. *
  47. * If the reference count decreases below zero, the object's free()
  48. * method will be called.
  49. *
  50. * If @c refcnt is NULL, no action is taken.
  51. */
  52. void ref_put ( struct refcnt *refcnt ) {
  53. if ( ! refcnt )
  54. return;
  55. refcnt->refcnt--;
  56. DBGC2 ( refcnt, "REFCNT %p decremented to %d\n",
  57. refcnt, refcnt->refcnt );
  58. if ( refcnt->refcnt >= 0 )
  59. return;
  60. if ( refcnt->free ) {
  61. DBGC ( refcnt, "REFCNT %p being freed via method %p\n",
  62. refcnt, refcnt->free );
  63. refcnt->free ( refcnt );
  64. } else {
  65. DBGC ( refcnt, "REFCNT %p being freed\n", refcnt );
  66. free ( refcnt );
  67. }
  68. }