mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-03-29 02:50:04 +01:00
gmem: Add an inline definition of g_free() to automatically use g_free_sized()
When using GCC we can take the advantage of __builtin_object_size() to know the allocated size of a memory area, this generally only works when some optimization level enabled (-O1 seems enough here) and can provide us with memory size information for lower-level optimizations.
This commit is contained in:
parent
ad464d5563
commit
c580b5c2f4
12
glib/gmem.c
12
glib/gmem.c
@ -223,11 +223,17 @@ g_realloc (gpointer mem,
|
||||
* If you know the allocated size of @mem, calling g_free_sized() may be faster,
|
||||
* depending on the libc implementation in use.
|
||||
*
|
||||
* Starting from GLib 2.78, this may happen automatically in case a GCC
|
||||
* compatible compiler is used with some optimization level and the allocated
|
||||
* size is known at compile time (see [documentation of
|
||||
* `__builtin_object_size()`](https://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html)
|
||||
* to understand its caveats).
|
||||
*
|
||||
* If @mem is %NULL it simply returns, so there is no need to check @mem
|
||||
* against %NULL before calling this function.
|
||||
*/
|
||||
void
|
||||
g_free (gpointer mem)
|
||||
(g_free) (gpointer mem)
|
||||
{
|
||||
free (mem);
|
||||
TRACE(GLIB_MEM_FREE((void*) mem));
|
||||
@ -246,6 +252,10 @@ g_free (gpointer mem)
|
||||
* allocated. @size is passed to this function to allow optimizations in the
|
||||
* allocator. If you don’t know the allocation size, use g_free() instead.
|
||||
*
|
||||
* In case a GCC compatible compiler is used, this function may be used
|
||||
* automatically via g_free() if the allocated size is known at compile time,
|
||||
* since GLib 2.78.
|
||||
*
|
||||
* Since: 2.76
|
||||
*/
|
||||
void
|
||||
|
11
glib/gmem.h
11
glib/gmem.h
@ -71,7 +71,7 @@ typedef struct _GMemVTable GMemVTable;
|
||||
*/
|
||||
|
||||
GLIB_AVAILABLE_IN_ALL
|
||||
void g_free (gpointer mem);
|
||||
void (g_free) (gpointer mem);
|
||||
GLIB_AVAILABLE_IN_2_76
|
||||
void g_free_sized (gpointer mem,
|
||||
size_t size);
|
||||
@ -165,6 +165,15 @@ void g_aligned_free_sized (gpointer mem,
|
||||
GLIB_AVAILABLE_MACRO_IN_2_34
|
||||
#endif /* __GNUC__ */
|
||||
|
||||
|
||||
#if G_GNUC_CHECK_VERSION (4, 1) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_78
|
||||
|
||||
#define g_free(mem) \
|
||||
(__builtin_object_size ((mem), 0) != ((size_t) - 1)) ? \
|
||||
g_free_sized (mem, __builtin_object_size ((mem), 0)) : (g_free) (mem)
|
||||
|
||||
#endif /* G_GNUC_CHECK_VERSION (4, 1) && && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_78 */
|
||||
|
||||
/**
|
||||
* g_steal_pointer:
|
||||
* @pp: (not nullable): a pointer to a pointer
|
||||
|
Loading…
x
Reference in New Issue
Block a user