Merge branch 'free-sized-macro' into 'main'

gmem: Add an inline definition of g_free() to automatically use g_free_sized()

See merge request GNOME/glib!3252
This commit is contained in:
Philip Withnall 2023-05-30 13:29:25 +00:00
commit 32ec11e51d
4 changed files with 27 additions and 2 deletions

View File

@ -212,6 +212,8 @@ typedef @g_pid_type@ GPid;
#define G_SEARCHPATH_SEPARATOR '@g_searchpath_separator@'
#define G_SEARCHPATH_SEPARATOR_S "@g_searchpath_separator@"
#mesondefine G_HAVE_FREE_SIZED
G_END_DECLS
#endif /* __GLIBCONFIG_H__ */

View File

@ -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 dont 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

View File

@ -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 && defined(G_HAVE_FREE_SIZED)
#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 && defined(G_HAVE_FREE_SIZED) */
/**
* g_steal_pointer:
* @pp: (not nullable): a pointer to a pointer

View File

@ -729,6 +729,10 @@ foreach f : functions
endif
endforeach
# Export the information about free_sized() so we can correctly define a macro
# wrapper around g_free()/g_free_sized() depending on whether its available
glibconfig_conf.set('G_HAVE_FREE_SIZED', have_func_free_sized)
# Check that stpcpy() is usable; must use header.
# See:
# https://github.com/mesonbuild/meson/issues/5628.