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.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. *
  30. * If @c refcnt is NULL, no action is taken.
  31. */
  32. void ref_get ( struct refcnt *refcnt ) {
  33. if ( ! refcnt )
  34. return;
  35. refcnt->refcnt++;
  36. DBGC ( refcnt, "REFCNT %p incremented to %d\n",
  37. refcnt, refcnt->refcnt );
  38. }
  39. /**
  40. * Decrement reference count
  41. *
  42. * @v refcnt Reference counter, or NULL
  43. *
  44. * If the reference count decreases below zero, the object's free()
  45. * method will be called.
  46. *
  47. * If @c refcnt is NULL, no action is taken.
  48. */
  49. void ref_put ( struct refcnt *refcnt ) {
  50. if ( ! refcnt )
  51. return;
  52. refcnt->refcnt--;
  53. DBGC ( refcnt, "REFCNT %p decremented to %d\n",
  54. refcnt, refcnt->refcnt );
  55. if ( refcnt->refcnt >= 0 )
  56. return;
  57. if ( refcnt->free ) {
  58. refcnt->free ( refcnt );
  59. } else {
  60. free ( refcnt );
  61. }
  62. }