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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <stdlib.h>
00022 #include <gpxe/refcnt.h>
00023 
00024 /** @file
00025  *
00026  * Reference counting
00027  *
00028  */
00029 
00030 /**
00031  * Increment reference count
00032  *
00033  * @v refcnt            Reference counter, or NULL
00034  * @ret refcnt          Reference counter
00035  *
00036  * If @c refcnt is NULL, no action is taken.
00037  */
00038 struct refcnt * ref_get ( struct refcnt *refcnt ) {
00039 
00040         if ( refcnt ) {
00041                 refcnt->refcnt++;
00042                 DBGC2 ( refcnt, "REFCNT %p incremented to %d\n",
00043                         refcnt, refcnt->refcnt );
00044         }
00045         return refcnt;
00046 }
00047 
00048 /**
00049  * Decrement reference count
00050  *
00051  * @v refcnt            Reference counter, or NULL
00052  *
00053  * If the reference count decreases below zero, the object's free()
00054  * method will be called.
00055  *
00056  * If @c refcnt is NULL, no action is taken.
00057  */
00058 void ref_put ( struct refcnt *refcnt ) {
00059 
00060         if ( ! refcnt )
00061                 return;
00062 
00063         refcnt->refcnt--;
00064         DBGC2 ( refcnt, "REFCNT %p decremented to %d\n",
00065                 refcnt, refcnt->refcnt );
00066 
00067         if ( refcnt->refcnt >= 0 )
00068                 return;
00069 
00070         if ( refcnt->free ) {
00071                 DBGC ( refcnt, "REFCNT %p being freed via method %p\n",
00072                        refcnt, refcnt->free );
00073                 refcnt->free ( refcnt );
00074         } else {
00075                 DBGC ( refcnt, "REFCNT %p being freed\n", refcnt );
00076                 free ( refcnt );
00077         }
00078 }

Generated on Tue Apr 6 20:00:51 2010 for gPXE by  doxygen 1.5.7.1