stlib: Extract SDL_memcpy and SDL_memset to its own file respectively This is done such that we can disable LTO for these 2 functions when building with MSVC. This is due to a limitation of Link Time Code Generation (LTCG). Code generation might generate a new reference to memset after linking has started. The LTCG must make assumptions about where memset is defined which is normally the C runtime.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7982b91..5beaf3b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -649,6 +649,11 @@ if(MSVC)
target_compile_definitions(sdl-build-options INTERFACE "-D_CRT_SECURE_NO_WARNINGS")
endif()
+if(MSVC)
+ # Due to a limitation of Microsoft's LTO implementation, LTO must be disabled for memcpy and memset.
+ set_property(SOURCE src/stdlib/SDL_memcpy.c src/stdlib/SDL_memset.c APPEND PROPERTY COMPILE_FLAGS /GL-)
+endif()
+
if(SDL_ASSEMBLY)
if(USE_GCC OR USE_CLANG)
# TODO: Those all seem to be quite GCC specific - needs to be
diff --git a/Makefile.os2 b/Makefile.os2
index 14b4953..0b1b13a 100644
--- a/Makefile.os2
+++ b/Makefile.os2
@@ -68,7 +68,7 @@ CFLAGS_DLL+= -DSDL_BUILD_MINOR_VERSION=$(MINOR_VERSION)
CFLAGS_DLL+= -DSDL_BUILD_MICRO_VERSION=$(MICRO_VERSION)
SRCS = SDL.c SDL_assert.c SDL_error.c SDL_guid.c SDL_log.c SDL_dataqueue.c SDL_hints.c SDL_list.c
-SRCS+= SDL_getenv.c SDL_iconv.c SDL_malloc.c SDL_qsort.c SDL_stdlib.c SDL_string.c SDL_strtokr.c SDL_crc32.c
+SRCS+= SDL_getenv.c SDL_iconv.c SDL_malloc.c SDL_memcpy.c SDL_memset.c SDL_qsort.c SDL_stdlib.c SDL_string.c SDL_strtokr.c SDL_crc32.c
SRCS+= SDL_cpuinfo.c SDL_atomic.c SDL_spinlock.c SDL_thread.c SDL_timer.c
SRCS+= SDL_rwops.c SDL_power.c
SRCS+= SDL_audio.c SDL_audiocvt.c SDL_audiodev.c SDL_audiotypecvt.c SDL_mixer.c SDL_wave.c
diff --git a/Makefile.w32 b/Makefile.w32
index fe19890..2a18692 100644
--- a/Makefile.w32
+++ b/Makefile.w32
@@ -44,7 +44,7 @@ CFLAGS_DLL+= -DSDL_BUILD_MICRO_VERSION=$(MICRO_VERSION)
RCFLAGS = -q -r -bt=nt $(INCPATH)
SRCS = SDL.c SDL_assert.c SDL_error.c SDL_guid.c SDL_log.c SDL_dataqueue.c SDL_hints.c SDL_list.c
-SRCS+= SDL_getenv.c SDL_iconv.c SDL_malloc.c SDL_qsort.c SDL_stdlib.c SDL_string.c SDL_strtokr.c SDL_crc32.c
+SRCS+= SDL_getenv.c SDL_iconv.c SDL_malloc.c SDL_memcpy.c SDL_memset.c SDL_qsort.c SDL_stdlib.c SDL_string.c SDL_strtokr.c SDL_crc32.c
SRCS+= SDL_cpuinfo.c SDL_atomic.c SDL_spinlock.c SDL_thread.c SDL_timer.c
SRCS+= SDL_rwops.c SDL_power.c
SRCS+= SDL_audio.c SDL_audiocvt.c SDL_audiodev.c SDL_audiotypecvt.c SDL_mixer.c SDL_wave.c
diff --git a/VisualC-WinRT/SDL-UWP.vcxproj b/VisualC-WinRT/SDL-UWP.vcxproj
index 80c8379..a4d7744 100644
--- a/VisualC-WinRT/SDL-UWP.vcxproj
+++ b/VisualC-WinRT/SDL-UWP.vcxproj
@@ -311,6 +311,8 @@
<ClCompile Include="..\src\stdlib\SDL_getenv.c" />
<ClCompile Include="..\src\stdlib\SDL_iconv.c" />
<ClCompile Include="..\src\stdlib\SDL_malloc.c" />
+ <ClCompile Include="..\src\stdlib\SDL_memcpy.c" />
+ <ClCompile Include="..\src\stdlib\SDL_memset.c" />
<ClCompile Include="..\src\stdlib\SDL_qsort.c" />
<ClCompile Include="..\src\stdlib\SDL_stdlib.c" />
<ClCompile Include="..\src\stdlib\SDL_string.c" />
diff --git a/VisualC/SDL/SDL.vcxproj b/VisualC/SDL/SDL.vcxproj
index 04ed6b4..013ca02 100644
--- a/VisualC/SDL/SDL.vcxproj
+++ b/VisualC/SDL/SDL.vcxproj
@@ -562,6 +562,8 @@
<ClCompile Include="..\..\src\stdlib\SDL_getenv.c" />
<ClCompile Include="..\..\src\stdlib\SDL_iconv.c" />
<ClCompile Include="..\..\src\stdlib\SDL_malloc.c" />
+ <ClCompile Include="..\..\src\stdlib\SDL_memcpy.c" />
+ <ClCompile Include="..\..\src\stdlib\SDL_memset.c" />
<ClCompile Include="..\..\src\stdlib\SDL_qsort.c" />
<ClCompile Include="..\..\src\stdlib\SDL_stdlib.c" />
<ClCompile Include="..\..\src\stdlib\SDL_string.c" />
diff --git a/src/stdlib/SDL_memcpy.c b/src/stdlib/SDL_memcpy.c
new file mode 100644
index 0000000..bedbce8
--- /dev/null
+++ b/src/stdlib/SDL_memcpy.c
@@ -0,0 +1,79 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#include "../SDL_internal.h"
+
+/* This file contains a portable memcpy manipulation function for SDL */
+
+void *
+SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len)
+{
+#ifdef __GNUC__
+ /* Presumably this is well tuned for speed.
+ On my machine this is twice as fast as the C code below.
+ */
+ return __builtin_memcpy(dst, src, len);
+#elif defined(HAVE_MEMCPY)
+ return memcpy(dst, src, len);
+#elif defined(HAVE_BCOPY)
+ bcopy(src, dst, len);
+ return dst;
+#else
+ /* GCC 4.9.0 with -O3 will generate movaps instructions with the loop
+ using Uint32* pointers, so we need to make sure the pointers are
+ aligned before we loop using them.
+ */
+ if (((uintptr_t)src & 0x3) || ((uintptr_t)dst & 0x3)) {
+ /* Do an unaligned byte copy */
+ Uint8 *srcp1 = (Uint8 *)src;
+ Uint8 *dstp1 = (Uint8 *)dst;
+
+ while (len--) {
+ *dstp1++ = *srcp1++;
+ }
+ } else {
+ size_t left = (len % 4);
+ Uint32 *srcp4, *dstp4;
+ Uint8 *srcp1, *dstp1;
+
+ srcp4 = (Uint32 *) src;
+ dstp4 = (Uint32 *) dst;
+ len /= 4;
+ while (len--) {
+ *dstp4++ = *srcp4++;
+ }
+
+ srcp1 = (Uint8 *) srcp4;
+ dstp1 = (Uint8 *) dstp4;
+ switch (left) {
+ case 3:
+ *dstp1++ = *srcp1++;
+ case 2:
+ *dstp1++ = *srcp1++;
+ case 1:
+ *dstp1++ = *srcp1++;
+ }
+ }
+ return dst;
+#endif /* __GNUC__ */
+}
+
+/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/stdlib/SDL_memset.c b/src/stdlib/SDL_memset.c
new file mode 100644
index 0000000..c2a4875
--- /dev/null
+++ b/src/stdlib/SDL_memset.c
@@ -0,0 +1,75 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#include "../SDL_internal.h"
+
+/* This file contains a portable memset manipulation function for SDL */
+
+void *
+SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len)
+{
+#if defined(HAVE_MEMSET)
+ return memset(dst, c, len);
+#else
+ size_t left;
+ Uint32 *dstp4;
+ Uint8 *dstp1 = (Uint8 *) dst;
+ Uint8 value1;
+ Uint32 value4;
+
+ /* The value used in memset() is a byte, passed as an int */
+ c &= 0xff;
+
+ /* The destination pointer needs to be aligned on a 4-byte boundary to
+ * execute a 32-bit set. Set first bytes manually if needed until it is
+ * aligned. */
+ value1 = (Uint8)c;
+ while ((uintptr_t)dstp1 & 0x3) {
+ if (len--) {
+ *dstp1++ = value1;
+ } else {
+ return dst;
+ }
+ }
+
+ value4 = ((Uint32)c | ((Uint32)c << 8) | ((Uint32)c << 16) | ((Uint32)c << 24));
+ dstp4 = (Uint32 *) dstp1;
+ left = (len % 4);
+ len /= 4;
+ while (len--) {
+ *dstp4++ = value4;
+ }
+
+ dstp1 = (Uint8 *) dstp4;
+ switch (left) {
+ case 3:
+ *dstp1++ = value1;
+ case 2:
+ *dstp1++ = value1;
+ case 1:
+ *dstp1++ = value1;
+ }
+
+ return dst;
+#endif /* HAVE_MEMSET */
+}
+
+/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c
index 77e0d30..943e727 100644
--- a/src/stdlib/SDL_string.c
+++ b/src/stdlib/SDL_string.c
@@ -264,108 +264,6 @@ SDL_ScanFloat(const char *text, double *valuep)
#endif
void *
-SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len)
-{
-#if defined(HAVE_MEMSET)
- return memset(dst, c, len);
-#else
- size_t left;
- Uint32 *dstp4;
- Uint8 *dstp1 = (Uint8 *) dst;
- Uint8 value1;
- Uint32 value4;
-
- /* The value used in memset() is a byte, passed as an int */
- c &= 0xff;
-
- /* The destination pointer needs to be aligned on a 4-byte boundary to
- * execute a 32-bit set. Set first bytes manually if needed until it is
- * aligned. */
- value1 = (Uint8)c;
- while ((uintptr_t)dstp1 & 0x3) {
- if (len--) {
- *dstp1++ = value1;
- } else {
- return dst;
- }
- }
-
- value4 = ((Uint32)c | ((Uint32)c << 8) | ((Uint32)c << 16) | ((Uint32)c << 24));
- dstp4 = (Uint32 *) dstp1;
- left = (len % 4);
- len /= 4;
- while (len--) {
- *dstp4++ = value4;
- }
-
- dstp1 = (Uint8 *) dstp4;
- switch (left) {
- case 3:
- *dstp1++ = value1;
- case 2:
- *dstp1++ = value1;
- case 1:
- *dstp1++ = value1;
- }
-
- return dst;
-#endif /* HAVE_MEMSET */
-}
-
-void *
-SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len)
-{
-#ifdef __GNUC__
- /* Presumably this is well tuned for speed.
- On my machine this is twice as fast as the C code below.
- */
- return __builtin_memcpy(dst, src, len);
-#elif defined(HAVE_MEMCPY)
- return memcpy(dst, src, len);
-#elif defined(HAVE_BCOPY)
- bcopy(src, dst, len);
- return dst;
-#else
- /* GCC 4.9.0 with -O3 will generate movaps instructions with the loop
- using Uint32* pointers, so we need to make sure the pointers are
- aligned before we loop using them.
- */
- if (((uintptr_t)src & 0x3) || ((uintptr_t)dst & 0x3)) {
- /* Do an unaligned byte copy */
- Uint8 *srcp1 = (Uint8 *)src;
- Uint8 *dstp1 = (Uint8 *)dst;
-
- while (len--) {
- *dstp1++ = *srcp1++;
- }
- } else {
- size_t left = (len % 4);
- Uint32 *srcp4, *dstp4;
- Uint8 *srcp1, *dstp1;
-
- srcp4 = (Uint32 *) src;
- dstp4 = (Uint32 *) dst;
- len /= 4;
- while (len--) {
- *dstp4++ = *srcp4++;
- }
-
- srcp1 = (Uint8 *) srcp4;
- dstp1 = (Uint8 *) dstp4;
- switch (left) {
- case 3:
- *dstp1++ = *srcp1++;
- case 2:
- *dstp1++ = *srcp1++;
- case 1:
- *dstp1++ = *srcp1++;
- }
- }
- return dst;
-#endif /* __GNUC__ */
-}
-
-void *
SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len)
{
#if defined(HAVE_MEMMOVE)