10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
18 * This data structure is designed to be embedded within a
19 * reference-counted object.
21 * Reference-counted objects are freed when their reference count
22 * drops below zero. This means that a freshly allocated-and-zeroed
23 * reference-counted object will be freed on the first call to
27 /** Current reference count
29 * When this count is decremented below zero, the free()
30 * method will be called.
33 /** Free containing object
35 * This method is called when the reference count is
36 * decremented below zero.
38 * If this method is left NULL, the standard library free()
39 * function will be called. The upshot of this is that you
40 * may omit the free() method if the @c refcnt object is the
41 * first element of your reference-counted struct.
43 void ( * free ) ( struct refcnt *refcnt );
47 * Initialise a reference counter
49 * @v refcnt Reference counter
50 * @v free Freeing function
52 static inline __attribute__ (( always_inline )) void
53 ref_init ( struct refcnt *refcnt,
54 void ( * free ) ( struct refcnt *refcnt ) ) {
59 * Initialise a reference counter
61 * @v refcnt Reference counter
62 * @v free Free containing object
64 #define ref_init( refcnt, free ) do { \
65 if ( __builtin_constant_p ( (free) ) && ( (free) == NULL ) ) { \
66 /* Skip common case of no initialisation required */ \
68 ref_init ( (refcnt), (free) ); \
73 * Initialise a static reference counter
75 * @v free_fn Free containing object
77 #define REF_INIT( free_fn ) { \
81 extern void ref_increment ( struct refcnt *refcnt );
82 extern void ref_decrement ( struct refcnt *refcnt );
85 * Get additional reference to object
87 * @v refcnt Reference counter, or NULL
88 * @ret refcnt Reference counter
90 * If @c refcnt is NULL, no action is taken.
92 #define ref_get( refcnt ) ( { \
94 assert ( (refcnt)->count >= 0 ); \
95 ref_increment ( refcnt ); \
99 * Drop reference to object
101 * @v refcnt Reference counter, or NULL
102 * @ret refcnt Reference counter
104 * If @c refcnt is NULL, no action is taken.
106 #define ref_put( refcnt ) do { \
108 assert ( (refcnt)->count >= 0 ); \
109 ref_decrement ( refcnt ); \
112 extern void ref_no_free ( struct refcnt *refcnt );
114 #endif /* _IPXE_REFCNT_H */