Files
NetworkManager/shared/systemd/src/basic/refcnt.h
Thomas Haller 2c537b9d21 systemd: move basic systemd library to shared/nm-utils
For better or worse, we already pull in large parts of systemd sources.

I need a base64 decode implementation (because glib's g_base64_decode()
cannot reject invalid encodings). Instead of coming up with my own or
copy-paste if from somewhere, reuse systemd's unbase64mem().

But for that, make systemd's basic bits an independent static library
first because I will need it in libnm-core.

This doesn't really change anything except making "libnm-systemd-core.la"
an indpendent static library that could be used from "libnm-core". We
shall still be mindful about which internal code of systemd we use, and only
access functionality that is exposed via "systemd/nm-sd-utils-shared.h".
2019-01-02 17:07:13 +01:00

55 lines
2.4 KiB
C

/* SPDX-License-Identifier: LGPL-2.1+ */
#pragma once
/* A type-safe atomic refcounter.
*
* DO NOT USE THIS UNLESS YOU ACTUALLY CARE ABOUT THREAD SAFETY! */
typedef struct {
volatile unsigned _value;
} RefCount;
#define REFCNT_GET(r) ((r)._value)
#define REFCNT_INC(r) (__sync_add_and_fetch(&(r)._value, 1))
#define REFCNT_DEC(r) (__sync_sub_and_fetch(&(r)._value, 1))
#define REFCNT_INIT ((RefCount) { ._value = 1 })
#define _DEFINE_ATOMIC_REF_FUNC(type, name, scope) \
scope type *name##_ref(type *p) { \
if (!p) \
return NULL; \
\
assert_se(REFCNT_INC(p->n_ref) >= 2); \
return p; \
}
#define _DEFINE_ATOMIC_UNREF_FUNC(type, name, free_func, scope) \
scope type *name##_unref(type *p) { \
if (!p) \
return NULL; \
\
if (REFCNT_DEC(p->n_ref) > 0) \
return NULL; \
\
return free_func(p); \
}
#define DEFINE_ATOMIC_REF_FUNC(type, name) \
_DEFINE_ATOMIC_REF_FUNC(type, name,)
#define DEFINE_PUBLIC_ATOMIC_REF_FUNC(type, name) \
_DEFINE_ATOMIC_REF_FUNC(type, name, _public_)
#define DEFINE_ATOMIC_UNREF_FUNC(type, name, free_func) \
_DEFINE_ATOMIC_UNREF_FUNC(type, name, free_func,)
#define DEFINE_PUBLIC_ATOMIC_UNREF_FUNC(type, name, free_func) \
_DEFINE_ATOMIC_UNREF_FUNC(type, name, free_func, _public_)
#define DEFINE_ATOMIC_REF_UNREF_FUNC(type, name, free_func) \
DEFINE_ATOMIC_REF_FUNC(type, name); \
DEFINE_ATOMIC_UNREF_FUNC(type, name, free_func);
#define DEFINE_PUBLIC_ATOMIC_REF_UNREF_FUNC(type, name, free_func) \
DEFINE_PUBLIC_ATOMIC_REF_FUNC(type, name); \
DEFINE_PUBLIC_ATOMIC_UNREF_FUNC(type, name, free_func);