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 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdlib.h>
  21. #include <ipxe/refcnt.h>
  22. /** @file
  23. *
  24. * Reference counting
  25. *
  26. */
  27. /**
  28. * Increment reference count
  29. *
  30. * @v refcnt Reference counter, or NULL
  31. *
  32. * If @c refcnt is NULL, no action is taken.
  33. */
  34. void ref_increment ( struct refcnt *refcnt ) {
  35. if ( refcnt ) {
  36. refcnt->count++;
  37. DBGC2 ( refcnt, "REFCNT %p incremented to %d\n",
  38. refcnt, refcnt->count );
  39. }
  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_decrement ( struct refcnt *refcnt ) {
  52. if ( ! refcnt )
  53. return;
  54. refcnt->count--;
  55. DBGC2 ( refcnt, "REFCNT %p decremented to %d\n",
  56. refcnt, refcnt->count );
  57. if ( refcnt->count >= 0 )
  58. return;
  59. if ( refcnt->count < -1 ) {
  60. DBGC ( refcnt, "REFCNT %p decremented too far (%d)!\n",
  61. refcnt, refcnt->count );
  62. /* Avoid multiple calls to free(), which typically
  63. * result in memory corruption that is very hard to
  64. * track down.
  65. */
  66. return;
  67. }
  68. if ( refcnt->free ) {
  69. DBGC ( refcnt, "REFCNT %p being freed via method %p\n",
  70. refcnt, refcnt->free );
  71. refcnt->free ( refcnt );
  72. } else {
  73. DBGC ( refcnt, "REFCNT %p being freed\n", refcnt );
  74. free ( refcnt );
  75. }
  76. }
  77. /**
  78. * Do not free reference-counted object
  79. *
  80. * @v refcnt Reference counter
  81. *
  82. * This is meant for initializing a reference counter structure in a
  83. * statically allocated object.
  84. */
  85. void ref_no_free ( struct refcnt *refcnt __unused ) {
  86. /* Do nothing */
  87. }