Improve file reading

This commit is contained in:
Maschell 2022-08-09 16:02:38 +02:00
parent d5c54fed78
commit 72901bc7a5

View File

@ -4,7 +4,6 @@
#include <malloc.h>
#include <unistd.h>
#define ROUNDDOWN(val, align) ((val) & ~(align - 1))
#define ROUNDUP(val, align) ROUNDDOWN(((val) + (align - 1)), align)
@ -20,8 +19,13 @@ int32_t LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_t *size)
return -1;
}
uint32_t filesize = lseek(iFd, 0, SEEK_END);
lseek(iFd, 0, SEEK_SET);
struct stat file_stat;
int rc = fstat(iFd, &file_stat);
if (rc < 0) {
close(iFd);
return -4;
}
uint32_t filesize = file_stat.st_size;
auto *buffer = (uint8_t *) memalign(0x40, ROUNDUP(filesize, 0x40));
if (buffer == nullptr) {
@ -29,7 +33,7 @@ int32_t LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_t *size)
return -2;
}
uint32_t blocksize = 0x20000;
uint32_t blocksize = 0x80000;
uint32_t done = 0;
int32_t readBytes;
@ -38,8 +42,9 @@ int32_t LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_t *size)
blocksize = filesize - done;
}
readBytes = read(iFd, buffer + done, blocksize);
if (readBytes <= 0)
if (readBytes <= 0) {
break;
}
done += readBytes;
}