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 

Trouble with malloc()

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



Joined: 04 Jan 2006
Posts: 26
Location: Saxony/Germany

PostPosted: Mon Jan 23, 2006 7:51 am    Post subject: Trouble with malloc() Reply with quote

Hi folks,

I'm in big trouble with malloc(). I had a PSP project up and running which was allocating several hundreds of kilobytes. I loaded jpgs, a truetupe font, music and so on.

But from yesterday on, almost all my malloc() calls fail. It is no longer possible to allocate 64k or more. I installed a brand new toolchain and PSPSDK, but no luck. I tried to hunt down the problem with reducing the code to a minimum, and here is the essence of what is failing:

malloc-test.c
Code:

#include <pspkernel.h>
#include <stdlib.h>
#include <stdio.h>
#include <pspctrl.h>

PSP_MODULE_INFO("hexxpipes", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);
int g_ExitApplication = 0;   // global exit flag, app runs until this is != 0

int exit_callback(int arg1, int arg2, void *common)
{
   // set our global exit flag
   g_ExitApplication = 1;
   return 0;
}

int CallbackThread(SceSize args, void *argp)
{
   int cbid;
   
   cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
   sceKernelRegisterExitCallback(cbid);
   
   sceKernelSleepThreadCB();
   
   return 0;
}

int SetupCallbacks(void)
{
   int thid = 0;
   
   // create thread
   thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11,
      0xFA0, 0, 0);

   // start thread
   if(thid >= 0)
   {
      sceKernelStartThread(thid, 0, 0);
   }
   
   return thid;
}

int InitApplication(int argc, char** argv)
{
   SceCtrlData pad, lastpad;
   int i = 0;
   char *c;
   
   printf("\nStarting...\n");
   SetupCallbacks();

   sceCtrlReadBufferPositive(&lastpad, 1);
   do
   {
      sceCtrlReadBufferPositive(&pad, 1);
      
      if(pad.Buttons != lastpad.Buttons)
       {
         if(pad.Buttons & PSP_CTRL_CROSS)
            {
            c = (char*)malloc(8192); i+=8;
            if(c) printf("malloc ok, %i k allocated\n", i);
            else printf("malloc failed\n");
         }
         lastpad = pad;
      }
   } while(!((pad.Buttons & PSP_CTRL_START) || g_ExitApplication));

   sceKernelExitGame();
   return 0;
}

int main(int argc, char** argv)
{
   InitApplication(argc, argv);      // defined in PSP/pspInit.h and win32/win32Init.h
   return 0;
}


Makefile:
Code:
TARGET = malloc-test
PRX_EXPORTS=exports.exp
BUILD_PRX=1
OBJS = malloc-test.o

CFLAGS = -O2 -G0 -Wall
LIBS =

CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti -fno-strict-aliasing
ASFLAGS = $(CFLAGS)

PSPSDK= $(shell psp-config --pspsdk-path)
PSPDIR= $(shell psp-config --psp-prefix)

include $(PSPSDK)/lib/build.mak


exports.exp:

Code:
PSP_BEGIN_EXPORTS
PSP_EXPORT_START(syslib, 0, 0x8000)
PSP_EXPORT_FUNC(module_start)
PSP_EXPORT_VAR(module_info)
PSP_EXPORT_END
PSP_END_EXPORTS


I debug the program with PSPLINK via wifi. I did reinstall PSPLINK with the newest sources. When I start the program, every keypress on 'X' allocates another chunk of 8k RAM. When I reach 64k, it fails. Why?

Any help would be greatly appreciated. I'm really lost.

Thoralt
Back to top
View user's profile Send private message
|Sasuke|



Joined: 11 Dec 2005
Posts: 3

PostPosted: Mon Jan 23, 2006 8:56 am    Post subject: Reply with quote

#include <malloc.h>
Back to top
View user's profile Send private message
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Mon Jan 23, 2006 9:30 am    Post subject: Reply with quote

nullp01nter, the trouble is the allocation structure changed. The original default was for sbrk to allocate the heap based on the total free memory size, I made a change to newlib so that it now only does that by default in ELFs and not in prxes (where you might want multiple running at the same time). The default heap size for a prx is 64kb.

To fix it you can set the size of your prx heap to something larger using the
PSP_HEAP_SIZE_KB(size) define. so setting PSP_HEAD_SIZE_KB(24*1024) should give you a 24Mb heap.
Back to top
View user's profile Send private message
nullp01nter



Joined: 04 Jan 2006
Posts: 26
Location: Saxony/Germany

PostPosted: Mon Jan 23, 2006 6:33 pm    Post subject: Reply with quote

@TyRaNiD: Thanks a lot. That PSP_HEAP_SIZE_KB did the trick (although allocating all 24 MB at once wasn't that clever for my first try :))

@|Sasuke|: If you read my posting closely, you might notice that in fact my source is compilable. If <malloc.h> was missing completely, then I wouldn't have been able to compile the thing (seems it is included indirectly). Additionally, I only had problems allocating 64k and more. Nevertheless thanks for the reply.

Thoralt
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