ogl_beamforming

Ultrasound Beamforming Implemented with OpenGL
git clone anongit@rnpnr.xyz:ogl_beamforming.git
Log | Files | Refs | Feed | Submodules | README | LICENSE

Commit: af0d29cc2d510fd2d90dc5f8c4054bef5f71fdcd
Parent: 829bc0d2e17aab601e9d10d03a67dda84807b7c3
Author: Randy Palamar
Date:   Tue, 31 Mar 2026 17:32:50 -0600

lib: fix shared memory size query on w32

GetFileSizeEx() cannot be used on a file mapping handle. Instead
it turns out that you can pass MapViewOfFile as size of 0 to tell
it to map the whole region. Then you can use the very easy to find
VirtualQuery() function to figure out the size that was actually
mapped.

My bad for trusting the clanker, it really doesn't seem to
understand how virtual memory works.

closes #52

Diffstat:
Mlib/ogl_beamformer_lib.c | 35+++++++++++++++++++++++++----------
1 file changed, 25 insertions(+), 10 deletions(-)

diff --git a/lib/ogl_beamformer_lib.c b/lib/ogl_beamformer_lib.c @@ -61,7 +61,7 @@ os_close_shared_memory_area(void *memory, i64 size) #elif OS_WINDOWS -W32(b32) GetFileSizeEx(iptr, i64 *); +W32(u64) VirtualQuery(void *base_address, void *memory_basic_info, u64 memory_basic_info_size); W32(b32) UnmapViewOfFile(void *); function b32 @@ -95,19 +95,34 @@ os_reserve_region_locks(void) function s8 os_open_shared_memory_area(char *name) { + struct alignas(16) { + void *BaseAddress; + void *AllocationBase; + u32 AllocationProtect; + u32 __alignment1; + u64 RegionSize; + u32 State; + u32 Protect; + u32 Type; + u32 __alignment2; + } memory_basic_info; + s8 result = {0}; iptr h = OpenFileMappingA(FILE_MAP_ALL_ACCESS, 0, name); if (h != INVALID_FILE) { - i64 size; - if (GetFileSizeEx(h, &size)) { - void *new = MapViewOfFile(h, FILE_MAP_ALL_ACCESS, 0, 0, size); - if (new && os_reserve_region_locks()) { - result.data = new; - result.len = size; - } - if (new && !result.data) - UnmapViewOfFile(new); + // NOTE(rnp): a size of 0 maps the whole region, we can determine its size after + void *new = MapViewOfFile(h, FILE_MAP_ALL_ACCESS, 0, 0, 0); + if (new && + VirtualQuery(new, &memory_basic_info, sizeof(memory_basic_info)) == sizeof(memory_basic_info) && + os_reserve_region_locks()) + { + result.data = new; + result.len = (i64)memory_basic_info.RegionSize; } + + if (new && !result.data) + UnmapViewOfFile(new); + CloseHandle(h); } return result;