2009-09-08 12:08:10 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
|
|
|
* Copyright (C) 2002-2009 PCSX2 Dev Team
|
2009-09-21 09:48:31 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
2009-02-09 21:15:56 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
2009-02-09 21:15:56 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
2009-02-09 21:15:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// This module contains implementations of _aligned_malloc for platforms that don't have
|
|
|
|
// it built into their CRT/libc.
|
|
|
|
|
|
|
|
#include "PrecompiledHeader.h"
|
|
|
|
|
|
|
|
struct AlignedMallocHeader
|
|
|
|
{
|
|
|
|
u32 size; // size of the allocated buffer (minus alignment and header)
|
|
|
|
void* baseptr; // offset of the original allocated pointer
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint headsize = sizeof(AlignedMallocHeader);
|
|
|
|
|
|
|
|
void* __fastcall pcsx2_aligned_malloc(size_t size, size_t align)
|
|
|
|
{
|
2009-12-24 10:04:03 +00:00
|
|
|
pxAssume( align < 0x10000 );
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
u8* p = (u8*)malloc(size+align+headsize);
|
|
|
|
|
|
|
|
// start alignment calculations from past the header.
|
|
|
|
uptr pasthead = (uptr)(p+headsize);
|
|
|
|
uptr aligned = (pasthead + align-1) & ~(align-1);
|
|
|
|
|
|
|
|
AlignedMallocHeader* header = (AlignedMallocHeader*)(aligned-headsize);
|
|
|
|
jASSUME( (uptr)header >= (uptr)p );
|
|
|
|
|
|
|
|
header->baseptr = p;
|
|
|
|
header->size = size;
|
|
|
|
|
|
|
|
return (void*)aligned;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* __fastcall pcsx2_aligned_realloc(void* handle, size_t size, size_t align)
|
|
|
|
{
|
2009-12-24 10:04:03 +00:00
|
|
|
pxAssume( align < 0x10000 );
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
void* newbuf = pcsx2_aligned_malloc( size, align );
|
|
|
|
|
2009-12-24 10:04:03 +00:00
|
|
|
if( handle != NULL )
|
|
|
|
{
|
|
|
|
AlignedMallocHeader* header = (AlignedMallocHeader*)((uptr)handle - headsize);
|
|
|
|
memcpy_fast( newbuf, handle, std::min( size, header->size ) );
|
|
|
|
free( header->baseptr );
|
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
return newbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline void pcsx2_aligned_free(void* pmem)
|
|
|
|
{
|
|
|
|
if( pmem == NULL ) return;
|
|
|
|
AlignedMallocHeader* header = (AlignedMallocHeader*)((uptr)pmem - headsize);
|
|
|
|
free( header->baseptr );
|
|
|
|
}
|
2009-09-12 02:58:22 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// And for lack of a better home ...
|
|
|
|
|
|
|
|
|
|
|
|
// Special unaligned memset used when all other optimized memsets fail (it's called from
|
|
|
|
// memzero_obj and stuff).
|
2009-09-21 09:48:31 +00:00
|
|
|
__forceinline void _memset16_unaligned( void* dest, u16 data, size_t size )
|
2009-09-12 02:58:22 +00:00
|
|
|
{
|
2009-12-24 10:04:03 +00:00
|
|
|
pxAssume( (size & 0x1) == 0 );
|
2009-09-12 02:58:22 +00:00
|
|
|
|
|
|
|
u16* dst = (u16*)dest;
|
|
|
|
for(int i=size; i; --i, ++dst )
|
|
|
|
*dst = data;
|
2009-09-21 09:48:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline void HostSys::Munmap( void* base, u32 size )
|
|
|
|
{
|
|
|
|
Munmap( (uptr)base, size );
|
|
|
|
}
|