mirror of https://github.com/encounter/SDL.git
Add SDL_SIMDRealloc
This commit is contained in:
parent
20aada0efc
commit
2aa8974f97
|
@ -246,10 +246,33 @@ extern DECLSPEC size_t SDLCALL SDL_SIMDGetAlignment(void);
|
||||||
* \return Pointer to newly-allocated block, NULL if out of memory.
|
* \return Pointer to newly-allocated block, NULL if out of memory.
|
||||||
*
|
*
|
||||||
* \sa SDL_SIMDAlignment
|
* \sa SDL_SIMDAlignment
|
||||||
|
* \sa SDL_SIMDRealloc
|
||||||
* \sa SDL_SIMDFree
|
* \sa SDL_SIMDFree
|
||||||
*/
|
*/
|
||||||
extern DECLSPEC void * SDLCALL SDL_SIMDAlloc(const size_t len);
|
extern DECLSPEC void * SDLCALL SDL_SIMDAlloc(const size_t len);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Reallocate memory obtained from SDL_SIMDAlloc
|
||||||
|
*
|
||||||
|
* It is not valid to use this function on a pointer from anything but
|
||||||
|
* SDL_SIMDAlloc(). It can't be used on pointers from malloc, realloc,
|
||||||
|
* SDL_malloc, memalign, new[], etc.
|
||||||
|
*
|
||||||
|
* \param mem The pointer obtained from SDL_SIMDAlloc. This function also
|
||||||
|
* accepts NULL, at which point this function is the same as
|
||||||
|
* calling SDL_realloc with a NULL pointer.
|
||||||
|
* \param len The length, in bytes, of the block to allocated. The actual
|
||||||
|
* allocated block might be larger due to padding, etc. Passing 0
|
||||||
|
* will return a non-NULL pointer, assuming the system isn't out of
|
||||||
|
* memory.
|
||||||
|
* \return Pointer to newly-reallocated block, NULL if out of memory.
|
||||||
|
*
|
||||||
|
* \sa SDL_SIMDAlignment
|
||||||
|
* \sa SDL_SIMDAlloc
|
||||||
|
* \sa SDL_SIMDFree
|
||||||
|
*/
|
||||||
|
extern DECLSPEC void * SDLCALL SDL_SIMDRealloc(void *mem, size_t len);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Deallocate memory obtained from SDL_SIMDAlloc
|
* \brief Deallocate memory obtained from SDL_SIMDAlloc
|
||||||
*
|
*
|
||||||
|
@ -260,6 +283,7 @@ extern DECLSPEC void * SDLCALL SDL_SIMDAlloc(const size_t len);
|
||||||
* However, SDL_SIMDFree(NULL) is a legal no-op.
|
* However, SDL_SIMDFree(NULL) is a legal no-op.
|
||||||
*
|
*
|
||||||
* \sa SDL_SIMDAlloc
|
* \sa SDL_SIMDAlloc
|
||||||
|
* \sa SDL_SIMDRealloc
|
||||||
*/
|
*/
|
||||||
extern DECLSPEC void SDLCALL SDL_SIMDFree(void *ptr);
|
extern DECLSPEC void SDLCALL SDL_SIMDFree(void *ptr);
|
||||||
|
|
||||||
|
|
|
@ -956,6 +956,58 @@ SDL_SIMDAlloc(const size_t len)
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *
|
||||||
|
SDL_SIMDRealloc(void *mem, const size_t len)
|
||||||
|
{
|
||||||
|
const size_t alignment = SDL_SIMDGetAlignment();
|
||||||
|
const size_t padding = alignment - (len % alignment);
|
||||||
|
const size_t padded = (padding != alignment) ? (len + padding) : len;
|
||||||
|
Uint8 *retval = (Uint8*) mem;
|
||||||
|
void *oldmem = mem;
|
||||||
|
size_t memdiff, ptrdiff;
|
||||||
|
Uint8 *ptr;
|
||||||
|
|
||||||
|
if (mem) {
|
||||||
|
void **realptr = (void **) mem;
|
||||||
|
realptr--;
|
||||||
|
mem = *(((void **) mem) - 1);
|
||||||
|
|
||||||
|
/* Check the delta between the real pointer and user pointer */
|
||||||
|
memdiff = ((size_t) oldmem) - ((size_t) mem);
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr = (Uint8 *) SDL_realloc(mem, padded + alignment + sizeof (void *));
|
||||||
|
|
||||||
|
if (ptr == mem) {
|
||||||
|
return retval; /* Pointer didn't change, nothing to do */
|
||||||
|
}
|
||||||
|
if (ptr == NULL) {
|
||||||
|
return NULL; /* Out of memory, bail! */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Store the actual malloc pointer right before our aligned pointer. */
|
||||||
|
retval = ptr + sizeof (void *);
|
||||||
|
retval += alignment - (((size_t) retval) % alignment);
|
||||||
|
|
||||||
|
/* Make sure the delta is the same! */
|
||||||
|
if (mem) {
|
||||||
|
ptrdiff = ((size_t) retval) - ((size_t) ptr);
|
||||||
|
if (memdiff != ptrdiff) { /* Delta has changed, copy to new offset! */
|
||||||
|
oldmem = (void*) (((size_t) ptr) + memdiff);
|
||||||
|
|
||||||
|
/* Even though the data past the old `len` is undefined, this is the
|
||||||
|
* only length value we have, and it guarantees that we copy all the
|
||||||
|
* previous memory anyhow.
|
||||||
|
*/
|
||||||
|
SDL_memmove(retval, oldmem, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Actually store the malloc pointer, finally. */
|
||||||
|
*(((void **) retval) - 1) = ptr;
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
SDL_SIMDFree(void *ptr)
|
SDL_SIMDFree(void *ptr)
|
||||||
{
|
{
|
||||||
|
|
|
@ -716,6 +716,7 @@
|
||||||
#define SDL_UIKitRunApp SDL_UIKitRunApp_REAL
|
#define SDL_UIKitRunApp SDL_UIKitRunApp_REAL
|
||||||
#define SDL_SIMDGetAlignment SDL_SIMDGetAlignment_REAL
|
#define SDL_SIMDGetAlignment SDL_SIMDGetAlignment_REAL
|
||||||
#define SDL_SIMDAlloc SDL_SIMDAlloc_REAL
|
#define SDL_SIMDAlloc SDL_SIMDAlloc_REAL
|
||||||
|
#define SDL_SIMDRealloc SDL_SIMDRealloc_REAL
|
||||||
#define SDL_SIMDFree SDL_SIMDFree_REAL
|
#define SDL_SIMDFree SDL_SIMDFree_REAL
|
||||||
#define SDL_RWsize SDL_RWsize_REAL
|
#define SDL_RWsize SDL_RWsize_REAL
|
||||||
#define SDL_RWseek SDL_RWseek_REAL
|
#define SDL_RWseek SDL_RWseek_REAL
|
||||||
|
|
|
@ -772,6 +772,7 @@ SDL_DYNAPI_PROC(int,SDL_UIKitRunApp,(int a, char *b, SDL_main_func c),(a,b,c),re
|
||||||
#endif
|
#endif
|
||||||
SDL_DYNAPI_PROC(size_t,SDL_SIMDGetAlignment,(void),(),return)
|
SDL_DYNAPI_PROC(size_t,SDL_SIMDGetAlignment,(void),(),return)
|
||||||
SDL_DYNAPI_PROC(void*,SDL_SIMDAlloc,(const size_t a),(a),return)
|
SDL_DYNAPI_PROC(void*,SDL_SIMDAlloc,(const size_t a),(a),return)
|
||||||
|
SDL_DYNAPI_PROC(void*,SDL_SIMDRealloc,(void *a, const size_t b),(a, b),return)
|
||||||
SDL_DYNAPI_PROC(void,SDL_SIMDFree,(void *a),(a),)
|
SDL_DYNAPI_PROC(void,SDL_SIMDFree,(void *a),(a),)
|
||||||
SDL_DYNAPI_PROC(Sint64,SDL_RWsize,(SDL_RWops *a),(a),return)
|
SDL_DYNAPI_PROC(Sint64,SDL_RWsize,(SDL_RWops *a),(a),return)
|
||||||
SDL_DYNAPI_PROC(Sint64,SDL_RWseek,(SDL_RWops *a, Sint64 b, int c),(a,b,c),return)
|
SDL_DYNAPI_PROC(Sint64,SDL_RWseek,(SDL_RWops *a, Sint64 b, int c),(a,b,c),return)
|
||||||
|
|
Loading…
Reference in New Issue