Skip to content

Commit a6a0859

Browse files
committed
macro: introduce DEFINE_TRIVIAL_REF_UNREF_FUNC() macro and friends
1 parent cf4b2f9 commit a6a0859

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/basic/macro.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,4 +464,53 @@ static inline int __coverity_check__(int condition) {
464464
func(*p); \
465465
}
466466

467+
#define _DEFINE_TRIVIAL_REF_FUNC(type, name, scope) \
468+
scope type *name##_ref(type *p) { \
469+
if (!p) \
470+
return NULL; \
471+
\
472+
assert(p->n_ref > 0); \
473+
p->n_ref++; \
474+
return p; \
475+
}
476+
477+
#define _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, scope) \
478+
scope type *name##_unref(type *p) { \
479+
if (!p) \
480+
return NULL; \
481+
\
482+
assert(p->n_ref > 0); \
483+
p->n_ref--; \
484+
if (p->n_ref > 0) \
485+
return NULL; \
486+
\
487+
return free_func(p); \
488+
}
489+
490+
#define DEFINE_TRIVIAL_REF_FUNC(type, name) \
491+
_DEFINE_TRIVIAL_REF_FUNC(type, name,)
492+
#define DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name) \
493+
_DEFINE_TRIVIAL_REF_FUNC(type, name, static)
494+
#define DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name) \
495+
_DEFINE_TRIVIAL_REF_FUNC(type, name, _public_)
496+
497+
#define DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func) \
498+
_DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func,)
499+
#define DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func) \
500+
_DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, static)
501+
#define DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func) \
502+
_DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, _public_)
503+
504+
#define DEFINE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
505+
DEFINE_TRIVIAL_REF_FUNC(type, name); \
506+
DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func);
507+
508+
#define DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
509+
DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name); \
510+
DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func);
511+
512+
#define DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
513+
DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name); \
514+
DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func);
515+
467516
#include "log.h"

0 commit comments

Comments
 (0)