File: ref-internal.hpp

package info (click to toggle)
ltt-control 2.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,744 kB
  • sloc: cpp: 207,706; sh: 28,837; python: 18,952; ansic: 11,636; makefile: 3,362; java: 109; xml: 46
file content (49 lines) | stat: -rw-r--r-- 945 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 * LTTng - Non thread-safe reference counting
 *
 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-only
 *
 */

#ifndef LTTNG_REF_INTERNAL_H
#define LTTNG_REF_INTERNAL_H

#include <common/macros.hpp>

#include <urcu/urcu.h>

using lttng_release_func = void (*)(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 */