Strange problem with malloc & sceKernelLoadModule

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

Moderators: cheriff, TyRaNiD

Post Reply
CyberBill
Posts: 86
Joined: Tue Jul 26, 2005 3:53 pm
Location: Redmond, WA

Strange problem with malloc & sceKernelLoadModule

Post by CyberBill »

I'm having some trouble with the prx loader sample. It seems that any time I call malloc in the same thread OR if I create a user thread and call malloc from there, when it goes to load the prx file it fails.

I have narrowed it down to calling malloc, and it doesnt matter how much memory you allocate. 100 bytes or 7MB, its all the same.

The call to sceKernelLoadModule() fails with:

block.c : _allocSysMemory : Low : no more space, can not alloc
block.c : _allocSysMemory : Low : request nblocks 14690 (max 2394)
block.c : sceKernelAllocPartitionMemory failed
SceModmgrLoadModuleFileBufferPRX, size 0x39618d
modulemgr.c exec_thread : LoadModule failed : 0x800200d9
modulemgr.c : sceKernelLoadModuleWithApitype : sceKernelLoadModule failed 0x800200d9

Its very strange because this was working just fine about a month ago, and then after I got a new version of the toolchain & PSPSDK it stopped working.

The prx I'm trying to load is approximately 3.5MB, if that matters.

Any help would be very much appreciated!! Thanks!

Ohh, and the point of doing this is so I can load up modules before loading up a WiFi project I'm working on, meaning I cant use sceKernelLoadExec because it resets all of the modules... or something, I really dont know how it works. :)

-Bill
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

By default, PSPSDK will reserve all of available memory for the application's heap. This leaves only about 1MB or less (IIRC) to load modules into. You can set a specific amount to reserve by doing:

Code: Select all

#include <pspmoduleinfo.h>

PSP_HEAP_SIZE_KB&#40;1024&#41;;
which will reserve 1MB for your heap the first time malloc() is called.
CyberBill
Posts: 86
Joined: Tue Jul 26, 2005 3:53 pm
Location: Redmond, WA

Post by CyberBill »

mrbrown is a GENIUS!

Thank you so much! :)
CyberBill
Posts: 86
Joined: Tue Jul 26, 2005 3:53 pm
Location: Redmond, WA

Post by CyberBill »

There wouldnt happen to be any way to disable this behavior, would there? Id really like my application that I'm launching to have all available RAM, instead of my launcher taking up a bunch that its not even using.

I need to be able to make a buffer, transfer the file into it, output it, and launch it.

Also, where is this code actually used? I mean, I do a search for PSP_HEAP_SIZE_KB and it comes up thats just a #define for unsigned int sce_newlib_heap_kb_size. So, I do a search for that... and nothing comes up anywhere in the SDK. This is very confusing...

I'm assuming this is part of the toolchain rather than the SDK... but who knows.

Thanks!!
User avatar
Agoln
Posts: 326
Joined: Wed Jun 08, 2005 3:14 am
Location: Fort Wayne, IN

Post by Agoln »

I just did a quick grep and here are my results:

Code: Select all

$ grep -R "sce_newlib_heap" *
Binary file psp/lib/libc.a matches
Binary file psp/lib/libg.a matches
psp/sdk/include/pspmoduleinfo.h&#58;        unsigned int sce_newlib_heap_kb_size = &#40;size_kb&#41;
Binary file psp/sdk/lib/libpsplibc.a matches
and in svn (not the latest, but only about 2 weeks old)

Code: Select all

$ grep -R "sce_newlib_heap_kb_size" *
pspsdk/sdk/libc/.svn/text-base/libcglue.c.svn-base&#58;extern unsigned int sce_newlib_heap_kb_size __attribute__&#40;&#40;weak&#41;&#41;;
pspsdk/sdk/libc/.svn/text-base/libcglue.c.svn-base&#58;             if &#40;&sce_newlib_heap_kb_size != NULL&#41; &#123;
pspsdk/sdk/libc/.svn/text-base/libcglue.c.svn-base&#58;                     heap_size = sce_newlib_heap_kb_size * 1024;
pspsdk/sdk/libc/libcglue.c&#58;extern unsigned int sce_newlib_heap_kb_size __attribute__&#40;&#40;weak&#41;&#41;;
pspsdk/sdk/libc/libcglue.c&#58;             if &#40;&sce_newlib_heap_kb_size != NULL&#41; &#123;
pspsdk/sdk/libc/libcglue.c&#58;                     heap_size = sce_newlib_heap_kb_size * 1024;
pspsdk/sdk/user/.svn/text-base/pspmoduleinfo.h.svn-base&#58;        unsigned int sce_newlib_heap_kb_size = &#40;size_kb&#41;
pspsdk/sdk/user/pspmoduleinfo.h&#58;        unsigned int sce_newlib_heap_kb_size = &#40;size_kb&#41;
psptoolchain/.svn/text-base/newlib-1.13.0.patch.svn-base&#58;+extern unsigned int sce_newlib_heap_kb_size __attribute__&#40;&#40;weak&#41;&#41;;
psptoolchain/.svn/text-base/newlib-1.13.0.patch.svn-base&#58;+              if &#40;&sce_newlib_heap_kb_size != NULL&#41; &#123;
psptoolchain/.svn/text-base/newlib-1.13.0.patch.svn-base&#58;+                      heap_size = sce_newlib_heap_kb_size * 1024;
psptoolchain/newlib-1.13.0.patch&#58;+extern unsigned int sce_newlib_heap_kb_size __attribute__&#40;&#40;weak&#41;&#41;;
psptoolchain/newlib-1.13.0.patch&#58;+              if &#40;&sce_newlib_heap_kb_size != NULL&#41; &#123;
psptoolchain/newlib-1.13.0.patch&#58;+                      heap_size = sce_newlib_heap_kb_size * 1024;

That may help out on where to look next.

Cheers!
Lego of my Ago!
urchin
Posts: 121
Joined: Thu Jun 02, 2005 5:41 pm

Post by urchin »

I'm am having a similar problem in my application too. I am trying to implement a reload mechanism that reloads the elf. When I call sceKernelLoadModuleMs, I get the 800020d9 error, with a message that it is failing to alloc mem of size 0x181250. I've tried PSP_HEAP_SIZE_KB(1024), but I get exactly the same problem. Any other ideas?
CyberBill
Posts: 86
Joined: Tue Jul 26, 2005 3:53 pm
Location: Redmond, WA

Post by CyberBill »

Use sceKernelCreateFpl if its been added to the SDK yet... if not, there is a sceKernelCreateMemoryPool I think. Use the first if you can. It allows you to allocate -real- memory, but you shouldnt call it often.
urchin
Posts: 121
Joined: Thu Jun 02, 2005 5:41 pm

Post by urchin »

Unfortunately, a quick grep reveals neither of those in the sdk (from the day before yesterday)

Oh well, not a massive issue for now, just a nice to have.
User avatar
Shazz
Posts: 244
Joined: Tue Aug 31, 2004 11:42 pm
Location: Somewhere over the rainbow
Contact:

Post by Shazz »

Yes they are ;-)

Just add the Fpl prototypes to your code and it works...

I wrote a malloc wrapper using Variable PooL (VPL) memory management functions and it works perfectly within the PRX. It allows you to easily manage closely the memory allocated in your PRX and free it while unloading.

Before I did many tries using the Heap and sce Partitons memory allcoations but I found lotsa troubles :
- difficlty to manage the heap withinh the PRX (look at previous messages)
- sceKernelAllocPartitionMemory cannot aloocate infinite numbers of partitons (seems restrict to 255 of a fixex number) and each partition seems to be bigger than the desired size (probably a minimum size) so it is not usable for multiple small mallocs.

... so use the VPL ! (or the FPL is you want to each time allocate blocks of the same size).

hope that helps...
- TiTAN Art Division -
http://www.titandemo.org
Post Reply