VFS: Optimize expanding in-memory files
Vicki Pfau vi@endrift.com
Sun, 16 Apr 2017 22:39:50 -0700
2 files changed,
20 insertions(+),
10 deletions(-)
M
CHANGES
→
CHANGES
@@ -103,6 +103,7 @@ - Qt: Add .gb/.gbc files to the extension list in Info.plist
- Feature: Make -l option explicit - Core: Ability to enumerate and modify video and audio channels - Debugger: Make attaching a backend idempotent + - VFS: Optimize expanding in-memory files 0.5.2: (2016-12-31) Bugfixes:
M
src/util/vfs/vfs-mem.c
→
src/util/vfs/vfs-mem.c
@@ -4,12 +4,14 @@ * This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */ #include <mgba-util/vfs.h> +#include <mgba-util/math.h> #include <mgba-util/memory.h> struct VFileMem { struct VFile d; void* mem; size_t size; + size_t bufferSize; size_t offset; };@@ -40,6 +42,7 @@ }
vfm->mem = mem; vfm->size = size; + vfm->bufferSize = size; vfm->offset = 0; vfm->d.close = _vfmClose; vfm->d.seek = _vfmSeek;@@ -67,6 +70,7 @@ }
vfm->mem = (void*) mem; vfm->size = size; + vfm->bufferSize = size; vfm->offset = 0; vfm->d.close = _vfmClose; vfm->d.seek = _vfmSeek;@@ -89,8 +93,9 @@ return 0;
} vfm->size = size; + vfm->bufferSize = toPow2(size); if (size) { - vfm->mem = anonymousMemoryMap(size); + vfm->mem = anonymousMemoryMap(vfm->bufferSize); if (mem) { memcpy(vfm->mem, mem, size); }@@ -113,15 +118,19 @@ return &vfm->d;
} void _vfmExpand(struct VFileMem* vfm, size_t newSize) { - void* oldBuf = vfm->mem; - vfm->mem = anonymousMemoryMap(newSize); - if (oldBuf) { - if (newSize < vfm->size) { - memcpy(vfm->mem, oldBuf, newSize); - } else { - memcpy(vfm->mem, oldBuf, vfm->size); + size_t alignedSize = toPow2(newSize); + if (alignedSize > vfm->bufferSize) { + void* oldBuf = vfm->mem; + vfm->mem = anonymousMemoryMap(alignedSize); + if (oldBuf) { + if (newSize < vfm->size) { + memcpy(vfm->mem, oldBuf, newSize); + } else { + memcpy(vfm->mem, oldBuf, vfm->size); + } + mappedMemoryFree(oldBuf, vfm->bufferSize); } - mappedMemoryFree(oldBuf, vfm->size); + vfm->bufferSize = alignedSize; } vfm->size = newSize; }@@ -135,7 +144,7 @@ }
bool _vfmCloseFree(struct VFile* vf) { struct VFileMem* vfm = (struct VFileMem*) vf; - mappedMemoryFree(vfm->mem, vfm->size); + mappedMemoryFree(vfm->mem, vfm->bufferSize); vfm->mem = 0; free(vfm); return true;