hi,
jimparis wrote:
t would only affect open(), fstat(), and close(). If we just have a fixed array of pointers, the overhead actually wouldn't be too bad (strdup for open, free for close, table lookup for fstat). Locking shouldn't be necessary. If we support 0<fd<1024, the mem overhead is only 4kb+filenames.
this ugly patch does that, take a look.
Code: Select all
diff -U 3 -H -N newlib-psp/newlib/libc/sys/psp/libcglue.c newlib-1.13.0/newlib/libc/sys/psp/libcglue.c
--- newlib-psp/newlib/libc/sys/psp/libcglue.c 2005-10-05 20:07:36.000000000 +0300
+++ newlib-1.13.0/newlib/libc/sys/psp/libcglue.c 2005-10-05 20:37:07.000000000 +0300
@@ -40,6 +40,9 @@
extern void __psp_init_cwd(char *argv_0);
extern int __psp_path_absolute(const char *in, char *out, int len);
+#define __PSP_FILENO_MAX 1024
+extern char * __psp_filename_map[__PSP_FILENO_MAX];
+
#ifdef F_getcwd
char *getcwd(char *buf, size_t size)
{
@@ -135,6 +138,7 @@
#ifdef F__open
int _open(const char *name, int flags, int mode)
{
+ int fd;
int sce_flags;
char dest[MAXPATHLEN + 1];
@@ -163,8 +167,12 @@
if (flags & O_NONBLOCK) {
sce_flags |= PSP_O_NBLOCK;
}
-
- return sceIoOpen(dest, sce_flags, mode);
+
+ fd = sceIoOpen(dest, sce_flags, mode);
+ if ((fd >= 0) && (fd < __PSP_FILENO_MAX)) {
+ __psp_filename_map[fd] = strdup(dest);
+ }
+ return fd;
}
#endif
@@ -184,6 +192,13 @@
if (sce_fd < 0) {
return -EBADF;
}
+
+ if ((sce_fd >= 0) && (sce_fd < __PSP_FILENO_MAX)) {
+ if (__psp_filename_map[sce_fd] != NULL) {
+ free(__psp_filename_map[sce_fd]);
+ __psp_filename_map[sce_fd] = NULL;
+ }
+ }
return sceIoClose(sce_fd);
}
@@ -453,9 +468,12 @@
#ifdef F__fstat
int _fstat(int fd, struct stat *sbuf)
{
- /* This is all Sony's newlib does, but should be improved */
- sbuf->st_mode = S_IFCHR;
- return 0;
+ if ((fd >= 0) && (fd < __PSP_FILENO_MAX)) {
+ if (__psp_filename_map[fd] != NULL) {
+ return stat(__psp_filename_map[fd], sbuf);
+ }
+ }
+ return -1;
}
#endif
@@ -619,6 +637,7 @@
/* Initialize cwd from this program's path */
__psp_init_cwd(argv[0]);
+ memset(__psp_filename_map, 0, sizeof(char *) * __PSP_FILENO_MAX);
/* Initialize timezone from PSP configuration */
int tzOffset = 0;
diff -U 3 -H -N newlib-psp/newlib/libc/sys/psp/pspcwd.c newlib-1.13.0/newlib/libc/sys/psp/pspcwd.c
--- newlib-psp/newlib/libc/sys/psp/pspcwd.c 2005-10-05 20:07:36.000000000 +0300
+++ newlib-1.13.0/newlib/libc/sys/psp/pspcwd.c 2005-10-05 20:24:37.000000000 +0300
@@ -19,6 +19,7 @@
#include <pspiofilemgr.h>
char __psp_cwd[MAXPATHLEN + 1] = { 0 };
+char * __psp_filename_map[1024];
/* Set the current working directory (CWD) to the path where the module was launched. */
void __psp_init_cwd(char *argv_0)
@@ -163,4 +164,3 @@
if(dr < 0) dr = 0;
return __psp_path_normalize(out + dr, len - dr);
}
checked with (barrowed from cwd sample);
Code: Select all
#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <psptypes.h>
#include <pspiofilemgr.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/unistd.h>
#include <fcntl.h>
PSP_MODULE_INFO("CwdTest", 0, 1, 1);
#define printf pspDebugScreenPrintf
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
sceKernelExitGame();
return 0;
}
/* Callback thread */
int CallbackThread(SceSize args, void *argp)
{
int cbid;
cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}
/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void)
{
int thid = 0;
thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
if(thid >= 0)
{
sceKernelStartThread(thid, 0, 0);
}
return thid;
}
int main(void)
{
int fd;
struct stat st;
pspDebugScreenInit();
SetupCallbacks();
stat("ms0:/share/xynth/configs/xynth.conf", &st);
printf("stat: %lu, %lu, %lu, %u, %lu\n", st.st_ctime,
st.st_atime,
st.st_mtime,
st.st_mode,
st.st_size);
fd = open("ms0:/share/xynth/configs/xynth.conf", O_RDWR);
printf("fd = %d\n", fd);
st.st_ctime =
st.st_atime =
st.st_mtime =
st.st_mode =
st.st_size = 0;
printf("fstat = %d\n", fstat(fd, &st));
printf("stat: %lu, %lu, %lu, %u, %lu\n", st.st_ctime,
st.st_atime,
st.st_mtime,
st.st_mode,
st.st_size);
close(fd);
printf("fstat = %d\n", fstat(fd, &st));
while (1) sleep (1);
return 0;
}