malloc newbie question

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

Moderators: cheriff, TyRaNiD

Post Reply
gloupygloup
Posts: 9
Joined: Thu May 12, 2005 3:28 am

malloc newbie question

Post by gloupygloup »

Hi there,

im trying to build a little things on my own but im not familiar with the PS2SDK.I can compile the HelloWorld sample, make some change, have it running but when i want to make an allocation like :

int *data;
data = malloc(1024);

it compile but the compiler tell me undefined refence for malloc, i think that i miss to link a library but wich one contains the standard stdlib function ?


Thanks in advance !
ooPo
Site Admin
Posts: 2023
Joined: Sat Jan 17, 2004 9:56 am
Location: Canada
Contact:

Post by ooPo »

Try including malloc.h:

http://cvs.ps2dev.org/cgi-bin/viewcvs.c ... .h?rev=1.5

The function itself would be in the libc library, so make sure you have a -lc on your link line.
gloupygloup
Posts: 9
Joined: Thu May 12, 2005 3:28 am

Post by gloupygloup »

Thanks, of course i have added the malloc.h ;-) but not the -lc
im going to test that right away.

Thanks !
Orion_
Posts: 69
Joined: Thu Jan 27, 2005 8:47 am

Post by Orion_ »

how could the malloc of the standard libc would know where is the psp user ram and so on ??
I don't think this will work.
gloupygloup
Posts: 9
Joined: Thu May 12, 2005 3:28 am

Post by gloupygloup »

Orion_ : i have checked your plasma wich work fine dont you alloc anything in memory ?
Orion_
Posts: 69
Joined: Thu Jan 27, 2005 8:47 am

Post by Orion_ »

no :/
I compute the plasma picture in realtime inside the vram directly.
gloupygloup
Posts: 9
Joined: Thu May 12, 2005 3:28 am

Post by gloupygloup »

hmm ok , i m going to do something about that so.
Klesk
Posts: 4
Joined: Sat Apr 02, 2005 6:54 am

Post by Klesk »

i suppose the guy who made the gameboy emulator knows how to allocate memory
User avatar
sq377
Posts: 87
Joined: Mon Apr 11, 2005 3:30 am

Post by sq377 »

yea but he also doesn't know english very well.
ooPo
Site Admin
Posts: 2023
Joined: Sat Jan 17, 2004 9:56 am
Location: Canada
Contact:

Post by ooPo »

Oops, I thought this was a PS2-related question. Sorry. :)

Why not just statically declare the memory?

int data[1024];
subbie
Posts: 122
Joined: Thu May 05, 2005 4:14 am

Post by subbie »

ooPo wrote:Oops, I thought this was a PS2-related question. Sorry. :)

Why not just statically declare the memory?

int data[1024];
Thats all fine for a temp fix but a form of mallocing will be needed.

Anybody know if the systems ram can be directly address or does one need to call on the bios to allocate a useable heap from main memory?
ooPo
Site Admin
Posts: 2023
Joined: Sat Jan 17, 2004 9:56 am
Location: Canada
Contact:

Post by ooPo »

It may be a little early in the life of the pspdev world to demand things like malloc.
subbie
Posts: 122
Joined: Thu May 05, 2005 4:14 am

Post by subbie »

ooPo wrote:It may be a little early in the life of the pspdev world to demand things like malloc.
Agreed but i'm not going to just sit around and want want want. Questioning then researching will do wonders. :)
ooPo
Site Admin
Posts: 2023
Joined: Sat Jan 17, 2004 9:56 am
Location: Canada
Contact:

Post by ooPo »

You bring a tear to my eye with such lovely words. :)
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

ooPo wrote:It may be a little early in the life of the pspdev world to demand things like malloc.
With a little research I found this: http://forums.ps2dev.org/viewtopic.php?p=11133#11133. sceKernelAllocPartitionMemory() looks like malloc() to me.
gloupygloup
Posts: 9
Joined: Thu May 12, 2005 3:28 am

Post by gloupygloup »

well i need dynamic allocation since that i have make a couple of emulator and i would like to give it a try to port them into PSP.

Im going to find a way around this problem but it will ugly :D
gloupygloup
Posts: 9
Joined: Thu May 12, 2005 3:28 am

Post by gloupygloup »

Just for information i have tried the sceKernelAllocPartitionMemory like that :

Code: Select all

	data = (int*)sceKernelAllocPartitionMemory(sizeof(int)*20);
	
	int	i;

	data[0] = 0xFFFF;

	pgFillvram(0);
	while(1) {
		pgPrint4(1,1,0xFFFF,"TESTING");
		pgScreenFlipV();
	}
when i run my code to the PSP i have black screen but when i comment data[0] = 0xFFFF; i have "TESTING" displayed correctly


I have added this at the end of the startup.s :

Code: Select all

   STUB_START   "SysMemUserForUser",0x40000000,0x00030005 
   STUB_FUNC   0x237DBD4F,sceKernelAllocPartitionMemory 
   STUB_FUNC   0xB6D61D02,sceKernelFreePartitionMemory 
   STUB_FUNC   0x9D9A5BA1,sceKernelGetBlockHeadAddr 
   STUB_END 
gloupygloup
Posts: 9
Joined: Thu May 12, 2005 3:28 am

Post by gloupygloup »

Edit : post deleted ( wrong manipulation = double post )
Shito
Posts: 21
Joined: Wed May 11, 2005 1:38 am

Post by Shito »

gloupygloup wrote:when i run my code to the PSP i have black screen but when i comment data[0] = 0xFFFF; i have "TESTING" displayed correctly
Well then that means that sceKernelAllocPartitionMemory returned null.
Either the memory could not be allocated because there wasn't enough free memory (I highly doubt that ^^), either there was something wrong with the function.
Now *what* is another question. '__'


(and that's all for the most useless and trivial post ever :| )
gloupygloup
Posts: 9
Joined: Thu May 12, 2005 3:28 am

Post by gloupygloup »

I wonder if you dont have to give the pointer to your variable to sceKernelAllocPartitionMemory and that it return only OK or ERROR.

im going to have a try
tonymoon
Posts: 4
Joined: Tue May 10, 2005 10:40 pm

Post by tonymoon »

gloupygloup wrote:Just for information i have tried the sceKernelAllocPartitionMemory like that :

Code: Select all

	data = (int*)sceKernelAllocPartitionMemory(sizeof(int)*20);
	
	int	i;

	data[0] = 0xFFFF;

	pgFillvram(0);
	while(1) {
		pgPrint4(1,1,0xFFFF,"TESTING");
		pgScreenFlipV();
	}
when i run my code to the PSP i have black screen but when i comment data[0] = 0xFFFF; i have "TESTING" displayed correctly


I have added this at the end of the startup.s :

Code: Select all

   STUB_START   "SysMemUserForUser",0x40000000,0x00030005 
   STUB_FUNC   0x237DBD4F,sceKernelAllocPartitionMemory 
   STUB_FUNC   0xB6D61D02,sceKernelFreePartitionMemory 
   STUB_FUNC   0x9D9A5BA1,sceKernelGetBlockHeadAddr 
   STUB_END 
When i run your code my psp got frozen. I try anthor way like this code

Code: Select all

int sceKernelAllocPartitionMemory(void* buf,int size);
void  sceKernelFreePartitionMemory(void* buf);
and I failed too.
Guest

Post by Guest »

subbie wrote:
ooPo wrote: Why not just statically declare the memory?

int data[1024];
Thats all fine for a temp fix but a form of mallocing will be needed.

Anybody know if the systems ram can be directly address or does one need to call on the bios to allocate a useable heap from main memory?
Then roll your own dyamic memory allocator. Start with Oopo's suggestion to create your own heap, then write code to alloc, dealloc, realloc, and manage all of it. That will get you going until the time any real allocator is found.

You have no excuse to whine about this. Go forth and code, or wait for deliverance. :)
gloupygloup
Posts: 9
Joined: Thu May 12, 2005 3:28 am

Post by gloupygloup »

Hehe,

i have find a way around and im starting getting things to show.
Soon as i got something i will post it here.
Post Reply