libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_ref.h
Go to the documentation of this file.
1/*! \file
2 * \brief Generic reference-counted pointers
3 * \ingroup utils
4 *
5 * This file contains GblRef, an atomic reference-counted
6 * shared pointer type, as well as its associated API.
7 *
8 * \note
9 * The reference-counting metadata for a GblRef is stored
10 * within a header above the returned pointer, meaning
11 * that a client can use a GblRef as though it were a
12 * regular void* type, other than managing its lifetime.
13 *
14 * \author 2023 Falco Girgis
15 * \copyright MIT License
16 */
17#ifndef GIMBAL_REF_H
18#define GIMBAL_REF_H
19
20#include "../core/gimbal_ctx.h"
21
22#define GBL_SELF_TYPE GblRef
23
25
26/*! Reference-counted shared pointer type
27 * \ingroup utils
28 */
29typedef void GblRef;
30
31typedef GBL_RESULT (*GblRefDestructFn) (GBL_SELF);
32
33GBL_EXPORT GBL_RESULT GblRef_reinit (void) GBL_NOEXCEPT;
34GBL_EXPORT GblRefCount GblRef_activeCount (void) GBL_NOEXCEPT;
35
36GBL_EXPORT void* GblRef_alloc (size_t size) GBL_NOEXCEPT;
37GBL_EXPORT void* GblRef_allocWithContext (size_t size, GblContext* pCtx) GBL_NOEXCEPT;
38
39GBL_EXPORT void* GblRef_acquire (GBL_CSELF) GBL_NOEXCEPT;
41GBL_EXPORT GblRefCount GblRef_releaseWithDtor (GBL_CSELF, GblRefDestructFn pFnDtor) GBL_NOEXCEPT;
42
44GBL_EXPORT GblContext* GblRef_context (GBL_CSELF) GBL_NOEXCEPT;
45
47
48#define GblRef_create(...) GBL_VA_OVERLOAD_CALL_ARGC(GblRef_create_, __VA_ARGS__)
49#define GblRef_ref(ref) GblRef_acquire(ref)
50#define GblRef_unref(...) GBL_VA_OVERLOAD_CALL_ARGC(GblRef_unref_, __VA_ARGS__)
51
52#define GblRef_create__1(size) GblRef_alloc(size)
53#define GblRef_create__2(size, ctx) GblRef_allocWithContext(size, ctx)
54
55#define GblRef_unref__1(ref) GblRef_release(ref)
56#define GblRef_unref__2(ref, dtor) GblRef_releaseWithDtor(ref, dtor)
57
58#undef GBL_SELF_TYPE
59
60#endif // GIMBAL_REF_H
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_EXPORT
#define GBL_VA_OVERLOAD_CALL_ARGC(BASE,...)
uint16_t GblRefCount
Type able to hold a reference counter across the codebase.
void GblRef
Reference-counted shared pointer type.
Definition gimbal_ref.h:29