X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=include%2Flttng%2Fref-internal.hpp;fp=include%2Flttng%2Fref-internal.hpp;h=a02ae4cdc1229a627226e74991cebb49887b4820;hp=0000000000000000000000000000000000000000;hb=c9e313bc594f40a86eed237dce222c0fc99c957f;hpb=4878de5c7deb512bbdac4fdfc498907efa06fb7c diff --git a/include/lttng/ref-internal.hpp b/include/lttng/ref-internal.hpp new file mode 100644 index 000000000..a02ae4cdc --- /dev/null +++ b/include/lttng/ref-internal.hpp @@ -0,0 +1,49 @@ +#ifndef LTTNG_REF_INTERNAL_H +#define LTTNG_REF_INTERNAL_H + +/* + * LTTng - Non thread-safe reference counting + * + * Copyright 2013, 2014 Jérémie Galarneau + * + * SPDX-License-Identifier: LGPL-2.1-only + * + */ + + +typedef void (*lttng_release_func)(void *); + +struct lttng_ref { + unsigned long count; + lttng_release_func release; +}; + +static inline +void lttng_ref_init(struct lttng_ref *ref, lttng_release_func release) +{ + LTTNG_ASSERT(ref); + ref->count = 1; + ref->release = release; +} + +static inline +void lttng_ref_get(struct lttng_ref *ref) +{ + LTTNG_ASSERT(ref); + ref->count++; + /* Overflow check. */ + LTTNG_ASSERT(ref->count); +} + +static inline +void lttng_ref_put(struct lttng_ref *ref) +{ + LTTNG_ASSERT(ref); + /* Underflow check. */ + LTTNG_ASSERT(ref->count); + if (caa_unlikely((--ref->count) == 0)) { + ref->release(ref); + } +} + +#endif /* LTTNG_REF_INTERNAL_H */