PokerUnicorn
Loading...
Searching...
No Matches
ref.h
Go to the documentation of this file.
1/*
2 * PokerUnicorn Server
3 * This project uses test network, NO real coin or NO real money involved.
4 * Copyright (C) 2023, Oğuzhan Eroğlu <meowingcate@gmail.com> (https://meowingcat.io)
5 * Licensed under GPLv3 License
6 * See LICENSE for more info
7 */
8
9#define _GNU_SOURCE
10
11#pragma once
12
18#include <stdio.h>
19#include <pthread.h>
20
21#include "../include/util.h"
22
23typedef void (*pkrsrv_ref_free_f_t)(void*);
24
32#define PKRSRV_REF_COUNTED_INIT(obj, free_f) \
33 pkrsrv_ref_counted_init(&(obj->ref_counted), (pkrsrv_ref_free_f_t) free_f);
34
41#define PKRSRV_REF_COUNTED_USE(obj) \
42 PKRSRV_UTIL_ASSERT(obj); \
43 if (obj != NULL) { \
44 pkrsrv_util_verbose("Referencing: %s\n", #obj); \
45 \
46 pkrsrv_ref_counted_use(&(obj->ref_counted)); \
47 }
48
55#define PKRSRV_REF_COUNTED_LEAVE(obj) \
56 PKRSRV_UTIL_ASSERT(obj); \
57 { \
58 if (obj != NULL) { \
59 __typeof__(obj) derefing = obj; \
60 pkrsrv_util_verbose("Dereferencing: %s\n", #obj); \
61 pkrsrv_ref_counted_leave((void **) (&(derefing)), &(derefing->ref_counted)); \
62 } \
63 }
64
70#define PKRSRV_REF_COUNTEDIFY() \
71 pkrsrv_ref_counted_t ref_counted;
72#define PKRSRV_REF_BY(name) name##_ref
73#define PKRSRV_REF(type, name) \
74 type name; \
75 pkrsrv_ref_t* PKRSRV_REF_BY(name);
76#define PKRSRV_REF_NEW(path, expr) \
77 path = expr; \
78 PKRSRV_REF_BY(path) = pkrsrv_ref_new((void *) path, &(path->ref_counted));
79#define PKRSRV_REF_ASSIGN(var, val) \
80 pkrsrv_ref_assign(&(PKRSRV_REF_BY(var)), &(PKRSRV_REF_BY(val))); \
81 var = val;
82#define PKRSRV_REF_SET(ref, obj) \
83 ref = obj; \
84 pkrsrv_ref_set(PKRSRV_REF_BY(ref), (void *) (ref));
85#define PKRSRV_REF_USE(name) pkrsrv_ref_use(PKRSRV_REF_BY(name));
86#define PKRSRV_REF_LEAVE(name) pkrsrv_ref_leave(&(PKRSRV_REF_BY(name)));
87#define PKRSRV_REF_ARG(type, name) type name, pkrsrv_ref_t* name##_ref
88#define PKRSRV_REF_PASS(name) pkrsrv_ref_new((void *) name, &(name->ref_counted));
89
90typedef struct pkrsrv_ref_counted pkrsrv_ref_counted_t;
91typedef struct pkrsrv_ref pkrsrv_ref_t;
92
102
104 int count;
105 void* obj;
106 pkrsrv_ref_counted_t* ref_counted;
107};
108
116void pkrsrv_ref_counted_init(pkrsrv_ref_counted_t* ref_counted, pkrsrv_ref_free_f_t free_f);
123void pkrsrv_ref_counted_use(pkrsrv_ref_counted_t* ref_counted);
130void pkrsrv_ref_counted_leave(void** obj_vp, pkrsrv_ref_counted_t* ref_counted);
131
132pkrsrv_ref_t* pkrsrv_ref_new(void* obj, pkrsrv_ref_counted_t* ref_counted);
133void pkrsrv_ref_free(pkrsrv_ref_t* ref);
134void pkrsrv_ref_use(pkrsrv_ref_t* ref);
135void pkrsrv_ref_leave(pkrsrv_ref_t** ref);
136void pkrsrv_ref_assign(pkrsrv_ref_t** dst, pkrsrv_ref_t** src);
137void pkrsrv_ref_set(pkrsrv_ref_t* ref, void* obj);
int count
Definition ref.h:98
pkrsrv_ref_free_f_t free_f
Definition ref.h:99
pthread_mutex_t mutex
Definition ref.h:100
void pkrsrv_ref_counted_init(pkrsrv_ref_counted_t *ref_counted, pkrsrv_ref_free_f_t free_f)
Initializes a reference-counted object. Should be called in the constructor of the object after the d...
void pkrsrv_ref_counted_leave(void **obj_vp, pkrsrv_ref_counted_t *ref_counted)
Decrements the reference count of a reference-counted object.
void pkrsrv_ref_counted_use(pkrsrv_ref_counted_t *ref_counted)
Increments the reference count of a reference-counted object.
Reference-counted object structure.
Definition ref.h:97
void pkrsrv_ref_set(pkrsrv_ref_t *ref, void *obj)
pkrsrv_ref_t * pkrsrv_ref_new(void *obj, pkrsrv_ref_counted_t *ref_counted)
void pkrsrv_ref_use(pkrsrv_ref_t *ref)
void pkrsrv_ref_assign(pkrsrv_ref_t **dst, pkrsrv_ref_t **src)
void pkrsrv_ref_free(pkrsrv_ref_t *ref)
void * obj
Definition ref.h:105
int count
Definition ref.h:104
void(* pkrsrv_ref_free_f_t)(void *)
Definition ref.h:23
void pkrsrv_ref_leave(pkrsrv_ref_t **ref)
pkrsrv_ref_counted_t * ref_counted
Definition ref.h:106
Definition ref.h:103