Hi there :)
When I try to use malloc() in a kernel module (compiled against kernel libs - USE_KERNEL_LIBC and USE_KERNEL_LIBS) I get an "unknown/missing reference" for malloc() during compilation?!?!
When compiling against user libs (or adding -lc) the .PRX can't load anymore due to userimports in a kernel module - but it compiles fine....
How do I use malloc() in a pure kernel module?
malloc() in kernel .PRX
-
- Posts: 376
- Joined: Wed May 10, 2006 11:31 pm
You can have a look at this thread : http://forums.ps2dev.org/viewtopic.php? ... malloc+prx
--pspZorba--
NO to K1.5 !
NO to K1.5 !
Ah, guys, you are brilliant - again :)
I was afraid that I had to use those sysmen functions - they look scary ;)
But I searched some more (esp. about the mem partition IDs) and I think I found all the bits I need.
At least the plugin compiles now without any errors.
It's too late to give it a life testrun on the PSP right now, will do later after some sleep.
I'll let you know the outcome!
[EDIT]
YEZZZ! Working like a charm :)
At least the plugin is doing what it's supposed to w/o crashing the PSP, so I assume it works :D
Here's what I made of it:
I was afraid that I had to use those sysmen functions - they look scary ;)
But I searched some more (esp. about the mem partition IDs) and I think I found all the bits I need.
At least the plugin compiles now without any errors.
It's too late to give it a life testrun on the PSP right now, will do later after some sleep.
I'll let you know the outcome!
[EDIT]
YEZZZ! Working like a charm :)
At least the plugin is doing what it's supposed to w/o crashing the PSP, so I assume it works :D
Here's what I made of it:
Code: Select all
/*
A small "wrapper" like thing to allocate memory in a pure kernel module
as easy as one would do with malloc()
It creates an own function, fully compatible wih malloc(), but using
the SCE functions to do stuff....
*/
struct MallocEntry
{
int hcMallocID;
SceUID SceMemID;
void* addr;
} MallocList[32];
int MallocCount = 0;
void* hcMalloc(int size)
{
// yah.... since we don't have malloc() in a pure kernel module (WHY?), this is a wrapper
// to replace malloc() with a similar easy to use function that uses some SCE syscalls
// to grab a chunk of memory and some more voodoo to manage it
SceUID memID;
char memName[128];
void* memAddr;
MallocCount++;
sprintf( memName, "BigzChunkzOfRAMz%i", MallocCount );
memID = sceKernelAllocPartitionMemory( 1, memName, PSP_SMEM_Low, size, 0 );
memAddr = sceKernelGetBlockHeadAddr( memID );
MallocList[MallocCount].hcMallocID = MallocCount;
MallocList[MallocCount].SceMemID = memID;
MallocList[MallocCount].addr = memAddr;
return memAddr;
}
void hcFree(void* memAddr)
{
// well.... if one has his own malloc() version, he also needs a way to free() it ;)
int i;
for(i=1; i<=MallocCount; i++)
{
// look for the entry to free
if( MallocList[i].addr == memAddr )
{
// check valadity
if( MallocList[i].hcMallocID == i )
{
// set willy free
sceKernelFreePartitionMemory( MallocList[i].SceMemID );
}
}
}
}