nim-ffi/ffi/internal/c_wire.nim
2026-06-23 12:55:07 -03:00

19 lines
708 B
Nim

## Runtime helpers for the macro-generated `*_CWire` companion types: only the
## `cstring` fields need allocation here (seq/Option are alloc'd inline by the
## macro), packed on pack and released on free.
import ../alloc
proc cwireAllocStr*(s: string): cstring {.inline.} =
## NUL-terminated `malloc` copy of `s` (see `ffi/alloc.nim`); pair with
## `cwireFreeStr`. Empty input still yields a valid buffer, never NULL.
alloc.alloc(s)
proc cwireFreeStr*(s: var cstring) {.inline.} =
## Idempotent free for a `cwireAllocStr` cstring; `nil` is a no-op. Taken by
## `var` and reset to `nil` after release so a repeated call can't double-free.
if s.isNil():
return
alloc.dealloc(s)
s = nil