Files
SonicForgeStudio/native_bridge/include/FxShm.h
T

51 lines
1.3 KiB
C++

// native_bridge/include/FxShm.h
// openShm/closeShm dùng chung giữa RealtimeFxLoop.cpp (fx_vst_bridge
// --realtime-fx) và juce_fx/JuceFxLoop.cpp (juce_fx_bridge --juce-fx).
// Windows-only (FileMapping); mở mapping có sẵn do engine (Python) tạo.
#pragma once
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#endif
#include <string>
namespace fxshm {
struct ShmView {
void* map = nullptr;
void* view = nullptr;
};
inline ShmView* openShm(const std::string& name, size_t size) {
#ifdef _WIN32
int wlen = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, nullptr, 0);
std::wstring wname(wlen, L'\0');
MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, &wname[0], wlen);
HANDLE map = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, wname.c_str());
if (!map) return nullptr;
void* view = MapViewOfFile(map, FILE_MAP_ALL_ACCESS, 0, 0, size);
if (!view) { CloseHandle(map); return nullptr; }
ShmView* v = new ShmView();
v->map = map;
v->view = view;
return v;
#else
(void)name; (void)size;
return nullptr;
#endif
}
inline void closeShm(ShmView* v) {
if (!v) return;
#ifdef _WIN32
if (v->view) UnmapViewOfFile(v->view);
if (v->map) CloseHandle((HANDLE)v->map);
#endif
delete v;
}
} // namespace fxshm