When I allocate memory for image, then load and free it everything works fine. If I repeat it for about 100 times with the same image - everything is still ok. But at the attempt no. 101 OSLib returns error - no such file.
There is not enough ram. I've tested it via PSP_HEAP_SIZE_KB(18*-1024); - it crashes after about 10 attempts.
I believe, that malloc cannot allocate enough space in fragmented memory. I think that this is a stupid question (modifing every pointer in a program, lol ), but is there any way to defragment memory automatically?
Here's a code sample of my image loader (I read image from an one, big, merged file (data.dat), where the information about merged files is in data.h)
Code: Select all
typedef struct {
char name[32];
int len;
int offset;
} OCB_FILE;
void * imagebuff; //global image buffer
OCB_FILE * ocb_exists_notify(const char *filename) //gets the information about storaged file (it's lenght, offset and name)
{
OCB_FILE *ocb =(OCB_FILE *) malloc(sizeof(OCB_FILE));
FILE * ocb_header = fopen (OCB_FHEADER, "r" );
char line [ 128 ];
char ocb_name[128];
int ocb_len=0;
int ocb_offset=0;
while ( fgets ( line, sizeof line, ocb_header ) != NULL )
{
sscanf (line,"- %s %d %d", ocb_name, &ocb_len, &ocb_offset);
if (strcmp(ocb_name, filename) == 0)
{
ocb->len=ocb_len;
strcpy(ocb->name, ocb_name);
ocb->offset=ocb_offset;
fclose(ocb_header);
return ocb;
}
}
char * error=(char *)malloc(255);
sprintf(error, "no file: %s", filename);
oslFatalError(error);
return 0;
}
char * ocb_read(OCB_FILE * ocb) { //reads file
char * buffer=(char *) malloc(ocb->len);
FILE *input = fopen (OCB_FDATA, "rb" );
if (input==NULL) oslFatalError("error opening data file!");
fseek (input , ocb->offset, SEEK_SET );
fread (buffer,1, ocb->len,input);
fclose ( input );
return buffer;
}
OSL_IMAGE * ocb_loadimage(const char * filename, int location, int pixelFormat) { //loads image
OCB_FILE *ocb=malloc(sizeof(OCB_FILE));
ocb=ocb_exists_notify(filename);
free(imagebuff);
imagebuff=malloc(ocb->len);
imagebuff=ocb_read(ocb);
oslSetTempFileData(imagebuff, ocb->len, &VF_MEMORY);
char *ext, extension[10];
int i;
ext = strrchr(filename, '.');
if (!ext)
return NULL;
i = 0;
while(ext[i] && i < sizeof(extension) - 2)
{
extension[i] = tolower(ext[i]);
i++;
}
extension[i] = 0;
if (!strcmp(extension, ".png"))
return oslLoadImageFilePNG(oslGetTempFileName(), location, pixelFormat);
else if (!strcmp(extension, ".jpg"))
return oslLoadImageFileJPG(oslGetTempFileName(), location, pixelFormat);
else if (!strcmp(extension, ".gif"))
return oslLoadImageFileGIF(oslGetTempFileName(), location, pixelFormat);
return NULL;
}
Piotrek Żórawski