platform: PSP volatile(P5) memory allocator

This commit is contained in:
Crow-bar
2022-09-21 08:55:44 +03:00
parent 9c61941e52
commit b567531cdb
8 changed files with 395 additions and 125 deletions

View File

@@ -393,6 +393,10 @@ static ref_api_t gEngfuncs =
pfnDrawNormalTriangles,
pfnDrawTransparentTriangles,
&clgame.drawFuncs
#if XASH_PSP
,P5Ram_Alloc,
P5Ram_Free
#endif
};
static void R_UnloadProgs( void )

View File

@@ -42,6 +42,8 @@ void Platform_MessageBox( const char *title, const char *message, qboolean paren
void Platform_ReadCmd( const char *fname, int *argc, char **argv );
SceUID Platform_LoadModule( const char *filename, int mpid, SceSize argsize, void *argp );
int Platform_UnloadModule( SceUID modid, int *sce_code );
#include "psp/p5ram_psp.h"
#endif
#if XASH_ANDROID
const char *Android_GetAndroidID( void );

View File

@@ -0,0 +1,163 @@
/*
p5ram_psp.c - PSP P5 memory allocator
Copyright (C) 2022 Sergey Galushko
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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.
*/
#include <pspkernel.h>
#include <psppower.h>
#include <pspsuspend.h>
#include <stdio.h>
#include <stdlib.h>
#include "p5ram_psp.h"
#define P5RAM_ALIGN( x, a ) ((( x ) + (( typeof( x ))( a ) - 1 )) & ~( typeof( x )( a ) - 1 ))
#define P5RAM_CHECK( addr ) ((( unsigned int )addr >= ( unsigned int )p5ram_addr) && (( unsigned int )addr < ( unsigned int )p5ram_addr + p5ram_size))
#define P5RAM_FLAG_INIT 0x01
#define P5RAM_FLAG_SUSPENDED 0x02
typedef struct p5ram_info_s
{
SceUID uid;
size_t size;
struct p5ram_info_s *next;
}p5ram_info_t;
static unsigned char p5ram_flags = 0x00;
static p5ram_info_t *p5ram_poolchain = NULL;
static void *p5ram_addr;
static unsigned int p5ram_size;
int P5Ram_Init( void )
{
int result;
if( p5ram_flags & P5RAM_FLAG_INIT )
return -1;
result = sceKernelVolatileMemLock( 0, &p5ram_addr, &p5ram_size );
if( result == 0 )
p5ram_flags |= P5RAM_FLAG_INIT;
return result;
}
void *P5Ram_Alloc( size_t size )
{
SceUID uid;
void *ptr;
if(!( p5ram_flags & P5RAM_FLAG_INIT ))
return NULL;
uid = sceKernelAllocPartitionMemory( 5, "USER P5", PSP_SMEM_Low, size + /*sizeof( p5ram_info_t )*/64, NULL );
if( uid < 0 ) return NULL;
ptr = sceKernelGetBlockHeadAddr( uid );
(( p5ram_info_t* )ptr )->uid = uid;
(( p5ram_info_t* )ptr )->size = size;
(( p5ram_info_t* )ptr )->next = p5ram_poolchain;
p5ram_poolchain = ptr;
return ptr + /*sizeof( p5ram_info_t )*/64;
}
void P5Ram_Free( void *ptr )
{
p5ram_info_t *info, *next_ptr;
if( !( p5ram_flags & P5RAM_FLAG_INIT ) || ptr == NULL ) return;
info = ( p5ram_info_t* )( ptr - /*sizeof( p5ram_info_t )*/64);
if( info == p5ram_poolchain )
{
p5ram_poolchain = p5ram_poolchain->next;
}
else
{
next_ptr = p5ram_poolchain;
while( next_ptr )
{
if( next_ptr->next == info )
{
next_ptr->next = info->next;
break;
}
next_ptr = next_ptr->next;
}
}
sceKernelFreePartitionMemory( info->uid );
}
void P5Ram_FreeAll( void )
{
p5ram_info_t *next_ptr;
while( p5ram_poolchain )
{
next_ptr = p5ram_poolchain->next;
sceKernelFreePartitionMemory( p5ram_poolchain->uid );
p5ram_poolchain = next_ptr;
}
}
void P5Ram_Shutdown( void )
{
if( !( p5ram_flags & P5RAM_FLAG_INIT ))
return;
P5Ram_FreeAll();
sceKernelVolatileMemUnlock( 0 );
p5ram_flags &= ~P5RAM_FLAG_INIT;
}
void P5Ram_PowerCallback( int count, int arg, void *common )
{
if( !( p5ram_flags & ( P5RAM_FLAG_INIT | P5RAM_FLAG_SUSPENDED )))
return;
if ( arg & PSP_POWER_CB_POWER_SWITCH || arg & PSP_POWER_CB_SUSPENDING )
{
P5Ram_Shutdown();
p5ram_flags |= P5RAM_FLAG_SUSPENDED;
}
else if ( arg & PSP_POWER_CB_RESUMING )
{
}
else if ( arg & PSP_POWER_CB_RESUME_COMPLETE )
{
P5Ram_Init();
p5ram_flags &= ~P5RAM_FLAG_SUSPENDED;
}
}
#ifdef P5RAM_DEBUG
void P5Ram_Print( void )
{
p5ram_info_t *next_ptr;
printf("+++++++++++++++++++++++++++++\n");
next_ptr = p5ram_poolchain;
while( next_ptr )
{
printf("P5 ALLOC [ 0x%08X 0x%08X 0x%08X 0x%08X ]\n", next_ptr, next_ptr->uid, next_ptr->size, next_ptr->next );
next_ptr = next_ptr->next;
}
printf("+++++++++++++++++++++++++++++\n");
}
#endif

View File

@@ -0,0 +1,38 @@
/*
p5ram_psp.h - PSP P5 memory allocator header
Copyright (C) 2022 Sergey Galushko
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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.
*/
#ifndef P5RAM_PSP_H
#define P5RAM_PSP_H
//#define P5RAM_DEBUG
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
int P5Ram_Init( void );
void *P5Ram_Alloc( size_t size );
void P5Ram_Free( void *ptr );
void P5Ram_FreeAll( void );
void P5Ram_Shutdown( void );
void P5Ram_PowerCallback( int count, int arg, void *common );
#ifdef P5RAM_DEBUG
void P5Ram_Print( void );
#endif
#ifdef __cplusplus
}
#endif
#endif // P5RAM_PSP_H

View File

@@ -30,31 +30,39 @@ PSP_MAIN_THREAD_ATTR( PSP_THREAD_ATTR_USER | PSP_THREAD_ATTR_VFPU );
PSP_MAIN_THREAD_STACK_SIZE_KB( 512 );
PSP_HEAP_SIZE_KB( -3 * 1024 ); /* 3 MB for prx modules */
/* Exit callback */
static int exit_callback( int arg1, int arg2, void *common )
static int Platform_ExitCallback( int count, int arg, void *common )
{
host.crashed = true;
return 0;
}
/* Callback thread */
static int CallbackThread( SceSize args, void *argp )
static int Platform_PowerCallback( int count, int arg, void *common )
{
P5Ram_PowerCallback( count, arg, common );
return 0;
}
static int Platform_CallbackThread( SceSize args, void *argp )
{
int cbid;
cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);
cbid = sceKernelCreateCallback( "Exit Callback", Platform_ExitCallback, NULL );
sceKernelRegisterExitCallback( cbid );
cbid = sceKernelCreateCallback( "Power Callback", Platform_PowerCallback, NULL );
scePowerRegisterCallback( 0, cbid );
sceKernelSleepThreadCB();
return 0;
}
/* Sets up the callback thread and returns its thread id */
static int SetupCallbacks( void )
static int Platform_SetupCallbacks( void )
{
int thid = 0;
thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
thid = sceKernelCreateThread("update_thread", Platform_CallbackThread, 0x11, 0xFA0, 0, 0);
if(thid >= 0)
{
sceKernelStartThread(thid, 0, 0);
@@ -259,7 +267,7 @@ int Platform_UnloadModule( SceUID modid, int *sce_code )
*sce_code = sceKernelUnloadModule( modid );
return ( ( ( *sce_code ) < 0 ) ? -2 : 0 );
}
#include "vfs_psp.h"
void Platform_Init( void )
{
SceUID kamID;
@@ -269,7 +277,7 @@ void Platform_Init( void )
pspSdkDisableFPUExceptions();
// exit callback thread
SetupCallbacks();
Platform_SetupCallbacks();
// set max cpu/gpu frequency
scePowerSetClockFrequency( 333, 333, 166 );
@@ -283,10 +291,14 @@ void Platform_Init( void )
result = kaGeEdramSetSize( result );
Platform_UnloadModule( kamID, &result );
}
// P5 Ram init
P5Ram_Init();
}
void Platform_Shutdown( void )
{
P5Ram_Shutdown();
#if XASH_PROFILING
gprof_cleanup();
#endif

View File

@@ -431,6 +431,10 @@ typedef struct ref_api_s
void (*pfnDrawNormalTriangles)( void );
void (*pfnDrawTransparentTriangles)( void );
render_interface_t *drawFuncs;
#if XASH_PSP
void *(*P5Ram_Alloc)( size_t size );
void (*P5Ram_Free)( void *ptr );
#endif
} ref_api_t;
struct mip_s;

View File

@@ -291,7 +291,11 @@ static void InitEntityTable( SAVERESTOREDATA *pSaveData, int entityCount )
ENTITYTABLE *pTable;
int i;
#if XASH_PSP
pSaveData->pTable = P5Ram_Alloc( sizeof( ENTITYTABLE ) * entityCount );
#else
pSaveData->pTable = Mem_Calloc( host.mempool, sizeof( ENTITYTABLE ) * entityCount );
#endif
pSaveData->tableCount = entityCount;
// setup entitytable
@@ -607,8 +611,13 @@ static SAVERESTOREDATA *SaveInit( int size, int tokenCount )
{
SAVERESTOREDATA *pSaveData;
#if XASH_PSP
pSaveData = P5Ram_Alloc( sizeof( SAVERESTOREDATA ) + size );
pSaveData->pTokens = (char **)P5Ram_Alloc( tokenCount * sizeof( char* ));
#else
pSaveData = Mem_Calloc( host.mempool, sizeof( SAVERESTOREDATA ) + size );
pSaveData->pTokens = (char **)Mem_Calloc( host.mempool, tokenCount * sizeof( char* ));
#endif
pSaveData->tokenCount = tokenCount;
pSaveData->pBaseData = (char *)(pSaveData + 1); // skip the save structure);
@@ -657,20 +666,32 @@ static void SaveFinish( SAVERESTOREDATA *pSaveData )
if( pSaveData->pTokens )
{
#if XASH_PSP
P5Ram_Free( pSaveData->pTokens );
#else
Mem_Free( pSaveData->pTokens );
#endif
pSaveData->pTokens = NULL;
pSaveData->tokenCount = 0;
}
if( pSaveData->pTable )
{
#if XASH_PSP
P5Ram_Free( pSaveData->pTable );
#else
Mem_Free( pSaveData->pTable );
#endif
pSaveData->pTable = NULL;
pSaveData->tableCount = 0;
}
svgame.globals->pSaveData = NULL;
#if XASH_PSP
P5Ram_Free( pSaveData );
#else
Mem_Free( pSaveData );
#endif
}
/*
@@ -2246,7 +2267,11 @@ int GAME_EXPORT SV_GetSaveComment( const char *savename, char *comment )
// allocate a table for the strings, and parse the table
if( tokenSize > 0 )
{
#if XASH_PSP
pTokenList = P5Ram_Alloc( tokenCount * sizeof( char* ));
#else
pTokenList = Mem_Calloc( host.mempool, tokenCount * sizeof( char* ));
#endif
// make sure the token strings pointed to by the pToken hashtable.
for( i = 0; i < tokenCount; i++ )
@@ -2274,8 +2299,13 @@ int GAME_EXPORT SV_GetSaveComment( const char *savename, char *comment )
if( Q_stricmp( pFieldName, "GameHeader" ))
{
Q_strncpy( comment, "<missing GameHeader>", MAX_STRING );
#if XASH_PSP
if( pTokenList ) P5Ram_Free( pTokenList );
if( pSaveData ) P5Ram_Free( pSaveData );
#else
if( pTokenList ) Mem_Free( pTokenList );
if( pSaveData ) Mem_Free( pSaveData );
#endif
FS_Close( f );
return 0;
}
@@ -2325,8 +2355,13 @@ int GAME_EXPORT SV_GetSaveComment( const char *savename, char *comment )
}
// delete the string table we allocated
#if XASH_PSP
if( pTokenList ) P5Ram_Free( pTokenList );
if( pSaveData ) P5Ram_Free( pSaveData );
#else
if( pTokenList ) Mem_Free( pTokenList );
if( pSaveData ) Mem_Free( pSaveData );
#endif
FS_Close( f );
// at least mapname should be filled