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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #include <stdlib.h>
  19. #include <gpxe/refcnt.h>
  20. /** @file
  21. *
  22. * Reference counting
  23. *
  24. */
  25. /**
  26. * Increment reference count
  27. *
  28. * @v refcnt Reference counter, or NULL
  29. * @ret refcnt Reference counter
  30. *
  31. * If @c refcnt is NULL, no action is taken.
  32. */
  33. struct refcnt * ref_get ( struct refcnt *refcnt ) {
  34. if ( refcnt ) {
  35. refcnt->refcnt++;
  36. DBGC2 ( refcnt, "REFCNT %p incremented to %d\n",
  37. refcnt, refcnt->refcnt );
  38. }
  39. return refcnt;
  40. }
  41. /**
  42. * Decrement reference count
  43. *
  44. * @v refcnt Reference counter, or NULL
  45. *
  46. * If the reference count decreases below zero, the object's free()
  47. * method will be called.
  48. *
  49. * If @c refcnt is NULL, no action is taken.
  50. */
  51. void ref_put ( struct refcnt *refcnt ) {
  52. if ( ! refcnt )
  53. return;
  54. refcnt->refcnt--;
  55. DBGC2 ( refcnt, "REFCNT %p decremented to %d\n",
  56. refcnt, refcnt->refcnt );
  57. if ( refcnt->refcnt >= 0 )
  58. return;
  59. if ( refcnt->free ) {
  60. DBGC ( refcnt, "REFCNT %p being freed via method %p\n",
  61. refcnt, refcnt->free );
  62. refcnt->free ( refcnt );
  63. } else {
  64. DBGC ( refcnt, "REFCNT %p being freed\n", refcnt );
  65. free ( refcnt );
  66. }
  67. }