Skip to content

Consistent shm interface - #32

Merged
davetbarr merged 4 commits into
mainfrom
consistent-shm-interface
Sep 24, 2026
Merged

davetbarr merged 4 commits into
mainfrom
consistent-shm-interface

Conversation

@scetre

@scetre scetre commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds a clearer primary API for daoShm in C and uses it from every language binding. The old API is kept: old C function names still work, so existing code keeps building and linking.

C library (dao.h / dao.c)

New primary functions, grouped by what they do:

Group Functions
Create daoShmCreate, daoShmCreate1D, daoShmCreateFifo, daoShmCreateSem
Open / close daoShmOpen, daoShmClose
Write daoShmSetData, daoShmSetDataQuiet, daoShmSetDataPart, daoShmSetDataPartFinalize
Read daoShmGetData, daoShmGetDataAt, daoShmGetDataNext, daoShmResetReadTail, daoShmCheckOverwrite
Wait / sync daoShmWaitData, daoShmWaitSem, daoShmWaitSemTimeout, daoShmWaitCounter, daoShmWaitTargetCounter, daoShmPostSem, daoShmPostSemAll
  • Backward compatible: every existing function (daoShmImageCreate, daoShmShm2Img, daoShmImage2Shm, daoShmInit1D, ...) keeps its signature and now calls the new function. No struct, typedef or #define changed in dao.h, so the IMAGE layout is unchanged.
  • daoShmImageCreateSem was declared in dao.h but never defined. It now has an implementation.
  • Removed include/daoShm.h. Nothing in the repo used it, its signatures no longer matched dao.c, and it had an out-of-date lastNbArray[512] (the real size is 2024). Code that included it should include dao.h. daoShm.hpp is unaffected.

Python

  • The ctypes bindings call the new C names, with argument order fixed to match.
  • The public shm class API is unchanged.
  • Added README.md and example_shm.py.

Julia

  • ccall wrappers renamed to the new C names.
  • Bug fixes: IMAGE_METADATA.lastNbArray was declared with 512 elements instead of 2024, and both IMAGE and IMAGE_METADATA were missing the trailing FIFO fields. Julia was passing C a buffer that was too small.
  • Added dao.close().
  • Added README.md and example_basic.jl, replacing the old one-line README.

MATLAB

  • Calls renamed to the new C names, and one argument order fixed.
  • Added close_shm / daoShm.close(). Before this, shm_init allocated an IMAGE that was never freed or closed.
  • Bug fix: shm_init used malloc instead of calloc, so daoShmClose called free() on uninitialised semReadPID / semWritePID pointers.
  • Removed the committed daomex.mex binary and added a .gitignore for it.
  • Added README.md and example_basic.m.

Rust

  • The binding was an unbuilt stub (main.rs only printed "Hello, world!"). This PR fixes its build setup:
    • wrapper.h now points to the real dao.h.
    • Removed the invalid [build] rustflags block in Cargo.toml.
    • bindgen is now a build dependency.
  • Added a safe Shm wrapper (src/lib.rs). It uses bindings that bindgen generates from dao.h, so the struct layout can't get out of sync with C the way Julia's did.
  • main.rs is now a runnable example. Added README.md and a .gitignore.

Testing

Tested create / write / read / reopen / close against a fresh build of libdao:

  • Python and Julia: run directly
  • MATLAB: through Octave / mkoctfile
  • Rust: cargo build / cargo run

Impact on existing code

  • C/C++ code using the old daoShm* names: no changes needed.
  • Code including daoShm.h: switch to dao.h. daoBase, daoHw and daoTools don't include it.
  • Julia / MATLAB users: the struct fixes and the new close() are behaviour fixes. Existing scripts should still work.

C (dao.c/dao.h): introduce daoShmCreate/daoShmOpen/daoShmSetData/
daoShmGetData/... as the primary, better-named API, grouped by
create / open-close / write / read / wait-synchronize. The old
daoShmImageCreate/daoShmShm2Img/daoShmImage2Shm/... names are kept as
thin forwarders for existing code, so nothing that already links
against libdao breaks. Also gives daoShmImageCreateSem, previously
declared in dao.h but never defined, a real implementation.

Remove daoShm.h/daoShm.hpp's C sibling: unused anywhere in the repo,
declared functions with signatures that no longer matched dao.c, and
a stale lastNbArray[512] (the real struct is 2024).

Python (daoShm.py): ctypes bindings now call the new C names, with
argument order fixed to match. Public API (the `shm` class) is
unchanged. Added README.md and example_shm.py.

Julia (dao.jl): ccall wrappers renamed to match. Also fixes two real
bugs found while doing this: IMAGE_METADATA.lastNbArray was declared
as 512 elements against the real 2024, and both IMAGE and
IMAGE_METADATA were missing trailing FIFO fields the C library
writes to, meaning Julia was under-allocating the buffer it hands to
C. Added a dao.close(), which didn't exist before. Added README.md
and example_basic.jl; folded the old bare README stub into it.

MATLAB (daomex.c/daoShm.m): calls renamed to match, one argument
order fixed. Added close_shm/daoShm.close(), which didn't exist
before - shm_init malloc'd an IMAGE and never freed it or called
daoShmClose. Fixed a related bug that surfaced only once close was
exercised: shm_init used malloc instead of calloc, so daoShmClose's
free() calls on semReadPID/semWritePID ran against garbage pointers.
Added README.md and example_basic.m; folded the old bare README
stub into it. Removed the previously-committed compiled daomex.mex
binary and added .gitignore for it.

Rust: was an unbuilt stub (main.rs was literally "Hello, world!");
the existing build.rs/wrapper.h/Cargo.toml scaffold had real bugs
(wrapper.h pointed at a build/include/dao.h path waf never creates,
Cargo.toml had an invalid [build] rustflags block and bindgen in the
wrong dependency section). Fixed the plumbing and added a safe Shm
wrapper (src/lib.rs) over bindgen-generated bindings from the real
dao.h, so the struct layout can't drift the way it did in Julia.
main.rs is now a runnable example. Added README.md and .gitignore
for target/ and Cargo.lock.

Tested end-to-end (create/write/read/reopen/close) against a fresh
build of libdao: Python and Julia directly, MATLAB via Octave/
mkoctfile, Rust via cargo build/run.

@davetbarr davetbarr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good ready to merge

@davetbarr
davetbarr merged commit 70d3fe0 into main Sep 24, 2026
2 checks passed
@scetre
scetre deleted the consistent-shm-interface branch October 6, 2026 08:39
@scetre
scetre restored the consistent-shm-interface branch October 6, 2026 08:39
@scetre
scetre deleted the consistent-shm-interface branch October 6, 2026 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants