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 

Slvd|Memory Allocation overwriting previously allocated data

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



Joined: 28 May 2008
Posts: 842

PostPosted: Wed Jun 25, 2008 4:09 am    Post subject: Slvd|Memory Allocation overwriting previously allocated data Reply with quote

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:
#include <pspkernel.h>
#include <pspdebug.h>
#include <stdio.h>

#define printf pspDebugScreenPrintf

PSP_MODULE_INFO("Bytetest", 0, 1, 0);

#define NUMFILES 14
typedef struct
{
   int x,y,h,w;
   unsigned int size;
} imagehdr;

typedef struct
{
   imagehdr hdr;
   SceUID blockid;
   unsigned char* data;
} imagefile;

imagefile images[NUMFILES];

int exit_callback(int arg1, int arg2, void *common) {
          sceKernelExitGame();
          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;

          thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
          if(thid >= 0) {
                    sceKernelStartThread(thid, 0, 0);
          }

          return thid;
}

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

Code:
TARGET = bytetest
OBJS = main.o

INCDIR =
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)

#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=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/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
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Wed Jun 25, 2008 4:42 am    Post subject: Reply with quote

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 &.
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