Allocating Memory Problem in Kernel Mode

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
ADePSP
Posts: 58
Joined: Thu Jul 13, 2006 9:07 pm

Allocating Memory Problem in Kernel Mode

Post by ADePSP »

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: Select all

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
weltall
Posts: 310
Joined: Fri Feb 20, 2004 1:56 am
Contact:

Post by weltall »

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 :)
ADePSP
Posts: 58
Joined: Thu Jul 13, 2006 9:07 pm

Post by ADePSP »

Thanks man... :D
ADePSP
Posts: 58
Joined: Thu Jul 13, 2006 9:07 pm

Post by ADePSP »

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
weltall
Posts: 310
Joined: Fri Feb 20, 2004 1:56 am
Contact:

Post by weltall »

what is memid?
maybe you are out of memory and it doesn't alloc it (so you are reading in a NULL pointer)
ADePSP
Posts: 58
Joined: Thu Jul 13, 2006 9:07 pm

Post by ADePSP »

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
weltall
Posts: 310
Joined: Fri Feb 20, 2004 1:56 am
Contact:

Post by weltall »

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
Post Reply