ogl_beamforming

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

Commit: aeb0aea4878e95d473a1fafa36868302afa969a2
Parent: 4e9d162efecc9a21d0917aa054d5acb4e97367f3
Author: Randy Palamar
Date:   Wed, 14 Aug 2024 20:48:01 -0600

allow arenas to be expanded after allocation

This is only useful on NVIDIA cards for resizing the CPU side rf
data buffer. In fact normal allocations using alloc() are still
not allowed to fail.

Diffstat:
Mbeamformer.c | 4+---
Mmain.c | 2+-
Mos_unix.c | 15++++++++++-----
Mos_win32.c | 13+++++++++----
4 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/beamformer.c b/beamformer.c @@ -89,9 +89,7 @@ alloc_shader_storage(BeamformerCtx *ctx, Arena a) full_rf_buf_size, map_flags); break; case GL_VENDOR_NVIDIA: - /* TODO: allow this to grow if the raw data has been resized */ - if (cs->raw_data_arena.beg == 0) - cs->raw_data_arena = os_new_arena(rf_raw_size); + cs->raw_data_arena = os_alloc_arena(cs->raw_data_arena, rf_raw_size); break; } diff --git a/main.c b/main.c @@ -158,7 +158,7 @@ main(void) { BeamformerCtx ctx = {0}; - Arena temp_memory = os_new_arena(256 * MEGABYTE); + Arena temp_memory = os_alloc_arena((Arena){0}, 8 * MEGABYTE); ctx.window_size = (uv2){.w = 960, .h = 720}; ctx.out_data_dim = (uv4){.x = 256, .y = 1024, .z = 1}; diff --git a/os_unix.c b/os_unix.c @@ -19,17 +19,22 @@ typedef struct { } os_file_stats; static Arena -os_new_arena(size capacity) +os_alloc_arena(Arena a, size capacity) { - Arena a = {0}; - size pagesize = sysconf(_SC_PAGESIZE); if (capacity % pagesize != 0) - capacity += (pagesize - capacity % pagesize); + capacity += pagesize - capacity % pagesize; + + size oldsize = a.end - a.beg; + if (oldsize > capacity) + return a; + + if (a.beg) + munmap(a.beg, oldsize); a.beg = mmap(0, capacity, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); if (a.beg == MAP_FAILED) - die("os_new_arena: couldn't allocate memory\n"); + die("os_alloc_arena: couldn't allocate memory\n"); a.end = a.beg + capacity; return a; } diff --git a/os_win32.c b/os_win32.c @@ -25,19 +25,24 @@ typedef struct { } os_file_stats; static Arena -os_new_arena(size capacity) +os_alloc_arena(Arena a, size capacity) { - Arena a = {0}; - SYSTEM_INFO Info; GetSystemInfo(&Info); if (capacity % Info.dwPageSize != 0) capacity += (Info.dwPageSize - capacity % Info.dwPageSize); + size oldsize = a.end - a.beg; + if (oldsize > capacity) + return a; + + if (a.beg) + VirtualFree(a.beg, oldsize, MEM_RELEASE); + a.beg = VirtualAlloc(0, capacity, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); if (a.beg == NULL) - die("os_new_arena: couldn't allocate memory\n"); + die("os_alloc_arena: couldn't allocate memory\n"); a.end = a.beg + capacity; return a; }