We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 5185

Summary: [Patch] Add SDL_SIMDRealloc
Product: SDL Reporter: Ethan Lee <flibitijibibo>
Component: *don't know*Assignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: enhancement    
Priority: P2 Keywords: target-2.0.14
Version: HG 2.0   
Hardware: All   
OS: All   
Attachments: SDL_SIMDRealloc
SDL_SIMDRealloc v2
SDL_SIMDRealloc v3
SDL_SIMDRealloc v4

Description Ethan Lee 2020-06-11 14:25:25 UTC
Created attachment 4376 [details]
SDL_SIMDRealloc

Attached is a patch that adds SDL_SIMDRealloc, allowing for reallocation of SIMDAlloc pointers without freeing every time. This function is particularly nice for when the buffer size ends up being the same, which can happen pretty frequently when the buffer in question is consistently smaller than the padding/alignment size.

The implementation itself is pretty much a mishmashed copypaste of SIMDAlloc and SIMDFree (without the free, of course), down to the documentation.
Comment 1 Ethan Lee 2020-06-11 15:28:18 UTC
Created attachment 4377 [details]
SDL_SIMDRealloc v2
Comment 2 Ryan C. Gordon 2020-06-11 15:34:07 UTC
This seems reasonable to me. I’ll commit it when I get to the office.

--ryan.
Comment 3 Ethan Lee 2020-06-11 15:58:53 UTC
Created attachment 4378 [details]
SDL_SIMDRealloc v3

One more update, this function now does some additional work to make sure that the buffer contents remain the same if A: the pointer changes, and B: the delta between the real pointer and user pointer changes. Pretty narrow case, but it is possible to hit it with some effort!
Comment 4 Ethan Lee 2020-06-11 16:04:56 UTC
Created attachment 4379 [details]
SDL_SIMDRealloc v4

v4 is just v3 with some aggressive warning fixes... (sorry for all the spam, the delta case caught me by surprise!)
Comment 5 Sam Lantinga 2020-06-11 19:18:57 UTC
Added, thanks!
https://hg.libsdl.org/SDL/rev/f7db85ea3b3c
Comment 6 Ryan C. Gordon 2020-06-11 19:32:12 UTC
Wait, wait,

SDL_memmove(retval, oldmem, len) will cause a Valgrind warning if the buffer is growing, as it will copy uninitialized memory, I think. We could store the allocated length along with the malloc() pointer to avoid this (which also lets us return the original pointer immediately if the length >= the newly requested length).

--ryan.
Comment 7 Ethan Lee 2020-06-11 19:54:04 UTC
Is there a specific warning you have to enable to make the warning show up? I was running my test case with Valgrind 3.16 and it seems to be okay (minus the obvious large allocs that my test is doing):

--------------------------------------------------------------------------------

int main(int argc, char **argv)
{
	size_t size = SDL_SIMDGetAlignment();
	char *barf = (char*) SDL_SIMDAlloc(size);
	SDL_strlcpy(barf, "barf", size);
	while (size < 1073741824)
	{
		size *= 2;
		barf = (char*) SDL_SIMDRealloc(barf, size);
		SDL_assert(SDL_strcmp(barf, "barf") == 0);
		printf("%zu %p %s\n", size, barf, barf);
	}
	SDL_SIMDFree(barf);
	return 0;
}

--------------------------------------------------------------------------------

[flibitijibibo@flibitAMD flibitBuild]$ valgrind ./a.out 
==87176== Memcheck, a memory error detector
==87176== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==87176== Using Valgrind-3.16.0 and LibVEX; rerun with -h for copyright info
==87176== Command: ./a.out
==87176== 
64 0x4d240e0 barf
128 0x4d245e0 barf
256 0x4d246c0 barf
512 0x4d24840 barf
1024 0x4d24aa0 barf
2048 0x4d24f20 barf
4096 0x4d25780 barf
8192 0x4d26800 barf
16384 0x4d28860 barf
32768 0x4d2c8e0 barf
65536 0x4d34940 barf
131072 0x4d449c0 barf
262144 0x4d64a20 barf
524288 0x4da4aa0 barf
1048576 0x4e24b00 barf
2097152 0x5124060 barf
4194304 0x5524060 barf
8388608 0x5925060 barf
16777216 0x6126060 barf
33554432 0x7127060 barf
67108864 0x9128060 barf
134217728 0xd129060 barf
268435456 0x1512a060 barf
==87176== Warning: set address range perms: large range [0x1512a028, 0x2512a080) (noaccess)
536870912 0x2512b060 barf
==87176== Warning: set address range perms: large range [0x79c89068, 0x99c89068) (undefined)
==87176== Warning: set address range perms: large range [0x2512b028, 0x4512b080) (noaccess)
1073741824 0x59c89060 barf
==87176== Warning: set address range perms: large range [0x59c89028, 0x99c89080) (noaccess)
==87176== 
==87176== HEAP SUMMARY:
==87176==     in use at exit: 0 bytes in 0 blocks
==87176==   total heap usage: 27 allocs, 27 frees, 2,147,485,680 bytes allocated
==87176== 
==87176== All heap blocks were freed -- no leaks are possible
==87176== 
==87176== For lists of detected and suppressed errors, rerun with: -s
==87176== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

--------------------------------------------------------------------------------
Comment 8 Ryan C. Gordon 2020-06-26 18:09:48 UTC
Weird, I can't make Valgrind trigger either, although I'm certain it should. I'm going to close this bug for now, we'll revisit if it becomes an issue.

--ryan.