Slvd|Memory Allocation overwriting previously allocated data

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

Moderators: cheriff, TyRaNiD

Post Reply
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Slvd|Memory Allocation overwriting previously allocated data

Post by Torch »

What happens is that when I read data into image[1].data, it overwrites part of image[0].data. Similarly all the higher elements in the array overwrite the previous elements when I read data into them.

I don't even know if I'm allocating the memory correctly.
Run the program and look at the output files, you'll understand what I mean.
If the entire loop runs, only the last file is extracted correctly.

I tried printing the addresses that each image[].data points to, and the difference between subsequent ones (image[0].data and image[1].data) is on average around 4000 bytes.

When allocating image[0].data it should have allocated 44817 bytes, not 4000!!! Because image[0].hdr.size is 44817, and this value is passed to sceKernelAllocPartitionMemory in order to allocate image[0].data.

The structure of the lockdown.thm file is as follows.
4 byte int
4 byte int
4 byte int
4 byte int
4 byte unsigned int (this is hdr.size, the length of the following data until the next header)
....data....
....data....

Its repeated NUMFILES times.

Over here I'm writing the extracted files to the memory stick just to test if the extraction is working. I'm going to render the images directly from the images[].data

I'm clueless about dynamic memory allocation. Someone please help me T_T

Code: Select all

#include <pspkernel.h>
#include <pspdebug.h>
#include <stdio.h>

#define printf pspDebugScreenPrintf

PSP_MODULE_INFO&#40;"Bytetest", 0, 1, 0&#41;;

#define NUMFILES 14
typedef struct
&#123;
	int x,y,h,w;
	unsigned int size;
&#125; imagehdr;

typedef struct
&#123;
	imagehdr hdr;
	SceUID blockid;
	unsigned char* data;
&#125; imagefile;

imagefile images&#91;NUMFILES&#93;;

int exit_callback&#40;int arg1, int arg2, void *common&#41; &#123;
          sceKernelExitGame&#40;&#41;;
          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;

          thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
          if&#40;thid >= 0&#41; &#123;
                    sceKernelStartThread&#40;thid, 0, 0&#41;;
          &#125;

          return thid;
&#125;

int main&#40;int argc, char **argv&#41;
&#123;
	pspDebugScreenInit&#40;&#41;;
	SetupCallbacks&#40;&#41;;
	
	printf&#40;"Opening flash0&#58;/lockdown.thm.\n"&#41;;
	
	int i, bytesRead;
	SceUID fp, fo;
	
	fp = sceIoOpen&#40;"flash0&#58;/lockdown.thm", PSP_O_RDONLY, 0777&#41;;
	
	if &#40;fp < 0&#41;
	&#123;
		printf&#40;"Error loading flash0&#58;/lockdown.thm.\n"&#41;;
		return 0;
	&#125;
	else
	&#123;
		for &#40;i = 0; i < NUMFILES; i++&#41;
		&#123;
			bytesRead = sceIoRead&#40;fp, &images&#91;i&#93;.hdr, sizeof&#40;imagehdr&#41;&#41;;
			if &#40;bytesRead != sizeof&#40;imagehdr&#41;&#41;
			&#123;
				printf&#40;"Unexpected end of header %d. Read %d bytes. Header size %d bytes.\n", i, bytesRead, sizeof&#40;imagehdr&#41;&#41;;
				return 0;
			&#125;
			
			images&#91;i&#93;.blockid = sceKernelAllocPartitionMemory&#40;2, "block", 0, &#40;sizeof&#40;unsigned char&#41; * images&#91;i&#93;.hdr.size&#41;, NULL&#41;;
			if &#40;images&#91;i&#93;.blockid < 0&#41;
			&#123;
				printf&#40;"Memory allocation error 0x%x.\n", images&#91;i&#93;.blockid&#41;;
				return 0;
			&#125;
			images&#91;i&#93;.data = &#40;unsigned char*&#41; sceKernelGetBlockHeadAddr&#40;images&#91;i&#93;.blockid&#41;;
			
			bytesRead = sceIoRead&#40;fp, &images&#91;i&#93;.data, images&#91;i&#93;.hdr.size&#41;;
			if &#40;bytesRead != images&#91;i&#93;.hdr.size&#41;
			&#123;
				printf&#40;"Unexpected end of data in image %d. Read %d bytes. Data size %u bytes.\n", i, bytesRead, images&#91;i&#93;.hdr.size&#41;;
				return 0;
			&#125;
		&#125;
	&#125;
	sceIoClose&#40;fp&#41;;
	
	for &#40;i = 0; i < NUMFILES; i++&#41;
	&#123;
		char temp&#91;30&#93;;
		sprintf&#40;temp, "ms0&#58;/%d.png", i&#41;;
		printf&#40;"Writing %s\n", temp&#41;;
		fo = sceIoOpen&#40;temp, PSP_O_WRONLY|PSP_O_TRUNC|PSP_O_CREAT, 0777&#41;;
		sceIoWrite&#40;fo, &images&#91;i&#93;.data, images&#91;i&#93;.hdr.size&#41;;
		sceIoClose&#40;fo&#41;;
		sceKernelFreePartitionMemory&#40;images&#91;i&#93;.blockid&#41;;
	&#125;
		
	sceKernelSleepThread&#40;&#41;;
	
	return 0;
&#125;

Code: Select all

TARGET = bytetest
OBJS = main.o

INCDIR =
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

#BUILD_PRX = 1
#PRX_EXPORTS = exports.exp

#USE_KERNEL_LIBC=1
#USE_KERNEL_LIBS=1

PSP_FW_VERSION = 390

LIBDIR =
LIBS =
LDFLAGS = -mno-crt0

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Byte Test
#PSP_EBOOT_ICON="icon0.png"
#PSP_EBOOT_PIC1="pic1.png"
#PSP_EBOOT_SND0="snd0.at3"

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak
Here is the file used in the code http://ifile.it/b7vmh92/lockdown.thm (Click on Request Ticked, it will change to Download File).
Last edited by Torch on Wed Jun 25, 2008 4:43 am, edited 1 time in total.
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

Never mind I solved it. Stupid mistake.
images[].data is already a pointer so I just needed to pass it directly to sceIoWrite/Read instead of adding &.
Post Reply