Zrythm
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
objects.h
1// SPDX-FileCopyrightText: © 2019-2023 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#ifndef __UTILS_OBJECTS_H__
5#define __UTILS_OBJECTS_H__
6
7#include <cstddef>
8
9#include <gmodule.h>
10
17NONNULL static inline void
18_object_zero_and_free (void ** ptr, size_t sz)
19{
20 if (!*ptr)
21 return;
22
23 memset (*ptr, 0, sz);
24 free (*ptr);
25 *ptr = NULL;
26}
27
32NONNULL static inline void
33_object_zero_and_free_unresizable (void ** ptr, size_t sz)
34{
35 if (!*ptr)
36 return;
37
38 memset (*ptr, 0, sz);
39 free (*ptr);
40 *ptr = NULL;
41}
42
46#define object_new(type) (type *) g_malloc0 (sizeof (type))
47
59#define object_new_unresizable(type) object_new (type)
60
64#define object_new_n_sizeof(n, sz) g_malloc0_n (n, sz)
65
69#define object_new_n(n, type) \
70 (static_cast<type *> (object_new_n_sizeof (n, sizeof (type))))
71
72#define object_realloc_n_sizeof(obj, prev_sz, sz) \
73 realloc_zero (obj, prev_sz, sz)
74
81#define object_realloc_n(obj, prev_n, n, type) \
82 static_cast<type *> ( \
83 object_realloc_n_sizeof (obj, prev_n * sizeof (type), n * sizeof (type)))
84
90#define object_set_to_zero(ptr) memset (ptr, 0, sizeof (*(ptr)))
91
97#define object_free_unresizable(type, obj) free (obj)
98
103#define object_zero_and_free(ptr) \
104 _object_zero_and_free ((void **) &(ptr), sizeof (*(ptr)))
105
106#define object_zero_and_free_unresizable(type, ptr) \
107 _object_zero_and_free_unresizable ((void **) &(ptr), sizeof (type))
108
113#define object_free_w_func_and_null(_func, _obj) \
114 if (_obj) \
115 { \
116 _func (_obj); \
117 _obj = NULL; \
118 }
119
120#define object_delete_and_null(_obj) \
121 if (_obj) \
122 { \
123 delete _obj; \
124 _obj = nullptr; \
125 }
126
127#define object_zero_and_free_if_nonnull(ptr) \
128 object_free_w_func_and_null (object_zero_and_free, ptr)
129
131#define g_object_unref_and_null(ptr) \
132 object_free_w_func_and_null (g_object_unref, ptr)
133
135#define g_free_and_null(ptr) object_free_w_func_and_null (g_free, ptr)
136
138#define g_error_free_and_null(ptr) \
139 object_free_w_func_and_null (g_error_free, ptr)
140
142#define object_free_w_func_and_null_cast(_func, _cast, _obj) \
143 if (_obj) \
144 { \
145 _func ((_cast) _obj); \
146 _obj = NULL; \
147 }
148
149#define g_source_remove_and_zero(src_id) \
150 { \
151 g_source_remove (src_id); \
152 src_id = 0; \
153 }
154
159#endif