diff options
author | Michael Brown <mcb30@ipxe.org> | 2010-11-05 22:24:46 +0000 |
---|---|---|
committer | Michael Brown <mcb30@ipxe.org> | 2010-11-08 03:35:35 +0000 |
commit | 6e41f2cf18217dd5f4086852b61bb6f0146c1e0b (patch) | |
tree | 6d5e696d2e626d5187e69428d9d987e8cae9d5fb /src/include/ipxe/refcnt.h | |
parent | fc69ab94d939214e2a8bbb335a5e7e31552e88e1 (diff) | |
download | ipxe-6e41f2cf18217dd5f4086852b61bb6f0146c1e0b.zip ipxe-6e41f2cf18217dd5f4086852b61bb6f0146c1e0b.tar.gz ipxe-6e41f2cf18217dd5f4086852b61bb6f0146c1e0b.tar.bz2 |
[refcnt] Check reference validity on each use of ref_get() and ref_put()
Check that the reference count is valid (i.e. non-negative) on each
call to ref_get() and ref_put(), using an assert() at the point of
use.
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/include/ipxe/refcnt.h')
-rw-r--r-- | src/include/ipxe/refcnt.h | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/src/include/ipxe/refcnt.h b/src/include/ipxe/refcnt.h index 49fce50..0e8b865 100644 --- a/src/include/ipxe/refcnt.h +++ b/src/include/ipxe/refcnt.h @@ -9,6 +9,9 @@ FILE_LICENCE ( GPL2_OR_LATER ); +#include <stddef.h> +#include <assert.h> + /** * A reference counter * @@ -26,7 +29,7 @@ struct refcnt { * When this count is decremented below zero, the free() * method will be called. */ - int refcnt; + int count; /** Free containing object * * This method is called when the reference count is @@ -75,8 +78,37 @@ ref_init ( struct refcnt *refcnt, .free = free_fn, \ } -extern struct refcnt * ref_get ( struct refcnt *refcnt ); -extern void ref_put ( struct refcnt *refcnt ); +extern void ref_increment ( struct refcnt *refcnt ); +extern void ref_decrement ( struct refcnt *refcnt ); + +/** + * Get additional reference to object + * + * @v refcnt Reference counter, or NULL + * @ret refcnt Reference counter + * + * If @c refcnt is NULL, no action is taken. + */ +#define ref_get( refcnt ) ( { \ + if ( refcnt ) \ + assert ( (refcnt)->count >= 0 ); \ + ref_increment ( refcnt ); \ + (refcnt); } ) + +/** + * Drop reference to object + * + * @v refcnt Reference counter, or NULL + * @ret refcnt Reference counter + * + * If @c refcnt is NULL, no action is taken. + */ +#define ref_put( refcnt ) do { \ + if ( refcnt ) \ + assert ( (refcnt)->count >= 0 ); \ + ref_decrement ( refcnt ); \ + } while ( 0 ) + extern void ref_no_free ( struct refcnt *refcnt ); #endif /* _IPXE_REFCNT_H */ |