From af3e42d61678cd6c7691cce2edd0343f070af85a Mon Sep 17 00:00:00 2001 From: KSS <40847080+KSSBrawl@users.noreply.github.com> Date: Wed, 8 Jan 2025 06:01:00 -0600 Subject: [PATCH 1/4] Backport ZunMemory functions --- src/ZunMemory.hpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/ZunMemory.hpp diff --git a/src/ZunMemory.hpp b/src/ZunMemory.hpp new file mode 100644 index 00000000..9f0a7f80 --- /dev/null +++ b/src/ZunMemory.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +namespace th06 +{ +namespace ZunMemory +{ +inline void *Alloc(u32 size) +{ + return malloc(size); +} + +inline void Free(void *ptr) +{ + free(ptr); +} +}; // namespace ZunMemory +}; // namespace th06 From 668cc41f9497ef028539bdb2b39ac52a946a34d0 Mon Sep 17 00:00:00 2001 From: KSS <40847080+KSSBrawl@users.noreply.github.com> Date: Wed, 8 Jan 2025 06:06:22 -0600 Subject: [PATCH 2/4] Backport MidiOutput::Ntohl MidiOutput::ParseFile is temporarily non-matching again with this commit. --- src/MidiOutput.cpp | 20 +++----------------- src/MidiOutput.hpp | 12 ++++++++++++ 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/MidiOutput.cpp b/src/MidiOutput.cpp index 7a3890d4..82ac912f 100644 --- a/src/MidiOutput.cpp +++ b/src/MidiOutput.cpp @@ -222,21 +222,10 @@ void MidiOutput::ClearTracks() this->numTracks = 0; } -inline u32 InlineNtohl(u32 val, u8 *tmp) -{ - tmp[0] = ((u8 *)&val)[3]; - tmp[1] = ((u8 *)&val)[2]; - tmp[2] = ((u8 *)&val)[1]; - tmp[3] = ((u8 *)&val)[0]; - - return *(const u32 *)tmp; -} - #pragma var_order(trackIdx, currentCursor, currentCursorTrack, fileData, hdrLength, hdrRaw, trackLength, \ - endOfHeaderPointer, tmpHdrLength, trackArraySize, tmpTrackLength, trackLengthRaw) + endOfHeaderPointer, trackArraySize) ZunResult MidiOutput::ParseFile(i32 fileIdx) { - u8 trackLengthRaw[4]; u8 hdrRaw[8]; u32 trackLength; u8 *currentCursor, *currentCursorTrack, *endOfHeaderPointer; @@ -244,8 +233,6 @@ ZunResult MidiOutput::ParseFile(i32 fileIdx) u8 *fileData; u32 hdrLength; size_t trackArraySize; - u8 tmpHdrLength[4]; - u8 tmpTrackLength[4]; this->ClearTracks(); currentCursor = this->midiFileData[fileIdx]; @@ -262,7 +249,7 @@ ZunResult MidiOutput::ParseFile(i32 fileIdx) // Get a pointer to the end of the header chunk currentCursor += sizeof(hdrRaw); - hdrLength = InlineNtohl(*(u32 *)(&hdrRaw[4]), tmpHdrLength); + hdrLength = MidiOutput::Ntohl(*(u32 *)(hdrRaw + 4)); endOfHeaderPointer = currentCursor; currentCursor += hdrLength; @@ -293,8 +280,7 @@ ZunResult MidiOutput::ParseFile(i32 fileIdx) // Read a track (MTrk) chunk. // // First, read the length of the chunk - memcpy(trackLengthRaw, currentCursorTrack + 4, 4); - trackLength = InlineNtohl(*(u32 *)trackLengthRaw, tmpTrackLength); + trackLength = MidiOutput::Ntohl(*(u32 *)(currentCursorTrack + 4)); this->tracks[trackIdx].trackLength = trackLength; this->tracks[trackIdx].trackData = (u8 *)malloc(trackLength); this->tracks[trackIdx].trackPlaying = 1; diff --git a/src/MidiOutput.hpp b/src/MidiOutput.hpp index 7687e138..4d3c94e1 100644 --- a/src/MidiOutput.hpp +++ b/src/MidiOutput.hpp @@ -133,6 +133,18 @@ struct MidiOutput : MidiTimer static u16 Ntohs(u16 val); static u32 SkipVariableLength(u8 **curTrackDataCursor); + static u32 Ntohl(u32 val) + { + u8 tmp[4]; + + tmp[0] = ((u8 *)&val)[3]; + tmp[1] = ((u8 *)&val)[2]; + tmp[2] = ((u8 *)&val)[1]; + tmp[3] = ((u8 *)&val)[0]; + + return *(const u32 *)tmp; + } + MIDIHDR *midiHeaders[32]; i32 midiHeadersCursor; u8 *midiFileData[32]; From 4c3837c03582ed10870c9cfea79da4c73a236188 Mon Sep 17 00:00:00 2001 From: KSS <40847080+KSSBrawl@users.noreply.github.com> Date: Wed, 8 Jan 2025 06:09:56 -0600 Subject: [PATCH 3/4] Re-match MidiOutput::ParseFile --- src/MidiOutput.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/MidiOutput.cpp b/src/MidiOutput.cpp index 82ac912f..8842ddfc 100644 --- a/src/MidiOutput.cpp +++ b/src/MidiOutput.cpp @@ -6,6 +6,7 @@ #include "FileSystem.hpp" #include "MidiOutput.hpp" #include "Supervisor.hpp" +#include "ZunMemory.hpp" #include "i18n.hpp" #include "utils.hpp" @@ -223,7 +224,7 @@ void MidiOutput::ClearTracks() } #pragma var_order(trackIdx, currentCursor, currentCursorTrack, fileData, hdrLength, hdrRaw, trackLength, \ - endOfHeaderPointer, trackArraySize) + endOfHeaderPointer) ZunResult MidiOutput::ParseFile(i32 fileIdx) { u8 hdrRaw[8]; @@ -232,7 +233,6 @@ ZunResult MidiOutput::ParseFile(i32 fileIdx) i32 trackIdx; u8 *fileData; u32 hdrLength; - size_t trackArraySize; this->ClearTracks(); currentCursor = this->midiFileData[fileIdx]; @@ -269,8 +269,7 @@ ZunResult MidiOutput::ParseFile(i32 fileIdx) this->numTracks = MidiOutput::Ntohs(*(u16 *)(endOfHeaderPointer + 2)); // Allocate this->divisions * 32 bytes. - trackArraySize = sizeof(MidiTrack) * this->numTracks; - this->tracks = (MidiTrack *)malloc(trackArraySize); + this->tracks = (MidiTrack *)ZunMemory::Alloc(sizeof(MidiTrack) * this->numTracks); memset(this->tracks, 0, sizeof(MidiTrack) * this->numTracks); for (trackIdx = 0; trackIdx < this->numTracks; trackIdx += 1) { @@ -282,7 +281,7 @@ ZunResult MidiOutput::ParseFile(i32 fileIdx) // First, read the length of the chunk trackLength = MidiOutput::Ntohl(*(u32 *)(currentCursorTrack + 4)); this->tracks[trackIdx].trackLength = trackLength; - this->tracks[trackIdx].trackData = (u8 *)malloc(trackLength); + this->tracks[trackIdx].trackData = (u8 *)ZunMemory::Alloc(trackLength); this->tracks[trackIdx].trackPlaying = 1; memcpy(this->tracks[trackIdx].trackData, currentCursor, trackLength); currentCursor += trackLength; From 91de872d3be12d6b1130025480f3e9383f9389bd Mon Sep 17 00:00:00 2001 From: KSS <40847080+KSSBrawl@users.noreply.github.com> Date: Wed, 8 Jan 2025 06:22:43 -0600 Subject: [PATCH 4/4] ZunMemory::Alloc - u32->size_t --- src/ZunMemory.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ZunMemory.hpp b/src/ZunMemory.hpp index 9f0a7f80..83ac47e9 100644 --- a/src/ZunMemory.hpp +++ b/src/ZunMemory.hpp @@ -6,7 +6,7 @@ namespace th06 { namespace ZunMemory { -inline void *Alloc(u32 size) +inline void *Alloc(size_t size) { return malloc(size); }