Generate go wrappers using cgo instead of syscall #94
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary of new and updated functionality:
runtime.SetFinalizer
(manual management is still allowed)Closes #93
Key features
Examples generated using from lib3mf.
Importing library
It is required to have the
.dll
,.so
,.dylib
,.a
, ... located next to the executable or in a system wide folder.lib3mf_abi.h
andlib3mf_types.h
are necessary only when building the executable and they should be located next to the go binding file.Automatic memory managment
The wrapped C pointer is stored in a Go struct and it uses some dark magic tricks together with
runtime.SetFinalizer
to release the C memory when Go garbage collector collect the Go struct. It is save to manually callRelease
to release the memory before the garbage collector passes.Error wrapping
The return errors are of type
WrappedError
, this way users can inspect which is the error code without losing the error message.C Callbacks
cgo
cannot share Go pointers with C, and function are pointers, so it is somehow difficult to pass a function to C as parameter. It has to be done via a bridge C function and and exported Go function, and this is what has been implemented. There is one important limitation, as the Go bridge function is static and only one of each type can exist at the same time. It is somehow difficult to explain, so better document yourself at this blog.When callbacks are present
cfunc.go
file is also created, as//export
and C function declarations cannot coexist at the same time.