Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

refcnt.c 2.3KB

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