TARFS 0.1.5
Read-only TAR filesystem for ESP32
Loading...
Searching...
No Matches
refc.h
Go to the documentation of this file.
1/*
2 * TARFS - Immutable (read-only) filesystem for embedded systems.
3 *
4 * Copyright (c) 2026 Viacheslav Logunov
5 * SPDX-License-Identifier: MMIT
6 *
7 * Author:
8 * Viacheslav Logunov <vvb333007@gmail.com>
9 *
10 * Project:
11 * https://github.com/vvb333007/tarfs
12 *
13 * @file refc.h
14 * @brief Public Atomic Reference Counter API
15 */
16
17
30
31#pragma once
32
33/* Compilation issues (C / C++): these two languages use different atomic libraries, with different
34 * syntax. This is completely safe, as C++ code does not touch tarfs atomics
35 */
36#ifdef __cplusplus
37# undef _Atomic
38# define _Atomic(X) X
39#endif
40
41
47typedef unsigned int refc_type_t;
48
52typedef _Atomic(refc_type_t) refc_t;
53
54
55#ifndef __cplusplus
56
57#include <stdint.h>
58#include <stdatomic.h>
59#include <stdlib.h>
60#include <stdbool.h>
61
62
72static inline void initref(refc_t *ref) {
73
74 if (ref != NULL)
75 atomic_init(ref, 1);
76}
77
78
89static inline void initrefn(refc_t *ref, refc_type_t n) {
90
91 if (ref != NULL)
92 atomic_init(ref, n);
93}
94
95
96
115refc_type_t addrefn(refc_t *ref, refc_type_t n);
116
117
129static inline refc_type_t addref(refc_t *ref) {
130
131 return addrefn(ref, 1);
132}
133
170refc_type_t unrefxn(refc_t *r,
171 void *object,
172 refc_type_t n,
173 void (*dtor)(void *));
174
175
176
190static inline refc_type_t unref(refc_t *ref, void *object) {
191
192 return unrefxn(ref, object, 1, free);
193}
194
209static inline refc_type_t unrefn(refc_t *ref, void *object, refc_type_t n) {
210
211 return unrefxn(ref, object, n, free);
212}
213
227static inline refc_type_t unrefx(refc_t *ref, void *object, void (*dtor)(void *)) {
228
229 return unrefxn(ref, object, 1, dtor);
230}
231
243static inline refc_type_t readref(refc_t *r) {
244
245 return r ? atomic_load_explicit(r, memory_order_relaxed) : 0;
246}
247
248#endif /* not __cplusplus */
249
refc_type_t unrefxn(refc_t *r, void *object, refc_type_t n, void(*dtor)(void *))
Release references and optionally destroy object.
Definition refc.c:75
typedef _Atomic(refc_type_t) refc_t
Atomic reference counter type.
unsigned int refc_type_t
Use case:
Definition refc.h:47
refc_type_t addrefn(refc_t *ref, refc_type_t n)
Increase reference counter by specified value.
Definition refc.c:39