| View previous topic :: View next topic |
| Author |
Message |
ADePSP
Joined: 13 Jul 2006 Posts: 58
|
Posted: Mon Sep 25, 2006 3:13 am Post subject: Allocating Memory Problem in Kernel Mode |
|
|
I'm updating my prx so it can read from a text file but am having problems because in Kernel Mode it complains that malloc and free are undefined when compiling...
If I change the makefile to use PSPSDK_LIBC and PSPSDK_LIBS it compiles fine but that's no use for what i'm doing...
Does anyone know what the problem might be or if their are API alternatives I should be using...?
Here is the makefile i'm using that has the problem,
| Code: | TARGET = capture
OBJS = main.o imagecapture.o
BUILD_PRX=1
#USE_PSPSDK_LIBC=1
#USE_PSPSDK_LIBS=1
USE_KERNEL_LIBC=1
USE_KERNEL_LIBS=1
PRX_EXPORTS=exports.exp
INCDIR =
CFLAGS = -Os -G0 -Wall -fno-strict-aliasing -fno-builtin-printf
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
LIBDIR =
LDFLAGS = -mno-crt0 -nostartfiles
LIBS =
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak |
Thanks
ADe |
|
| Back to top |
|
 |
weltall
Joined: 20 Feb 2004 Posts: 310
|
Posted: Mon Sep 25, 2006 3:28 am Post subject: |
|
|
kernel doesn't use the psp sdk libc but the psp builtin kernel libc which doesn't use malloc and free.
the psp has only a bunch of own function to do malloch and freeing and they work a bit differently:
first of all you need to malloc part of the partition
int memid = sceKernelAllocPartitionMemory(1, "mybuffer", 0, howmuchtoalloch, NULL);
this will alloc the ram you need:
arg1: in the kernel partition (1 or 3(mirror)) if you want user use 2
arg2: buffer name not much interesting if you only need to alloch usefull to search for allocations in the uid lists
arg3: this can be 0 => alloc in the lowest adress 1 alloc in the highest adress 2 alloc in a user determined adress
arg4: num*byte of space to alloc
arg5: if arg3 = 2 adress to alloc to
but you aren't done you need the adress were your allocation starts :)
mypointer = sceKernelGetBlockHeadAddr(memid);
and you are done you can use it as a normal malloched adress.
then to free it:
sceKernelFreePartitionMemory(memid);
so you need to keep the memid data if you don't want to search for your allocation trought the uids lists
you are done :) |
|
| Back to top |
|
 |
ADePSP
Joined: 13 Jul 2006 Posts: 58
|
Posted: Mon Sep 25, 2006 3:30 am Post subject: |
|
|
| Thanks man... :D |
|
| Back to top |
|
 |
ADePSP
Joined: 13 Jul 2006 Posts: 58
|
Posted: Mon Sep 25, 2006 3:46 am Post subject: |
|
|
Well, that all sounded quite straight forward but it crashed when I try to read into the pointer... See the bellow snipit of code... It crashes the PSP on the line highlighted in bold...
Little help...
-----> CODE SNIPPIT
int size = 0;
int fd;
int flen = 0;
char* Text;
char* Line;
// Returns number of bytes in file (I know this works)
size = getFileSize(filename);
// Allocate memory to hold file contents
int memid = sceKernelAllocPartitionMemory(1, "inibuffer", 0, size, NULL);
Text = sceKernelGetBlockHeadAddr(memid);
// Open file and read contents into pointer
fd = sceIoOpen(filename, PSP_O_RDONLY, 0777);
flen = sceIoRead(fd, Text, size);
sceIoClose(fd);
---> END CODE SNIPPIT |
|
| Back to top |
|
 |
weltall
Joined: 20 Feb 2004 Posts: 310
|
Posted: Mon Sep 25, 2006 6:46 am Post subject: |
|
|
what is memid?
maybe you are out of memory and it doesn't alloc it (so you are reading in a NULL pointer) |
|
| Back to top |
|
 |
ADePSP
Joined: 13 Jul 2006 Posts: 58
|
Posted: Tue Sep 26, 2006 12:42 am Post subject: |
|
|
| weltall wrote: | what is memid?
maybe you are out of memory and it doesn't alloc it (so you are reading in a NULL pointer) |
It was my filesize function allocating too big a number... I've changed the code to allocate 5k but does the sceKernelAllocPartitionMemory function set all the allocated space to 0...? If not is there an equivalent of the memset function...?
Thanks again,
ADe |
|
| Back to top |
|
 |
weltall
Joined: 20 Feb 2004 Posts: 310
|
Posted: Tue Sep 26, 2006 3:08 pm Post subject: |
|
|
| well you must do it by yourself if you want a memset function. after all it's simple doing it you just need to make a for loop which copies the second argument over all memory |
|
| Back to top |
|
 |
|