SDL: alloc api having new attributes for micro optimisations.

From 975ffaea77bb57fd3b144046a675ac6beb6464dc Mon Sep 17 00:00:00 2001
From: David Carlier <[EMAIL REDACTED]>
Date: Sun, 4 Sep 2022 15:38:53 +0100
Subject: [PATCH] alloc api having new attributes for micro optimisations.

- SDL_MALLOC when the pointer returned is not aliased.
- SDL_ALLOC_SIZE* to gives hint about the size.
---
 include/SDL_stdinc.h |  8 ++++----
 include/begin_code.h | 30 ++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/include/SDL_stdinc.h b/include/SDL_stdinc.h
index 5183fd5fb8c1..689f23efda6e 100644
--- a/include/SDL_stdinc.h
+++ b/include/SDL_stdinc.h
@@ -436,9 +436,9 @@ extern "C" {
 #define SDL_stack_free(data)            SDL_free(data)
 #endif
 
-extern DECLSPEC void *SDLCALL SDL_malloc(size_t size);
-extern DECLSPEC void *SDLCALL SDL_calloc(size_t nmemb, size_t size);
-extern DECLSPEC void *SDLCALL SDL_realloc(void *mem, size_t size);
+extern SDL_MALLOC DECLSPEC void *SDLCALL SDL_malloc(size_t size);
+extern SDL_MALLOC SDL_ALLOC_SIZE2(1, 2) DECLSPEC void *SDLCALL SDL_calloc(size_t nmemb, size_t size);
+extern SDL_ALLOC_SIZE(2) DECLSPEC void *SDLCALL SDL_realloc(void *mem, size_t size);
 extern DECLSPEC void SDLCALL SDL_free(void *mem);
 
 typedef void *(SDLCALL *SDL_malloc_func)(size_t size);
@@ -576,7 +576,7 @@ extern DECLSPEC size_t SDLCALL SDL_strlen(const char *str);
 extern DECLSPEC size_t SDLCALL SDL_strlcpy(SDL_OUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
 extern DECLSPEC size_t SDLCALL SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_bytes);
 extern DECLSPEC size_t SDLCALL SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
-extern DECLSPEC char *SDLCALL SDL_strdup(const char *str);
+extern SDL_MALLOC DECLSPEC char *SDLCALL SDL_strdup(const char *str);
 extern DECLSPEC char *SDLCALL SDL_strrev(char *str);
 extern DECLSPEC char *SDLCALL SDL_strupr(char *str);
 extern DECLSPEC char *SDLCALL SDL_strlwr(char *str);
diff --git a/include/begin_code.h b/include/begin_code.h
index 3aaf9dab6f88..bab69a145e82 100644
--- a/include/begin_code.h
+++ b/include/begin_code.h
@@ -174,3 +174,33 @@
 #undef _HAS_FALLTHROUGH
 #endif /* C++17 or C2x */
 #endif /* SDL_FALLTHROUGH not defined */
+
+#ifndef SDL_MALLOC
+#if defined(__GNUC__)
+#define SDL_MALLOC __attribute__((malloc))
+#elif defined(_MSC_VER)
+#define SDL_MALLOC __declspec(allocator) __desclspec(restrict)
+#else
+#define SDL_MALLOC
+#endif
+#endif /* SDL_MALLOC not defined */
+
+#ifndef SDL_ALLOC_SIZE
+#if defined(__GNUC__)
+#define SDL_ALLOC_SIZE(p) __attribute__((alloc_size(p)))
+#elif defined(_MSC_VER)
+#define SDL_ALLOC_SIZE(p)
+#else
+#define SDL_ALLOC_SIZE(p)
+#endif
+#endif /* SDL_ALLOC_SIZE not defined */
+
+#ifndef SDL_ALLOC_SIZE2
+#if defined(__GNUC__)
+#define SDL_ALLOC_SIZE2(p1, p2) __attribute__((alloc_size(p1, p2)))
+#elif defined(_MSC_VER)
+#define SDL_ALLOC_SIZE2(p1, p2)
+#else
+#define SDL_ALLOC_SIZE2(p1, p2)
+#endif
+#endif /* SDL_ALLOC_SIZE2 not defined */