iPXE
refcnt.c
Go to the documentation of this file.
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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdlib.h>
27 #include <ipxe/refcnt.h>
28 
29 /** @file
30  *
31  * Reference counting
32  *
33  */
34 
35 /**
36  * Increment reference count
37  *
38  * @v refcnt Reference counter, or NULL
39  *
40  * If @c refcnt is NULL, no action is taken.
41  */
42 void ref_increment ( struct refcnt *refcnt ) {
43 
44  if ( refcnt ) {
45  refcnt->count++;
46  DBGC2 ( refcnt, "REFCNT %p incremented to %d\n",
47  refcnt, refcnt->count );
48  }
49 }
50 
51 /**
52  * Decrement reference count
53  *
54  * @v refcnt Reference counter, or NULL
55  *
56  * If the reference count decreases below zero, the object's free()
57  * method will be called.
58  *
59  * If @c refcnt is NULL, no action is taken.
60  */
61 void ref_decrement ( struct refcnt *refcnt ) {
62 
63  if ( ! refcnt )
64  return;
65 
66  refcnt->count--;
67  DBGC2 ( refcnt, "REFCNT %p decremented to %d\n",
68  refcnt, refcnt->count );
69 
70  if ( refcnt->count >= 0 )
71  return;
72 
73  if ( refcnt->count < -1 ) {
74  DBGC ( refcnt, "REFCNT %p decremented too far (%d)!\n",
75  refcnt, refcnt->count );
76  /* Avoid multiple calls to free(), which typically
77  * result in memory corruption that is very hard to
78  * track down.
79  */
80  return;
81  }
82 
83  if ( refcnt->free ) {
84  DBGC ( refcnt, "REFCNT %p being freed via method %p\n",
85  refcnt, refcnt->free );
86  refcnt->free ( refcnt );
87  } else {
88  DBGC ( refcnt, "REFCNT %p being freed\n", refcnt );
89  free ( refcnt );
90  }
91 }
92 
93 /**
94  * Do not free reference-counted object
95  *
96  * @v refcnt Reference counter
97  *
98  * This is meant for initializing a reference counter structure in a
99  * statically allocated object.
100  */
101 void ref_no_free ( struct refcnt *refcnt __unused ) {
102  /* Do nothing */
103 }
#define DBGC(...)
Definition: compiler.h:505
void ref_decrement(struct refcnt *refcnt)
Decrement reference count.
Definition: refcnt.c:61
A reference counter.
Definition: refcnt.h:26
int count
Current reference count.
Definition: refcnt.h:32
void ref_increment(struct refcnt *refcnt)
Increment reference count.
Definition: refcnt.c:42
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
#define DBGC2(...)
Definition: compiler.h:522
Reference counting.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
void ref_no_free(struct refcnt *refcnt __unused)
Do not free reference-counted object.
Definition: refcnt.c:101
void(* free)(struct refcnt *refcnt)
Free containing object.
Definition: refcnt.h:43