Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

refcnt.c 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdlib.h>
  25. #include <ipxe/refcnt.h>
  26. /** @file
  27. *
  28. * Reference counting
  29. *
  30. */
  31. /**
  32. * Increment reference count
  33. *
  34. * @v refcnt Reference counter, or NULL
  35. *
  36. * If @c refcnt is NULL, no action is taken.
  37. */
  38. void ref_increment ( struct refcnt *refcnt ) {
  39. if ( refcnt ) {
  40. refcnt->count++;
  41. DBGC2 ( refcnt, "REFCNT %p incremented to %d\n",
  42. refcnt, refcnt->count );
  43. }
  44. }
  45. /**
  46. * Decrement reference count
  47. *
  48. * @v refcnt Reference counter, or NULL
  49. *
  50. * If the reference count decreases below zero, the object's free()
  51. * method will be called.
  52. *
  53. * If @c refcnt is NULL, no action is taken.
  54. */
  55. void ref_decrement ( struct refcnt *refcnt ) {
  56. if ( ! refcnt )
  57. return;
  58. refcnt->count--;
  59. DBGC2 ( refcnt, "REFCNT %p decremented to %d\n",
  60. refcnt, refcnt->count );
  61. if ( refcnt->count >= 0 )
  62. return;
  63. if ( refcnt->count < -1 ) {
  64. DBGC ( refcnt, "REFCNT %p decremented too far (%d)!\n",
  65. refcnt, refcnt->count );
  66. /* Avoid multiple calls to free(), which typically
  67. * result in memory corruption that is very hard to
  68. * track down.
  69. */
  70. return;
  71. }
  72. if ( refcnt->free ) {
  73. DBGC ( refcnt, "REFCNT %p being freed via method %p\n",
  74. refcnt, refcnt->free );
  75. refcnt->free ( refcnt );
  76. } else {
  77. DBGC ( refcnt, "REFCNT %p being freed\n", refcnt );
  78. free ( refcnt );
  79. }
  80. }
  81. /**
  82. * Do not free reference-counted object
  83. *
  84. * @v refcnt Reference counter
  85. *
  86. * This is meant for initializing a reference counter structure in a
  87. * statically allocated object.
  88. */
  89. void ref_no_free ( struct refcnt *refcnt __unused ) {
  90. /* Do nothing */
  91. }