Skip to content

Commit

Permalink
Move realloc64 calls to SMS_ReallocWithAlign
Browse files Browse the repository at this point in the history
  • Loading branch information
uyjulian committed Jun 22, 2022
1 parent ebda3eb commit 98d010f
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 16 deletions.
1 change: 1 addition & 0 deletions include/SMS.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ extern "C" {
#endif /* __cplusplus */
void SMS_Initialize ( void* );
void* SMS_Realloc ( void*, unsigned int*, unsigned int );
void* SMS_ReallocWithAlign ( void*, unsigned int*, unsigned int );
uint32_t SMS_Align ( unsigned int, unsigned int );
void SMS_SetSifCmdHandler ( void ( * ) ( void* ), int );
int64_t SMS_Rescale ( int64_t, int64_t, int64_t );
Expand Down
1 change: 1 addition & 0 deletions include/SMS_FileContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ typedef struct FileContext {
unsigned int m_Pos;
unsigned int m_CurPos;
unsigned int m_BufSize;
unsigned int m_CurBufSize;
unsigned char* m_pBuff[ 2 ];
unsigned char* m_pPos;
unsigned char* m_pEnd;
Expand Down
28 changes: 28 additions & 0 deletions src/SMS_EE.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,34 @@ void* SMS_Realloc ( void* apData, unsigned int* apSize, unsigned int aMinSize )

} /* SMS_Realloc */

void* SMS_ReallocWithAlign ( void* apData, unsigned int* apSize, unsigned int aMinSize ) {

unsigned int lnOldSize;
void *lpNewData;

lnOldSize = *apSize;

if ( apData != NULL && aMinSize < lnOldSize ) return apData;
if ( aMinSize == 0 ) return apData;

lpNewData = memalign(64, aMinSize);

if ( lpNewData == NULL ) {
aMinSize = 0;
}
if ( apData != NULL ) {
if ( lpNewData != NULL ) {
memcpy( lpNewData, apData, lnOldSize );
}
free( apData );
}

*apSize = aMinSize;

return lpNewData;

} /* SMS_ReallocWithAlignment */

extern s64 MUL64 ( s64 , s64 );

s64 SMS_Rescale ( s64 anA, s64 aB, s64 aC ){
Expand Down
8 changes: 4 additions & 4 deletions src/SMS_FileContext.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,8 @@ static int CDDA_Stream ( FileContext* apCtx, unsigned int aStartPos, unsigned in

apCtx -> m_BufSize = anSectors * 2352;

apCtx -> m_pBuff[ 0 ] = realloc( apCtx -> m_pBuff[ 0 ], ( apCtx -> m_BufSize + 63 ) & ~63 );
apCtx -> m_pBuff[ 1 ] = realloc( apCtx -> m_pBuff[ 1 ], ( apCtx -> m_BufSize + 63 ) & ~63 );
apCtx -> m_pBuff[ 0 ] = SMS_ReallocWithAlign( apCtx -> m_pBuff[ 0 ], &( apCtx -> m_CurBufSize ), ( apCtx -> m_BufSize + 63 ) & ~63 );
apCtx -> m_pBuff[ 1 ] = SMS_ReallocWithAlign( apCtx -> m_pBuff[ 1 ], &( apCtx -> m_CurBufSize ), ( apCtx -> m_BufSize + 63 ) & ~63 );

if ( apCtx -> m_pBuff[ 0 ] == NULL || apCtx -> m_pBuff[ 1 ] == NULL ) return 0;

Expand Down Expand Up @@ -1411,8 +1411,8 @@ static int STIO_Stream ( FileContext* apCtx, unsigned int aStartPos, unsigned in

apCtx -> m_BufSize = anBlocks * 4096;

lpData = realloc( apCtx -> m_pBuff[ 0 ], ( apCtx -> m_BufSize + 63 ) & ~63 );
apCtx -> m_pBuff[ 1 ] = realloc( apCtx -> m_pBuff[ 1 ], ( apCtx -> m_BufSize + 63 ) & ~63 );
lpData = SMS_ReallocWithAlign( apCtx -> m_pBuff[ 0 ], &( apCtx -> m_CurBufSize ), ( apCtx -> m_BufSize + 63 ) & ~63 );
apCtx -> m_pBuff[ 1 ] = SMS_ReallocWithAlign( apCtx -> m_pBuff[ 1 ], &( apCtx -> m_CurBufSize ), ( apCtx -> m_BufSize + 63 ) & ~63 );

if ( lpData == NULL || apCtx -> m_pBuff[ 1 ] == NULL ) return 0;

Expand Down
29 changes: 19 additions & 10 deletions src/SMS_GS_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,18 @@ u64* GSContext_NewPacket ( int aCtx, int aDWC, GSPaintMethod aMethod )
int lPutIdx = g_GSCtx.m_PutIndex[ aCtx ];
unsigned int lQWC = ( aDWC + 1 ) >> 1;
u64* lpList = g_GSCtx.m_pDisplayList[ aCtx ];
unsigned int lOldAllocSize;

DMA_Wait ( DMAC_VIF1 );

lOldAllocSize = ( g_GSCtx.m_nAlloc[ aCtx ] * sizeof ( u64 ) + 63 ) & ~63;

if ( aDWC > g_GSCtx.m_nAlloc[ aCtx ] - lPutIdx - 8 )

g_GSCtx.m_nAlloc[ aCtx ] = aDWC + g_GSCtx.m_nAlloc[ aCtx ] + 512;

g_GSCtx.m_pDisplayList[ aCtx ] = lpList = ( u64* )realloc(
lpList, ( g_GSCtx.m_nAlloc[ aCtx ] * sizeof ( u64 ) + 63 ) & ~63
g_GSCtx.m_pDisplayList[ aCtx ] = lpList = ( u64* )SMS_ReallocWithAlign(
lpList, &lOldAllocSize, ( g_GSCtx.m_nAlloc[ aCtx ] * sizeof ( u64 ) + 63 ) & ~63
);

if ( aMethod & 0x02 ) {
Expand Down Expand Up @@ -254,12 +257,15 @@ void GSContext_CallList ( int aCtx, u64* apList ) {
unsigned int lQWC = ( apList[ -1 ] >> 32 ) & 0xFFFF;
unsigned int lPutIdx = g_GSCtx.m_PutIndex[ aCtx ];
u64* lpList = g_GSCtx.m_pDisplayList[ aCtx ];
unsigned int lOldAllocSize;

if ( 2 > g_GSCtx.m_nAlloc[ aCtx ] - g_GSCtx.m_PutIndex[ aCtx ] )

g_GSCtx.m_pDisplayList[ aCtx ] = lpList = ( u64* )realloc (
lpList, ( g_GSCtx.m_nAlloc[ aCtx ] = 2 + g_GSCtx.m_nAlloc[ aCtx ] + 512 ) * sizeof ( u64 )
if ( 2 > g_GSCtx.m_nAlloc[ aCtx ] - g_GSCtx.m_PutIndex[ aCtx ] ) {
lOldAllocSize = ( g_GSCtx.m_nAlloc[ aCtx ] ) * sizeof ( u64 );
g_GSCtx.m_nAlloc[ aCtx ] = 2 + g_GSCtx.m_nAlloc[ aCtx ] + 512;
g_GSCtx.m_pDisplayList[ aCtx ] = lpList = ( u64* )SMS_ReallocWithAlign (
lpList, &lOldAllocSize, ( g_GSCtx.m_nAlloc[ aCtx ] ) * sizeof ( u64 )
);
}

g_GSCtx.m_pLastTag[ aCtx ] = &lpList[ lPutIdx ];
lpList[ lPutIdx++ ] = DMA_TAG( lQWC + 1, 1, DMATAG_ID_REF, 0, ( unsigned int )( apList - 2 ), 0 );
Expand All @@ -274,12 +280,15 @@ void GSContext_CallList2 ( int aCtx, u64* apList ) {

unsigned int lPutIdx = g_GSCtx.m_PutIndex [ aCtx ];
u64* lpList = g_GSCtx.m_pDisplayList[ aCtx ];
unsigned int lOldAllocSize;

if ( 2 > g_GSCtx.m_nAlloc[ aCtx ] - g_GSCtx.m_PutIndex[ aCtx ] )

g_GSCtx.m_pDisplayList[ aCtx ] = lpList = ( u64* )realloc (
lpList, ( g_GSCtx.m_nAlloc[ aCtx ] = 2 + g_GSCtx.m_nAlloc[ aCtx ] + 512 ) * sizeof ( u64 )
if ( 2 > g_GSCtx.m_nAlloc[ aCtx ] - g_GSCtx.m_PutIndex[ aCtx ] ) {
lOldAllocSize = ( g_GSCtx.m_nAlloc[ aCtx ] ) * sizeof ( u64 );
g_GSCtx.m_nAlloc[ aCtx ] = 2 + g_GSCtx.m_nAlloc[ aCtx ] + 512;
g_GSCtx.m_pDisplayList[ aCtx ] = lpList = ( u64* )SMS_ReallocWithAlign (
lpList, &lOldAllocSize, ( g_GSCtx.m_nAlloc[ aCtx ] ) * sizeof ( u64 )
);
}

g_GSCtx.m_pLastTag[ aCtx ] = &lpList[ lPutIdx ];
lpList[ lPutIdx++ ] = DMA_TAG( 0, 0, DMATAG_ID_CALL, 0, ( unsigned int )apList, 0 );
Expand Down
4 changes: 3 additions & 1 deletion src/SMS_GUIDesktop.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ void GUI_Status ( char* apMsg ) {
int lWidth = g_GSCtx.m_Width - 96;
int lDX = -2;
int lQWC;
unsigned int lWidthOld;

while ( GSFont_WidthEx ( apMsg, lLen, lDX ) > lWidth && lDX >= -12 ) --lDX;
while ( GSFont_WidthEx ( apMsg, lLen, lDX ) > lWidth ) --lLen;
Expand All @@ -456,7 +457,8 @@ void GUI_Status ( char* apMsg ) {

if ( s_nDMASL < lWidth ) {

s_pDMASL = ( u64* )realloc( s_pDMASL, lWidth * sizeof ( u64 ) );
lWidthOld = s_nDMASL * sizeof ( u64 );
s_pDMASL = ( u64* )SMS_ReallocWithAlign( s_pDMASL, &lWidthOld, lWidth * sizeof ( u64 ) );
s_nDMASL = lWidth;

} /* end if */
Expand Down
2 changes: 1 addition & 1 deletion src/SMS_JPEG.c
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ int SMS_JPEGLoad ( SMS_JPEGContext* apCtx, void* apData, unsigned int aDataSize
apCtx -> m_nBitmapQWC = ( ( lnAlloc + 15 ) & ~15 ) >> 4;
apCtx -> m_nBitmapBytes = lnAlloc;

if ( apCtx -> m_nAlloc < lnAlloc ) apCtx -> m_pBitmap = ( unsigned char* )realloc( apCtx -> m_pBitmap, ( ( apCtx -> m_nAlloc = lnAlloc ) + 64 ) & ~63 );
apCtx -> m_pBitmap = ( unsigned char* )SMS_ReallocWithAlign( apCtx -> m_pBitmap, &( apCtx -> m_nAlloc ), ( ( lnAlloc ) + 64 ) & ~63 );

lpRC -> ProcessBuffer ( lpRC, UNCACHED_SEG( apCtx -> m_pBitmap ) );

Expand Down

0 comments on commit 98d010f

Please sign in to comment.