From df8f98982552112039f66b5f1639599e423c6800 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Fri, 28 May 2021 15:47:37 -0400 Subject: [PATCH] use malloc compiler attribute if available This informs the compiler that these functions return allocated memory. That allows it to make some optimization decisions which can produce better code. --- src/gd.h | 2 ++ src/gdhelpers.h | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/gd.h b/src/gd.h index 89bca20..a1e1393 100644 --- a/src/gd.h +++ b/src/gd.h @@ -65,6 +65,7 @@ extern "C" { # endif # define BGD_STDCALL __stdcall # define BGD_EXPORT_DATA_IMPL +# define BGD_MALLOC #else # if defined(__GNUC__) || defined(__clang__) # define BGD_EXPORT_DATA_PROT __attribute__ ((__visibility__ ("default"))) @@ -74,6 +75,7 @@ extern "C" { # define BGD_EXPORT_DATA_IMPL # endif # define BGD_STDCALL +# define BGD_MALLOC __attribute__ ((__malloc__)) #endif #define BGD_DECLARE(rt) BGD_EXPORT_DATA_PROT rt BGD_STDCALL diff --git a/src/gdhelpers.h b/src/gdhelpers.h index 687e156..9b187af 100644 --- a/src/gdhelpers.h +++ b/src/gdhelpers.h @@ -20,8 +20,8 @@ extern "C" { in gd.h, where callers can utilize it to correctly free memory allocated by these functions with the right version of free(). */ - void *gdCalloc (size_t nmemb, size_t size); - void *gdMalloc (size_t size); + void *gdCalloc(size_t nmemb, size_t size) BGD_MALLOC; + void *gdMalloc(size_t size) BGD_MALLOC; void *gdRealloc (void *ptr, size_t size); /* The extended version of gdReallocEx will free *ptr if the * realloc fails */