malloc() in kernel .PRX

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

Moderators: cheriff, TyRaNiD

Post Reply
Hellcat
Posts: 83
Joined: Wed Jan 24, 2007 2:52 pm

malloc() in kernel .PRX

Post by Hellcat »

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

Post by weltall »

use partition allocation functions.
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

As weltall said, use sceKernelAllocPartitionMemory and sceKernelGetBlockHeadAddr for allocation.
pspZorba
Posts: 156
Joined: Sat Sep 22, 2007 11:45 am
Location: NY

Post by pspZorba »

You can have a look at this thread : http://forums.ps2dev.org/viewtopic.php? ... malloc+prx
--pspZorba--
NO to K1.5 !
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

There are much more alternatives... the kernel Heap functions, the threadman Fpl functions, etc
Hellcat
Posts: 83
Joined: Wed Jan 24, 2007 2:52 pm

Post by Hellcat »

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:

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&#40;i=1; i<=MallocCount; i++&#41;
  &#123;
    // look for the entry to free
    if&#40; MallocList&#91;i&#93;.addr == memAddr &#41;
    &#123;
      // check valadity
      if&#40; MallocList&#91;i&#93;.hcMallocID == i &#41;
      &#123;
        // set willy free
        sceKernelFreePartitionMemory&#40; MallocList&#91;i&#93;.SceMemID &#41;;
      &#125;
    &#125;
  &#125;
&#125;
Post Reply