forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Allocating Memory Problem in Kernel Mode

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
ADePSP



Joined: 13 Jul 2006
Posts: 58

PostPosted: Mon Sep 25, 2006 3:13 am    Post subject: Allocating Memory Problem in Kernel Mode Reply with quote

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
View user's profile Send private message
weltall



Joined: 20 Feb 2004
Posts: 310

PostPosted: Mon Sep 25, 2006 3:28 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
ADePSP



Joined: 13 Jul 2006
Posts: 58

PostPosted: Mon Sep 25, 2006 3:30 am    Post subject: Reply with quote

Thanks man... :D
Back to top
View user's profile Send private message
ADePSP



Joined: 13 Jul 2006
Posts: 58

PostPosted: Mon Sep 25, 2006 3:46 am    Post subject: Reply with quote

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
View user's profile Send private message
weltall



Joined: 20 Feb 2004
Posts: 310

PostPosted: Mon Sep 25, 2006 6:46 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
ADePSP



Joined: 13 Jul 2006
Posts: 58

PostPosted: Tue Sep 26, 2006 12:42 am    Post subject: Reply with quote

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
View user's profile Send private message
weltall



Joined: 20 Feb 2004
Posts: 310

PostPosted: Tue Sep 26, 2006 3:08 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group