mirror of
https://github.com/libretro/mgba.git
synced 2024-11-30 19:50:34 +00:00
VFS: Optimize expanding in-memory files
This commit is contained in:
parent
5665ac0316
commit
fbb02475da
1
CHANGES
1
CHANGES
@ -103,6 +103,7 @@ Misc:
|
|||||||
- Feature: Make -l option explicit
|
- Feature: Make -l option explicit
|
||||||
- Core: Ability to enumerate and modify video and audio channels
|
- Core: Ability to enumerate and modify video and audio channels
|
||||||
- Debugger: Make attaching a backend idempotent
|
- Debugger: Make attaching a backend idempotent
|
||||||
|
- VFS: Optimize expanding in-memory files
|
||||||
|
|
||||||
0.5.2: (2016-12-31)
|
0.5.2: (2016-12-31)
|
||||||
Bugfixes:
|
Bugfixes:
|
||||||
|
@ -4,12 +4,14 @@
|
|||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
#include <mgba-util/vfs.h>
|
#include <mgba-util/vfs.h>
|
||||||
|
#include <mgba-util/math.h>
|
||||||
#include <mgba-util/memory.h>
|
#include <mgba-util/memory.h>
|
||||||
|
|
||||||
struct VFileMem {
|
struct VFileMem {
|
||||||
struct VFile d;
|
struct VFile d;
|
||||||
void* mem;
|
void* mem;
|
||||||
size_t size;
|
size_t size;
|
||||||
|
size_t bufferSize;
|
||||||
size_t offset;
|
size_t offset;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -40,6 +42,7 @@ struct VFile* VFileFromMemory(void* mem, size_t size) {
|
|||||||
|
|
||||||
vfm->mem = mem;
|
vfm->mem = mem;
|
||||||
vfm->size = size;
|
vfm->size = size;
|
||||||
|
vfm->bufferSize = size;
|
||||||
vfm->offset = 0;
|
vfm->offset = 0;
|
||||||
vfm->d.close = _vfmClose;
|
vfm->d.close = _vfmClose;
|
||||||
vfm->d.seek = _vfmSeek;
|
vfm->d.seek = _vfmSeek;
|
||||||
@ -67,6 +70,7 @@ struct VFile* VFileFromConstMemory(const void* mem, size_t size) {
|
|||||||
|
|
||||||
vfm->mem = (void*) mem;
|
vfm->mem = (void*) mem;
|
||||||
vfm->size = size;
|
vfm->size = size;
|
||||||
|
vfm->bufferSize = size;
|
||||||
vfm->offset = 0;
|
vfm->offset = 0;
|
||||||
vfm->d.close = _vfmClose;
|
vfm->d.close = _vfmClose;
|
||||||
vfm->d.seek = _vfmSeek;
|
vfm->d.seek = _vfmSeek;
|
||||||
@ -89,8 +93,9 @@ struct VFile* VFileMemChunk(const void* mem, size_t size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
vfm->size = size;
|
vfm->size = size;
|
||||||
|
vfm->bufferSize = toPow2(size);
|
||||||
if (size) {
|
if (size) {
|
||||||
vfm->mem = anonymousMemoryMap(size);
|
vfm->mem = anonymousMemoryMap(vfm->bufferSize);
|
||||||
if (mem) {
|
if (mem) {
|
||||||
memcpy(vfm->mem, mem, size);
|
memcpy(vfm->mem, mem, size);
|
||||||
}
|
}
|
||||||
@ -113,15 +118,19 @@ struct VFile* VFileMemChunk(const void* mem, size_t size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _vfmExpand(struct VFileMem* vfm, size_t newSize) {
|
void _vfmExpand(struct VFileMem* vfm, size_t newSize) {
|
||||||
|
size_t alignedSize = toPow2(newSize);
|
||||||
|
if (alignedSize > vfm->bufferSize) {
|
||||||
void* oldBuf = vfm->mem;
|
void* oldBuf = vfm->mem;
|
||||||
vfm->mem = anonymousMemoryMap(newSize);
|
vfm->mem = anonymousMemoryMap(alignedSize);
|
||||||
if (oldBuf) {
|
if (oldBuf) {
|
||||||
if (newSize < vfm->size) {
|
if (newSize < vfm->size) {
|
||||||
memcpy(vfm->mem, oldBuf, newSize);
|
memcpy(vfm->mem, oldBuf, newSize);
|
||||||
} else {
|
} else {
|
||||||
memcpy(vfm->mem, oldBuf, vfm->size);
|
memcpy(vfm->mem, oldBuf, vfm->size);
|
||||||
}
|
}
|
||||||
mappedMemoryFree(oldBuf, vfm->size);
|
mappedMemoryFree(oldBuf, vfm->bufferSize);
|
||||||
|
}
|
||||||
|
vfm->bufferSize = alignedSize;
|
||||||
}
|
}
|
||||||
vfm->size = newSize;
|
vfm->size = newSize;
|
||||||
}
|
}
|
||||||
@ -135,7 +144,7 @@ bool _vfmClose(struct VFile* vf) {
|
|||||||
|
|
||||||
bool _vfmCloseFree(struct VFile* vf) {
|
bool _vfmCloseFree(struct VFile* vf) {
|
||||||
struct VFileMem* vfm = (struct VFileMem*) vf;
|
struct VFileMem* vfm = (struct VFileMem*) vf;
|
||||||
mappedMemoryFree(vfm->mem, vfm->size);
|
mappedMemoryFree(vfm->mem, vfm->bufferSize);
|
||||||
vfm->mem = 0;
|
vfm->mem = 0;
|
||||||
free(vfm);
|
free(vfm);
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
Reference in New Issue
Block a user