[contrib] Allow Network Protocol header to display in rom-o-matic
[gpxe.git] / src / core / refcnt.c
blob162583250860354b2de66e2ab89e2dec4a132863
1 /*
2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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.
19 FILE_LICENCE ( GPL2_OR_LATER );
21 #include <stdlib.h>
22 #include <gpxe/refcnt.h>
24 /** @file
26 * Reference counting
30 /**
31 * Increment reference count
33 * @v refcnt Reference counter, or NULL
34 * @ret refcnt Reference counter
36 * If @c refcnt is NULL, no action is taken.
38 struct refcnt * ref_get ( struct refcnt *refcnt ) {
40 if ( refcnt ) {
41 refcnt->refcnt++;
42 DBGC2 ( refcnt, "REFCNT %p incremented to %d\n",
43 refcnt, refcnt->refcnt );
45 return refcnt;
48 /**
49 * Decrement reference count
51 * @v refcnt Reference counter, or NULL
53 * If the reference count decreases below zero, the object's free()
54 * method will be called.
56 * If @c refcnt is NULL, no action is taken.
58 void ref_put ( struct refcnt *refcnt ) {
60 if ( ! refcnt )
61 return;
63 refcnt->refcnt--;
64 DBGC2 ( refcnt, "REFCNT %p decremented to %d\n",
65 refcnt, refcnt->refcnt );
67 if ( refcnt->refcnt >= 0 )
68 return;
70 if ( refcnt->free ) {
71 DBGC ( refcnt, "REFCNT %p being freed via method %p\n",
72 refcnt, refcnt->free );
73 refcnt->free ( refcnt );
74 } else {
75 DBGC ( refcnt, "REFCNT %p being freed\n", refcnt );
76 free ( refcnt );
80 /**
81 * Do not free reference-counted object
83 * @v refcnt Reference counter
85 * This is meant for initializing a reference counter structure in a
86 * statically allocated object.
88 void ref_no_free ( struct refcnt *refcnt __unused ) {
89 /* Do nothing */