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 | |
---|
19 | FILE_LICENCE ( GPL2_OR_LATER ); |
---|
20 | |
---|
21 | #include <stdlib.h> |
---|
22 | #include <gpxe/refcnt.h> |
---|
23 | |
---|
24 | /** @file |
---|
25 | * |
---|
26 | * Reference counting |
---|
27 | * |
---|
28 | */ |
---|
29 | |
---|
30 | /** |
---|
31 | * Increment reference count |
---|
32 | * |
---|
33 | * @v refcnt Reference counter, or NULL |
---|
34 | * @ret refcnt Reference counter |
---|
35 | * |
---|
36 | * If @c refcnt is NULL, no action is taken. |
---|
37 | */ |
---|
38 | struct refcnt * ref_get ( struct refcnt *refcnt ) { |
---|
39 | |
---|
40 | if ( refcnt ) { |
---|
41 | refcnt->refcnt++; |
---|
42 | DBGC2 ( refcnt, "REFCNT %p incremented to %d\n", |
---|
43 | refcnt, refcnt->refcnt ); |
---|
44 | } |
---|
45 | return refcnt; |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * Decrement reference count |
---|
50 | * |
---|
51 | * @v refcnt Reference counter, or NULL |
---|
52 | * |
---|
53 | * If the reference count decreases below zero, the object's free() |
---|
54 | * method will be called. |
---|
55 | * |
---|
56 | * If @c refcnt is NULL, no action is taken. |
---|
57 | */ |
---|
58 | void ref_put ( struct refcnt *refcnt ) { |
---|
59 | |
---|
60 | if ( ! refcnt ) |
---|
61 | return; |
---|
62 | |
---|
63 | refcnt->refcnt--; |
---|
64 | DBGC2 ( refcnt, "REFCNT %p decremented to %d\n", |
---|
65 | refcnt, refcnt->refcnt ); |
---|
66 | |
---|
67 | if ( refcnt->refcnt >= 0 ) |
---|
68 | return; |
---|
69 | |
---|
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 ); |
---|
77 | } |
---|
78 | } |
---|