-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.h
161 lines (132 loc) · 3.43 KB
/
objects.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// SPDX-FileCopyrightText: © 2019-2023 Alexandros Theodotou <[email protected]>
// SPDX-License-Identifier: LicenseRef-ZrythmLicense
#ifndef __UTILS_OBJECTS_H__
#define __UTILS_OBJECTS_H__
#include <cstddef>
#include <gmodule.h>
/**
* @addtogroup utils
*
* @{
*/
#define NONNULL
NONNULL static inline void
_object_zero_and_free (void ** ptr, size_t sz)
{
if (!*ptr)
return;
memset (*ptr, 0, sz);
free (*ptr);
*ptr = NULL;
}
/**
* @note Some objects are created by libcyaml which uses
* plain malloc so avoid using this on serializable objects.
*/
NONNULL static inline void
_object_zero_and_free_unresizable (void ** ptr, size_t sz)
{
if (!*ptr)
return;
memset (*ptr, 0, sz);
free (*ptr);
*ptr = NULL;
}
/**
* Allocates memory for an object of type \ref type.
*/
#define object_new(type) (type *) g_malloc0 (sizeof (type))
/**
* Allocates memory for an object of type \ref type.
*
* This is more efficient than object_new() but does not
* support resizing.
*
* Must be free'd with object_free_unresizable().
*
* @note g_slice_*() API was removed from glib so use the
* same mechanism as above for now.
*/
#define object_new_unresizable(type) object_new (type)
/**
* Calloc equivalent.
*/
#define object_new_n_sizeof(n, sz) g_malloc0_n (n, sz)
/**
* Calloc \ref n blocks for type \ref type.
*/
#define object_new_n(n, type) \
(static_cast<type *> (object_new_n_sizeof (n, sizeof (type))))
#define object_realloc_n_sizeof(obj, prev_sz, sz) \
realloc_zero (obj, prev_sz, sz)
/**
* Reallocate memory for \ref obj.
*
* @param prev_n Previous number of blocks.
* @param n New number of blocks.
*/
#define object_realloc_n(obj, prev_n, n, type) \
static_cast<type *> ( \
object_realloc_n_sizeof (obj, prev_n * sizeof (type), n * sizeof (type)))
/**
* Zero's out the struct pointed to by \ref ptr.
*
* @param ptr A pointer to a struct.
*/
#define object_set_to_zero(ptr) memset (ptr, 0, sizeof (*(ptr)))
/**
* Frees memory for objects created with object_new().
*
* @note Prefer object_zero_and_free_unresizable().
*/
#define object_free_unresizable(type, obj) free (obj)
/**
* Zero's out a struct pointed to by \ref ptr and
* frees the object.
*/
#define object_zero_and_free(ptr) \
_object_zero_and_free ((void **) &(ptr), sizeof (*(ptr)))
#define object_zero_and_free_unresizable(type, ptr) \
_object_zero_and_free_unresizable ((void **) &(ptr), sizeof (type))
/**
* Call the function \ref _func to free \ref _obj
* and set \ref _obj to NULL.
*/
#define object_free_w_func_and_null(_func, _obj) \
if (_obj) \
{ \
_func (_obj); \
_obj = NULL; \
}
#define object_delete_and_null(_obj) \
if (_obj) \
{ \
delete _obj; \
_obj = nullptr; \
}
#define object_zero_and_free_if_nonnull(ptr) \
object_free_w_func_and_null (object_zero_and_free, ptr)
/** Convenience wrapper. */
#define g_object_unref_and_null(ptr) \
object_free_w_func_and_null (g_object_unref, ptr)
/** Convenience wrapper. */
#define g_free_and_null(ptr) object_free_w_func_and_null (g_free, ptr)
/** Convenience wrapper. */
#define g_error_free_and_null(ptr) \
object_free_w_func_and_null (g_error_free, ptr)
/** Convenience wrapper. */
#define object_free_w_func_and_null_cast(_func, _cast, _obj) \
if (_obj) \
{ \
_func ((_cast) _obj); \
_obj = NULL; \
}
#define g_source_remove_and_zero(src_id) \
{ \
g_source_remove (src_id); \
src_id = 0; \
}
/**
* @}
*/
#endif