Trouble with malloc()

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

Moderators: cheriff, TyRaNiD

Post Reply
User avatar
nullp01nter
Posts: 26
Joined: Wed Jan 04, 2006 7:40 am
Location: Saxony/Germany

Trouble with malloc()

Post by nullp01nter »

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: Select all

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

PSP_MODULE_INFO&#40;"hexxpipes", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER&#41;;
int g_ExitApplication = 0;	// global exit flag, app runs until this is != 0

int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
	// set our global exit flag
	g_ExitApplication = 1;
	return 0;
&#125;

int CallbackThread&#40;SceSize args, void *argp&#41;
&#123;
	int cbid;
	
	cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
	sceKernelRegisterExitCallback&#40;cbid&#41;;
	
	sceKernelSleepThreadCB&#40;&#41;;
	
	return 0;
&#125;

int SetupCallbacks&#40;void&#41;
&#123;
	int thid = 0;
	
	// create thread
	thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11,
		0xFA0, 0, 0&#41;;

	// start thread
	if&#40;thid >= 0&#41;
	&#123;
		sceKernelStartThread&#40;thid, 0, 0&#41;;
	&#125;
	
	return thid;
&#125;

int InitApplication&#40;int argc, char** argv&#41;
&#123;
	SceCtrlData pad, lastpad;
	int i = 0;
	char *c;
	
	printf&#40;"\nStarting...\n"&#41;;
	SetupCallbacks&#40;&#41;;

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

	sceKernelExitGame&#40;&#41;;
	return 0; 
&#125;

int main&#40;int argc, char** argv&#41;
&#123;
	InitApplication&#40;argc, argv&#41;;		// defined in PSP/pspInit.h and win32/win32Init.h
	return 0;
&#125;
Makefile:

Code: Select all

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

CFLAGS = -O2 -G0 -Wall
LIBS = 

CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti -fno-strict-aliasing 
ASFLAGS = $&#40;CFLAGS&#41;

PSPSDK= $&#40;shell psp-config --pspsdk-path&#41;
PSPDIR= $&#40;shell psp-config --psp-prefix&#41;

include $&#40;PSPSDK&#41;/lib/build.mak
exports.exp:

Code: Select all

PSP_BEGIN_EXPORTS
PSP_EXPORT_START&#40;syslib, 0, 0x8000&#41;
PSP_EXPORT_FUNC&#40;module_start&#41;
PSP_EXPORT_VAR&#40;module_info&#41;
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
User avatar
|Sasuke|
Posts: 3
Joined: Sun Dec 11, 2005 1:46 pm

Post by |Sasuke| »

#include <malloc.h>
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

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.
User avatar
nullp01nter
Posts: 26
Joined: Wed Jan 04, 2006 7:40 am
Location: Saxony/Germany

Post by nullp01nter »

@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
Post Reply