diff --git a/docs/reference/glib/glib-sections.txt.in b/docs/reference/glib/glib-sections.txt.in index 072084b2e..04f5aa4eb 100644 --- a/docs/reference/glib/glib-sections.txt.in +++ b/docs/reference/glib/glib-sections.txt.in @@ -1082,6 +1082,7 @@ g_try_realloc_n g_free +g_free_sized g_clear_pointer g_steal_pointer g_mem_gc_friendly @@ -1096,6 +1097,7 @@ g_newa0 g_aligned_alloc g_aligned_alloc0 g_aligned_free +g_aligned_free_sized g_memmove diff --git a/glib/gmem.c b/glib/gmem.c index fa175df62..7e19aed65 100644 --- a/glib/gmem.c +++ b/glib/gmem.c @@ -220,6 +220,9 @@ g_realloc (gpointer mem, * * Frees the memory pointed to by @mem. * + * If you know the allocated size of @mem, calling g_free_sized() may be faster, + * depending on the libc implementation in use. + * * If @mem is %NULL it simply returns, so there is no need to check @mem * against %NULL before calling this function. */ @@ -230,6 +233,33 @@ g_free (gpointer mem) TRACE(GLIB_MEM_FREE((void*) mem)); } +/** + * g_free_sized: + * @mem: (nullable): the memory to free + * @size: size of @mem, in bytes + * + * Frees the memory pointed to by @mem, assuming it is has the given @size. + * + * If @mem is %NULL this is a no-op (and @size is ignored). + * + * It is an error if @size doesn’t match the size passed when @mem was + * 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. + * + * Since: 2.76 + */ +void +g_free_sized (void *mem, + size_t size) +{ +#ifdef HAVE_FREE_SIZED + free_sized (mem, size); +#else + free (mem); +#endif + TRACE (GLIB_MEM_FREE ((void*) mem)); +} + /** * g_clear_pointer: (skip) * @pp: (nullable) (not optional) (inout) (transfer full): a pointer to a @@ -578,7 +608,7 @@ g_mem_profile (void) * the program is terminated. * * Aligned memory allocations returned by this function can only be - * freed using g_aligned_free(). + * freed using g_aligned_free_sized() or g_aligned_free(). * * Returns: (transfer full): the allocated memory * @@ -702,3 +732,33 @@ g_aligned_free (gpointer mem) { aligned_free (mem); } + +/** + * g_aligned_free_sized: + * @mem: (nullable): the memory to free + * @alignment: alignment of @mem + * @size: size of @mem, in bytes + * + * Frees the memory pointed to by @mem, assuming it is has the given @size and + * @alignment. + * + * If @mem is %NULL this is a no-op (and @size is ignored). + * + * It is an error if @size doesn’t match the size, or @alignment doesn’t match + * the alignment, passed when @mem was allocated. @size and @alignment are + * passed to this function to allow optimizations in the allocator. If you + * don’t know either of them, use g_aligned_free() instead. + * + * Since: 2.76 + */ +void +g_aligned_free_sized (void *mem, + size_t alignment, + size_t size) +{ +#ifdef HAVE_FREE_ALIGNED_SIZED + free_aligned_sized (mem, alignment, size); +#else + aligned_free (mem); +#endif +} diff --git a/glib/gmem.h b/glib/gmem.h index bae51fa47..933d15922 100644 --- a/glib/gmem.h +++ b/glib/gmem.h @@ -72,6 +72,9 @@ typedef struct _GMemVTable GMemVTable; GLIB_AVAILABLE_IN_ALL void g_free (gpointer mem); +GLIB_AVAILABLE_IN_2_76 +void g_free_sized (gpointer mem, + size_t size); GLIB_AVAILABLE_IN_2_34 void g_clear_pointer (gpointer *pp, @@ -123,6 +126,10 @@ gpointer g_aligned_alloc0 (gsize n_blocks, gsize alignment) G_GNUC_WARN_UNUSED_RESULT G_GNUC_ALLOC_SIZE2(1,2); GLIB_AVAILABLE_IN_2_72 void g_aligned_free (gpointer mem); +GLIB_AVAILABLE_IN_2_76 +void g_aligned_free_sized (gpointer mem, + size_t alignment, + size_t size); #if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58 #define g_clear_pointer(pp, destroy) \ diff --git a/glib/tests/utils.c b/glib/tests/utils.c index 574333a4b..afa6cad9f 100644 --- a/glib/tests/utils.c +++ b/glib/tests/utils.c @@ -1142,6 +1142,39 @@ test_aligned_mem_zeroed (void) g_aligned_free (p); } +static void +test_aligned_mem_free_sized (void) +{ + gsize n_blocks = 10; + guint *p; + + g_test_summary ("Check that g_aligned_free_sized() works"); + + p = g_aligned_alloc (n_blocks, sizeof (*p), 16); + g_assert_nonnull (p); + + g_aligned_free_sized (p, sizeof (*p), n_blocks * 16); + + /* NULL should be ignored */ + g_aligned_free_sized (NULL, sizeof (*p), n_blocks * 16); +} + +static void +test_free_sized (void) +{ + gpointer p; + + g_test_summary ("Check that g_free_sized() works"); + + p = g_malloc (123); + g_assert_nonnull (p); + + g_free_sized (p, 123); + + /* NULL should be ignored */ + g_free_sized (NULL, 123); +} + static void test_nullify (void) { @@ -1317,6 +1350,8 @@ main (int argc, g_test_add_func ("/utils/aligned-mem/subprocess/aligned_alloc_nmov", aligned_alloc_nmov); g_test_add_func ("/utils/aligned-mem/alignment", test_aligned_mem_alignment); g_test_add_func ("/utils/aligned-mem/zeroed", test_aligned_mem_zeroed); + g_test_add_func ("/utils/aligned-mem/free-sized", test_aligned_mem_free_sized); + g_test_add_func ("/utils/free-sized", test_free_sized); g_test_add_func ("/utils/nullify", test_nullify); g_test_add_func ("/utils/atexit", test_atexit); g_test_add_func ("/utils/check-setuid", test_check_setuid); diff --git a/meson.build b/meson.build index 19d8b1777..3aef5b1ee 100644 --- a/meson.build +++ b/meson.build @@ -614,6 +614,8 @@ functions = [ 'fchmod', 'fchown', 'fdwalk', + 'free_aligned_sized', + 'free_sized', 'fsync', 'getauxval', 'getc_unlocked',