/usr/include/glib-2.0/glib/gmem.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* GLIB - Library of useful routines for C programming |
2 | | * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
3 | | * |
4 | | * This library is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Lesser General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2.1 of the License, or (at your option) any later version. |
8 | | * |
9 | | * This library is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | | * Lesser General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Lesser General Public |
15 | | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | |
18 | | /* |
19 | | * Modified by the GLib Team and others 1997-2000. See the AUTHORS |
20 | | * file for a list of people on the GLib Team. See the ChangeLog |
21 | | * files for a list of changes. These files are distributed with |
22 | | * GLib at ftp://ftp.gtk.org/pub/gtk/. |
23 | | */ |
24 | | |
25 | | #ifndef __G_MEM_H__ |
26 | | #define __G_MEM_H__ |
27 | | |
28 | | #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION) |
29 | | #error "Only <glib.h> can be included directly." |
30 | | #endif |
31 | | |
32 | | #include <glib/gutils.h> |
33 | | |
34 | | G_BEGIN_DECLS |
35 | | |
36 | | /** |
37 | | * GMemVTable: |
38 | | * @malloc: function to use for allocating memory. |
39 | | * @realloc: function to use for reallocating memory. |
40 | | * @free: function to use to free memory. |
41 | | * @calloc: function to use for allocating zero-filled memory. |
42 | | * @try_malloc: function to use for allocating memory without a default error handler. |
43 | | * @try_realloc: function to use for reallocating memory without a default error handler. |
44 | | * |
45 | | * A set of functions used to perform memory allocation. The same #GMemVTable must |
46 | | * be used for all allocations in the same program; a call to g_mem_set_vtable(), |
47 | | * if it exists, should be prior to any use of GLib. |
48 | | * |
49 | | * This functions related to this has been deprecated in 2.46, and no longer work. |
50 | | */ |
51 | | typedef struct _GMemVTable GMemVTable; |
52 | | |
53 | | |
54 | | #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG |
55 | | /** |
56 | | * G_MEM_ALIGN: |
57 | | * |
58 | | * Indicates the number of bytes to which memory will be aligned on the |
59 | | * current platform. |
60 | | */ |
61 | | # define G_MEM_ALIGN GLIB_SIZEOF_VOID_P |
62 | | #else /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */ |
63 | | # define G_MEM_ALIGN GLIB_SIZEOF_LONG |
64 | | #endif /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */ |
65 | | |
66 | | |
67 | | /* Memory allocation functions |
68 | | */ |
69 | | |
70 | | GLIB_AVAILABLE_IN_ALL |
71 | | void g_free (gpointer mem); |
72 | | |
73 | | GLIB_AVAILABLE_IN_2_34 |
74 | | void g_clear_pointer (gpointer *pp, |
75 | | GDestroyNotify destroy); |
76 | | |
77 | | GLIB_AVAILABLE_IN_ALL |
78 | | gpointer g_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1); |
79 | | GLIB_AVAILABLE_IN_ALL |
80 | | gpointer g_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1); |
81 | | GLIB_AVAILABLE_IN_ALL |
82 | | gpointer g_realloc (gpointer mem, |
83 | | gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT; |
84 | | GLIB_AVAILABLE_IN_ALL |
85 | | gpointer g_try_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1); |
86 | | GLIB_AVAILABLE_IN_ALL |
87 | | gpointer g_try_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1); |
88 | | GLIB_AVAILABLE_IN_ALL |
89 | | gpointer g_try_realloc (gpointer mem, |
90 | | gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT; |
91 | | |
92 | | GLIB_AVAILABLE_IN_ALL |
93 | | gpointer g_malloc_n (gsize n_blocks, |
94 | | gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2); |
95 | | GLIB_AVAILABLE_IN_ALL |
96 | | gpointer g_malloc0_n (gsize n_blocks, |
97 | | gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2); |
98 | | GLIB_AVAILABLE_IN_ALL |
99 | | gpointer g_realloc_n (gpointer mem, |
100 | | gsize n_blocks, |
101 | | gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT; |
102 | | GLIB_AVAILABLE_IN_ALL |
103 | | gpointer g_try_malloc_n (gsize n_blocks, |
104 | | gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2); |
105 | | GLIB_AVAILABLE_IN_ALL |
106 | | gpointer g_try_malloc0_n (gsize n_blocks, |
107 | | gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2); |
108 | | GLIB_AVAILABLE_IN_ALL |
109 | | gpointer g_try_realloc_n (gpointer mem, |
110 | | gsize n_blocks, |
111 | | gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT; |
112 | | |
113 | | #if defined(g_has_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58 |
114 | | #define g_clear_pointer(pp, destroy) \ |
115 | | G_STMT_START { \ |
116 | | G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \ |
117 | | __typeof__((pp)) _pp = (pp); \ |
118 | | __typeof__(*(pp)) _ptr = *_pp; \ |
119 | | *_pp = NULL; \ |
120 | | if (_ptr) \ |
121 | | (destroy) (_ptr); \ |
122 | | } G_STMT_END \ |
123 | | GLIB_AVAILABLE_MACRO_IN_2_34 |
124 | | #else /* __GNUC__ */ |
125 | | #define g_clear_pointer(pp, destroy) \ |
126 | | G_STMT_START { \ |
127 | | G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \ |
128 | | /* Only one access, please; work around type aliasing */ \ |
129 | | union { char *in; gpointer *out; } _pp; \ |
130 | | gpointer _p; \ |
131 | | /* This assignment is needed to avoid a gcc warning */ \ |
132 | | GDestroyNotify _destroy = (GDestroyNotify) (destroy); \ |
133 | | \ |
134 | | _pp.in = (char *) (pp); \ |
135 | | _p = *_pp.out; \ |
136 | | if (_p) \ |
137 | | { \ |
138 | | *_pp.out = NULL; \ |
139 | | _destroy (_p); \ |
140 | | } \ |
141 | | } G_STMT_END \ |
142 | | GLIB_AVAILABLE_MACRO_IN_2_34 |
143 | | #endif /* __GNUC__ */ |
144 | | |
145 | | /** |
146 | | * g_steal_pointer: |
147 | | * @pp: (not nullable): a pointer to a pointer |
148 | | * |
149 | | * Sets @pp to %NULL, returning the value that was there before. |
150 | | * |
151 | | * Conceptually, this transfers the ownership of the pointer from the |
152 | | * referenced variable to the "caller" of the macro (ie: "steals" the |
153 | | * reference). |
154 | | * |
155 | | * The return value will be properly typed, according to the type of |
156 | | * @pp. |
157 | | * |
158 | | * This can be very useful when combined with g_autoptr() to prevent the |
159 | | * return value of a function from being automatically freed. Consider |
160 | | * the following example (which only works on GCC and clang): |
161 | | * |
162 | | * |[ |
163 | | * GObject * |
164 | | * create_object (void) |
165 | | * { |
166 | | * g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL); |
167 | | * |
168 | | * if (early_error_case) |
169 | | * return NULL; |
170 | | * |
171 | | * return g_steal_pointer (&obj); |
172 | | * } |
173 | | * ]| |
174 | | * |
175 | | * It can also be used in similar ways for 'out' parameters and is |
176 | | * particularly useful for dealing with optional out parameters: |
177 | | * |
178 | | * |[ |
179 | | * gboolean |
180 | | * get_object (GObject **obj_out) |
181 | | * { |
182 | | * g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL); |
183 | | * |
184 | | * if (early_error_case) |
185 | | * return FALSE; |
186 | | * |
187 | | * if (obj_out) |
188 | | * *obj_out = g_steal_pointer (&obj); |
189 | | * |
190 | | * return TRUE; |
191 | | * } |
192 | | * ]| |
193 | | * |
194 | | * In the above example, the object will be automatically freed in the |
195 | | * early error case and also in the case that %NULL was given for |
196 | | * @obj_out. |
197 | | * |
198 | | * Since: 2.44 |
199 | | */ |
200 | | static inline gpointer |
201 | | g_steal_pointer (gpointer pp) |
202 | 0 | { |
203 | 0 | gpointer *ptr = (gpointer *) pp; |
204 | 0 | gpointer ref; |
205 | 0 |
|
206 | 0 | ref = *ptr; |
207 | 0 | *ptr = NULL; |
208 | 0 |
|
209 | 0 | return ref; |
210 | 0 | } Unexecuted instantiation: fuzzing-main.cpp:g_steal_pointer(void*) Unexecuted instantiation: binding-handler.cpp:g_steal_pointer(void*) Unexecuted instantiation: boolcfg-stub.cpp:g_steal_pointer(void*) Unexecuted instantiation: device-energy-management-stub.cpp:g_steal_pointer(void*) Unexecuted instantiation: energy-evse-stub.cpp:g_steal_pointer(void*) Unexecuted instantiation: occupancy-sensing-stub.cpp:g_steal_pointer(void*) Unexecuted instantiation: operational-state-delegate-impl.cpp:g_steal_pointer(void*) Unexecuted instantiation: smco-stub.cpp:g_steal_pointer(void*) Unexecuted instantiation: software-diagnostics-stub.cpp:g_steal_pointer(void*) Unexecuted instantiation: wifi-diagnostics-stub.cpp:g_steal_pointer(void*) Unexecuted instantiation: DeviceEnergyManagementDelegateImpl.cpp:g_steal_pointer(void*) Unexecuted instantiation: DeviceEnergyManagementManager.cpp:g_steal_pointer(void*) Unexecuted instantiation: device-energy-management-mode.cpp:g_steal_pointer(void*) Unexecuted instantiation: ChargingTargetsMemMgr.cpp:g_steal_pointer(void*) Unexecuted instantiation: EVSEManufacturerImpl.cpp:g_steal_pointer(void*) Unexecuted instantiation: EnergyEvseDelegateImpl.cpp:g_steal_pointer(void*) Unexecuted instantiation: EnergyEvseManager.cpp:g_steal_pointer(void*) Unexecuted instantiation: EnergyEvseTargetsStore.cpp:g_steal_pointer(void*) Unexecuted instantiation: energy-evse-mode.cpp:g_steal_pointer(void*) Unexecuted instantiation: WhmDelegateImpl.cpp:g_steal_pointer(void*) Unexecuted instantiation: WhmInstance.cpp:g_steal_pointer(void*) Unexecuted instantiation: WhmMain.cpp:g_steal_pointer(void*) Unexecuted instantiation: WhmManufacturer.cpp:g_steal_pointer(void*) Unexecuted instantiation: water-heater-mode.cpp:g_steal_pointer(void*) Unexecuted instantiation: thermostat-delegate-impl.cpp:g_steal_pointer(void*) Unexecuted instantiation: AllClustersCommandDelegate.cpp:g_steal_pointer(void*) Unexecuted instantiation: AppOptions.cpp:g_steal_pointer(void*) Unexecuted instantiation: ButtonEventsSimulator.cpp:g_steal_pointer(void*) Unexecuted instantiation: WindowCoveringManager.cpp:g_steal_pointer(void*) Unexecuted instantiation: main-common.cpp:g_steal_pointer(void*) Unexecuted instantiation: AppMain.cpp:g_steal_pointer(void*) Unexecuted instantiation: CommissionableInit.cpp:g_steal_pointer(void*) Unexecuted instantiation: Options.cpp:g_steal_pointer(void*) Unexecuted instantiation: CustomCSRResponse.cpp:g_steal_pointer(void*) Unexecuted instantiation: reporting.cpp:g_steal_pointer(void*) Unexecuted instantiation: attribute-storage.cpp:g_steal_pointer(void*) Unexecuted instantiation: access-control-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: CodegenIntegration.cpp:g_steal_pointer(void*) Unexecuted instantiation: basic-information.cpp:g_steal_pointer(void*) Unexecuted instantiation: BindingManager.cpp:g_steal_pointer(void*) Unexecuted instantiation: bindings.cpp:g_steal_pointer(void*) Unexecuted instantiation: camera-av-settings-user-level-management-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: camera-av-stream-management-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: color-control-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: descriptor.cpp:g_steal_pointer(void*) Unexecuted instantiation: device-energy-management-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: dishwasher-alarm-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: energy-evse-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: fault-injection-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: fixed-label-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: general-commissioning-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: general-diagnostics-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: group-key-mgmt-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: identify-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: laundry-dryer-controls-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: laundry-washer-controls-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: level-control.cpp:g_steal_pointer(void*) Unexecuted instantiation: localization-configuration-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: mode-base-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: mode-select-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: CodegenInstance.cpp:g_steal_pointer(void*) Unexecuted instantiation: on-off-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: operational-credentials-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: operational-state-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: BDXDownloader.cpp:g_steal_pointer(void*) Unexecuted instantiation: DefaultOTARequestor.cpp:g_steal_pointer(void*) Unexecuted instantiation: DefaultOTARequestorDriver.cpp:g_steal_pointer(void*) Unexecuted instantiation: ExtendedOTARequestorDriver.cpp:g_steal_pointer(void*) Unexecuted instantiation: pump-configuration-and-control-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: refrigerator-alarm-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: resource-monitoring-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: scenes-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: switch-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: temperature-control-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: test-cluster-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: thermostat-server-atomic.cpp:g_steal_pointer(void*) Unexecuted instantiation: thermostat-server-presets.cpp:g_steal_pointer(void*) Unexecuted instantiation: thermostat-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: thread-network-diagnostics-provider.cpp:g_steal_pointer(void*) Unexecuted instantiation: thread-network-diagnostics-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: time-format-localization-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: time-synchronization-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: user-label-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: valve-configuration-and-control-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: water-heater-management-server.cpp:g_steal_pointer(void*) Unexecuted instantiation: IMClusterCommandHandler.cpp:g_steal_pointer(void*) Unexecuted instantiation: AdministratorCommissioningCluster.cpp:g_steal_pointer(void*) Unexecuted instantiation: AdministratorCommissioningLogic.cpp:g_steal_pointer(void*) Unexecuted instantiation: NetworkCommissioningCluster.cpp:g_steal_pointer(void*) Unexecuted instantiation: NetworkCommissioningLogic.cpp:g_steal_pointer(void*) Unexecuted instantiation: OnboardingCodesUtil.cpp:g_steal_pointer(void*) Unexecuted instantiation: EventManagement.cpp:g_steal_pointer(void*) Unexecuted instantiation: FailSafeContext.cpp:g_steal_pointer(void*) Unexecuted instantiation: ReliableMessageContext.cpp:g_steal_pointer(void*) Unexecuted instantiation: ReliableMessageMgr.cpp:g_steal_pointer(void*) Unexecuted instantiation: ReliableMessageProtocolConfig.cpp:g_steal_pointer(void*) Unexecuted instantiation: DeviceControlServer.cpp:g_steal_pointer(void*) Unexecuted instantiation: Globals.cpp:g_steal_pointer(void*) Unexecuted instantiation: LockTracker.cpp:g_steal_pointer(void*) Unexecuted instantiation: PlatformEventSupport.cpp:g_steal_pointer(void*) Unexecuted instantiation: SingletonConfigurationManager.cpp:g_steal_pointer(void*) Unexecuted instantiation: CHIPLinuxStorage.cpp:g_steal_pointer(void*) Unexecuted instantiation: CHIPLinuxStorageIni.cpp:g_steal_pointer(void*) Unexecuted instantiation: ConfigurationManagerImpl.cpp:g_steal_pointer(void*) Unexecuted instantiation: ConnectivityManagerImpl.cpp:g_steal_pointer(void*) Unexecuted instantiation: ConnectivityUtils.cpp:g_steal_pointer(void*) Unexecuted instantiation: DiagnosticDataProviderImpl.cpp:g_steal_pointer(void*) Unexecuted instantiation: NetworkCommissioningEthernetDriver.cpp:g_steal_pointer(void*) Unexecuted instantiation: PlatformManagerImpl.cpp:g_steal_pointer(void*) Unexecuted instantiation: GeneralUtils.cpp:g_steal_pointer(void*) Unexecuted instantiation: DeviceInstanceInfoProviderImpl.cpp:g_steal_pointer(void*) Unexecuted instantiation: PosixConfig.cpp:g_steal_pointer(void*) Unexecuted instantiation: SystemTimeSupport.cpp:g_steal_pointer(void*) Unexecuted instantiation: BLEManagerImpl.cpp:g_steal_pointer(void*) Unexecuted instantiation: BluezAdvertisement.cpp:g_steal_pointer(void*) Unexecuted instantiation: BluezConnection.cpp:g_steal_pointer(void*) Unexecuted instantiation: BluezEndpoint.cpp:g_steal_pointer(void*) Unexecuted instantiation: BluezObjectManager.cpp:g_steal_pointer(void*) Unexecuted instantiation: ChipDeviceScanner.cpp:g_steal_pointer(void*) Unexecuted instantiation: NetworkCommissioningWiFiDriver.cpp:g_steal_pointer(void*) Unexecuted instantiation: DBusWpa.c:g_steal_pointer Unexecuted instantiation: DBusWpaBss.c:g_steal_pointer Unexecuted instantiation: DBusWpaInterface.c:g_steal_pointer Unexecuted instantiation: DBusWpaNetwork.c:g_steal_pointer Unexecuted instantiation: DBusBluez.c:g_steal_pointer Unexecuted instantiation: SessionManager.cpp:g_steal_pointer(void*) Unexecuted instantiation: LastKnownGoodTime.cpp:g_steal_pointer(void*) Unexecuted instantiation: CASESessionManager.cpp:g_steal_pointer(void*) Unexecuted instantiation: InteractionModelEngine.cpp:g_steal_pointer(void*) Unexecuted instantiation: WriteHandler.cpp:g_steal_pointer(void*) Unexecuted instantiation: CommandResponseSender.cpp:g_steal_pointer(void*) Unexecuted instantiation: ReadHandler.cpp:g_steal_pointer(void*) Unexecuted instantiation: OperationalSessionSetup.cpp:g_steal_pointer(void*) Unexecuted instantiation: CASESession.cpp:g_steal_pointer(void*) Unexecuted instantiation: PairingSession.cpp:g_steal_pointer(void*) Unexecuted instantiation: Engine.cpp:g_steal_pointer(void*) Unexecuted instantiation: ReadClient.cpp:g_steal_pointer(void*) Unexecuted instantiation: SubscriptionResumptionSessionEstablisher.cpp:g_steal_pointer(void*) Unexecuted instantiation: Server.cpp:g_steal_pointer(void*) Unexecuted instantiation: CommissioningWindowManager.cpp:g_steal_pointer(void*) Unexecuted instantiation: Dnssd.cpp:g_steal_pointer(void*) Unexecuted instantiation: ReportSchedulerImpl.cpp:g_steal_pointer(void*) Unexecuted instantiation: WiFiPAF.cpp:g_steal_pointer(void*) Unexecuted instantiation: TimerDelegates.cpp:g_steal_pointer(void*) Unexecuted instantiation: AllClustersExampleDeviceInfoProviderImpl.cpp:g_steal_pointer(void*) Unexecuted instantiation: DeviceInfoProviderImpl.cpp:g_steal_pointer(void*) |
211 | | |
212 | | /* type safety */ |
213 | | #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) && !defined(__cplusplus) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58 |
214 | | #define g_steal_pointer(pp) ((__typeof__(*pp)) (g_steal_pointer) (pp)) |
215 | | #else /* __GNUC__ */ |
216 | | /* This version does not depend on gcc extensions, but gcc does not warn |
217 | | * about incompatible-pointer-types: */ |
218 | | #define g_steal_pointer(pp) \ |
219 | | (0 ? (*(pp)) : (g_steal_pointer) (pp)) |
220 | | #endif /* __GNUC__ */ |
221 | | |
222 | | /* Optimise: avoid the call to the (slower) _n function if we can |
223 | | * determine at compile-time that no overflow happens. |
224 | | */ |
225 | | #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__) |
226 | | # define _G_NEW(struct_type, n_structs, func) \ |
227 | 0 | (struct_type *) (G_GNUC_EXTENSION ({ \ |
228 | 0 | gsize __n = (gsize) (n_structs); \ |
229 | 0 | gsize __s = sizeof (struct_type); \ |
230 | 0 | gpointer __p; \ |
231 | 0 | if (__s == 1) \ |
232 | 0 | __p = g_##func (__n); \ |
233 | 0 | else if (__builtin_constant_p (__n) && \ |
234 | 0 | (__s == 0 || __n <= G_MAXSIZE / __s)) \ |
235 | 0 | __p = g_##func (__n * __s); \ |
236 | 0 | else \ |
237 | 0 | __p = g_##func##_n (__n, __s); \ |
238 | 0 | __p; \ |
239 | 0 | })) |
240 | | # define _G_RENEW(struct_type, mem, n_structs, func) \ |
241 | | (struct_type *) (G_GNUC_EXTENSION ({ \ |
242 | | gsize __n = (gsize) (n_structs); \ |
243 | | gsize __s = sizeof (struct_type); \ |
244 | | gpointer __p = (gpointer) (mem); \ |
245 | | if (__s == 1) \ |
246 | | __p = g_##func (__p, __n); \ |
247 | | else if (__builtin_constant_p (__n) && \ |
248 | | (__s == 0 || __n <= G_MAXSIZE / __s)) \ |
249 | | __p = g_##func (__p, __n * __s); \ |
250 | | else \ |
251 | | __p = g_##func##_n (__p, __n, __s); \ |
252 | | __p; \ |
253 | | })) |
254 | | |
255 | | #else |
256 | | |
257 | | /* Unoptimised version: always call the _n() function. */ |
258 | | |
259 | | #define _G_NEW(struct_type, n_structs, func) \ |
260 | | ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type))) |
261 | | #define _G_RENEW(struct_type, mem, n_structs, func) \ |
262 | | ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type))) |
263 | | |
264 | | #endif |
265 | | |
266 | | /** |
267 | | * g_new: |
268 | | * @struct_type: the type of the elements to allocate |
269 | | * @n_structs: the number of elements to allocate |
270 | | * |
271 | | * Allocates @n_structs elements of type @struct_type. |
272 | | * The returned pointer is cast to a pointer to the given type. |
273 | | * If @n_structs is 0 it returns %NULL. |
274 | | * Care is taken to avoid overflow when calculating the size of the allocated block. |
275 | | * |
276 | | * Since the returned pointer is already casted to the right type, |
277 | | * it is normally unnecessary to cast it explicitly, and doing |
278 | | * so might hide memory allocation errors. |
279 | | * |
280 | | * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type |
281 | | */ |
282 | | #define g_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc) |
283 | | /** |
284 | | * g_new0: |
285 | | * @struct_type: the type of the elements to allocate. |
286 | | * @n_structs: the number of elements to allocate. |
287 | | * |
288 | | * Allocates @n_structs elements of type @struct_type, initialized to 0's. |
289 | | * The returned pointer is cast to a pointer to the given type. |
290 | | * If @n_structs is 0 it returns %NULL. |
291 | | * Care is taken to avoid overflow when calculating the size of the allocated block. |
292 | | * |
293 | | * Since the returned pointer is already casted to the right type, |
294 | | * it is normally unnecessary to cast it explicitly, and doing |
295 | | * so might hide memory allocation errors. |
296 | | * |
297 | | * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type. |
298 | | */ |
299 | 0 | #define g_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc0) |
300 | | /** |
301 | | * g_renew: |
302 | | * @struct_type: the type of the elements to allocate |
303 | | * @mem: the currently allocated memory |
304 | | * @n_structs: the number of elements to allocate |
305 | | * |
306 | | * Reallocates the memory pointed to by @mem, so that it now has space for |
307 | | * @n_structs elements of type @struct_type. It returns the new address of |
308 | | * the memory, which may have been moved. |
309 | | * Care is taken to avoid overflow when calculating the size of the allocated block. |
310 | | * |
311 | | * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type |
312 | | */ |
313 | | #define g_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, realloc) |
314 | | /** |
315 | | * g_try_new: |
316 | | * @struct_type: the type of the elements to allocate |
317 | | * @n_structs: the number of elements to allocate |
318 | | * |
319 | | * Attempts to allocate @n_structs elements of type @struct_type, and returns |
320 | | * %NULL on failure. Contrast with g_new(), which aborts the program on failure. |
321 | | * The returned pointer is cast to a pointer to the given type. |
322 | | * The function returns %NULL when @n_structs is 0 of if an overflow occurs. |
323 | | * |
324 | | * Since: 2.8 |
325 | | * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type |
326 | | */ |
327 | | #define g_try_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc) |
328 | | /** |
329 | | * g_try_new0: |
330 | | * @struct_type: the type of the elements to allocate |
331 | | * @n_structs: the number of elements to allocate |
332 | | * |
333 | | * Attempts to allocate @n_structs elements of type @struct_type, initialized |
334 | | * to 0's, and returns %NULL on failure. Contrast with g_new0(), which aborts |
335 | | * the program on failure. |
336 | | * The returned pointer is cast to a pointer to the given type. |
337 | | * The function returns %NULL when @n_structs is 0 or if an overflow occurs. |
338 | | * |
339 | | * Since: 2.8 |
340 | | * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type |
341 | | */ |
342 | | #define g_try_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc0) |
343 | | /** |
344 | | * g_try_renew: |
345 | | * @struct_type: the type of the elements to allocate |
346 | | * @mem: the currently allocated memory |
347 | | * @n_structs: the number of elements to allocate |
348 | | * |
349 | | * Attempts to reallocate the memory pointed to by @mem, so that it now has |
350 | | * space for @n_structs elements of type @struct_type, and returns %NULL on |
351 | | * failure. Contrast with g_renew(), which aborts the program on failure. |
352 | | * It returns the new address of the memory, which may have been moved. |
353 | | * The function returns %NULL if an overflow occurs. |
354 | | * |
355 | | * Since: 2.8 |
356 | | * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type |
357 | | */ |
358 | | #define g_try_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, try_realloc) |
359 | | |
360 | | |
361 | | /* Memory allocation virtualization for debugging purposes |
362 | | * g_mem_set_vtable() has to be the very first GLib function called |
363 | | * if being used |
364 | | */ |
365 | | struct _GMemVTable { |
366 | | gpointer (*malloc) (gsize n_bytes); |
367 | | gpointer (*realloc) (gpointer mem, |
368 | | gsize n_bytes); |
369 | | void (*free) (gpointer mem); |
370 | | /* optional; set to NULL if not used ! */ |
371 | | gpointer (*calloc) (gsize n_blocks, |
372 | | gsize n_block_bytes); |
373 | | gpointer (*try_malloc) (gsize n_bytes); |
374 | | gpointer (*try_realloc) (gpointer mem, |
375 | | gsize n_bytes); |
376 | | }; |
377 | | GLIB_DEPRECATED_IN_2_46 |
378 | | void g_mem_set_vtable (GMemVTable *vtable); |
379 | | GLIB_DEPRECATED_IN_2_46 |
380 | | gboolean g_mem_is_system_malloc (void); |
381 | | |
382 | | GLIB_VAR gboolean g_mem_gc_friendly; |
383 | | |
384 | | /* Memory profiler and checker, has to be enabled via g_mem_set_vtable() |
385 | | */ |
386 | | GLIB_VAR GMemVTable *glib_mem_profiler_table; |
387 | | GLIB_DEPRECATED_IN_2_46 |
388 | | void g_mem_profile (void); |
389 | | |
390 | | G_END_DECLS |
391 | | |
392 | | #endif /* __G_MEM_H__ */ |