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 

malloc() in kernel .PRX

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



Joined: 24 Jan 2007
Posts: 84

PostPosted: Mon Feb 25, 2008 3:07 pm    Post subject: malloc() in kernel .PRX Reply with quote

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



Joined: 20 Feb 2004
Posts: 310

PostPosted: Mon Feb 25, 2008 4:29 pm    Post subject: Reply with quote

use partition allocation functions.
Back to top
View user's profile Send private message Visit poster's website
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Mon Feb 25, 2008 7:34 pm    Post subject: Reply with quote

As weltall said, use sceKernelAllocPartitionMemory and sceKernelGetBlockHeadAddr for allocation.
Back to top
View user's profile Send private message
pspZorba



Joined: 22 Sep 2007
Posts: 156
Location: NY

PostPosted: Mon Feb 25, 2008 9:44 pm    Post subject: Reply with quote

You can have a look at this thread : http://forums.ps2dev.org/viewtopic.php?t=6716&highlight=malloc+prx
_________________
--pspZorba--
NO to K1.5 !
Back to top
View user's profile Send private message
moonlight



Joined: 26 Oct 2005
Posts: 567

PostPosted: Mon Feb 25, 2008 10:24 pm    Post subject: Reply with quote

There are much more alternatives... the kernel Heap functions, the threadman Fpl functions, etc
Back to top
View user's profile Send private message
Hellcat



Joined: 24 Jan 2007
Posts: 84

PostPosted: Mon Feb 25, 2008 10:47 pm    Post subject: Reply with quote

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:
/*
    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 );
      }
    }
  }
}
Back to top
View user's profile Send private message
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