iPXE
refcnt.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdlib.h>
00027 #include <ipxe/refcnt.h>
00028 
00029 /** @file
00030  *
00031  * Reference counting
00032  *
00033  */
00034 
00035 /**
00036  * Increment reference count
00037  *
00038  * @v refcnt            Reference counter, or NULL
00039  *
00040  * If @c refcnt is NULL, no action is taken.
00041  */
00042 void ref_increment ( struct refcnt *refcnt ) {
00043 
00044         if ( refcnt ) {
00045                 refcnt->count++;
00046                 DBGC2 ( refcnt, "REFCNT %p incremented to %d\n",
00047                         refcnt, refcnt->count );
00048         }
00049 }
00050 
00051 /**
00052  * Decrement reference count
00053  *
00054  * @v refcnt            Reference counter, or NULL
00055  *
00056  * If the reference count decreases below zero, the object's free()
00057  * method will be called.
00058  *
00059  * If @c refcnt is NULL, no action is taken.
00060  */
00061 void ref_decrement ( struct refcnt *refcnt ) {
00062 
00063         if ( ! refcnt )
00064                 return;
00065 
00066         refcnt->count--;
00067         DBGC2 ( refcnt, "REFCNT %p decremented to %d\n",
00068                 refcnt, refcnt->count );
00069 
00070         if ( refcnt->count >= 0 )
00071                 return;
00072 
00073         if ( refcnt->count < -1 ) {
00074                 DBGC ( refcnt, "REFCNT %p decremented too far (%d)!\n",
00075                        refcnt, refcnt->count );
00076                 /* Avoid multiple calls to free(), which typically
00077                  * result in memory corruption that is very hard to
00078                  * track down.
00079                  */
00080                 return;
00081         }
00082 
00083         if ( refcnt->free ) {
00084                 DBGC ( refcnt, "REFCNT %p being freed via method %p\n",
00085                        refcnt, refcnt->free );
00086                 refcnt->free ( refcnt );
00087         } else {
00088                 DBGC ( refcnt, "REFCNT %p being freed\n", refcnt );
00089                 free ( refcnt );
00090         }
00091 }
00092 
00093 /**
00094  * Do not free reference-counted object
00095  *
00096  * @v refcnt            Reference counter
00097  *
00098  * This is meant for initializing a reference counter structure in a
00099  * statically allocated object.
00100  */
00101 void ref_no_free ( struct refcnt *refcnt __unused ) {
00102         /* Do nothing */
00103 }