Hi, Odin community! I’m working on bindings for WiredTiger (MongoDB’s storage engine) and running into issues with segfaults and invalid arguments. I’d really appreciate some help debugging this.
Current Implementation
Here’s my current binding code:
package wiredtiger_odin
import "core:c"
import "core:fmt"
import "core:os"
import "core:strings"
struct__wt_session :: struct {
connection: ^WT_CONNECTION,
close: proc "c" (session: ^WT_SESSION, opts: cstring) -> c.int,
open_cursor: proc "c" (
session: ^WT_SESSION,
uri: Maybe(cstring),
to_dup: Maybe(^WT_CURSOR),
config: cstring,
cursorp: ^^WT_CURSOR,
) -> c.int,
create: proc "c" (
session: ^WT_SESSION,
name: cstring,
config: cstring,
) -> c.int,
....
@(link_prefix = "wiredtiger_")
foreign wiredtiger {
open :: proc(home: cstring, event_handler: ^WT_EVENT_HANDLER, config: cstring, connection: ^^WT_CONNECTION) -> c.int ---
strerror :: proc(error: c.int) -> cstring ---
}
}
// Example usage in main:
main :: proc() {
conn: ^WT_CONNECTION = nil
session: ^WT_SESSION = nil
cursor: ^WT_CURSOR = nil
// Setup connection
config_cstr := strings.clone_to_cstring("create,cache_size=100MB,async=(enabled=false)")
defer delete(config_cstr)
ret := open(home_cstr, nil, config_cstr, &conn)
// Open session
session_ret := conn.open_session(conn, nil, nil, &session)
// Try to open cursor
table_uri := strings.clone_to_cstring("table:test")
defer delete(table_uri)
cursor_ret := session.open_cursor(
session,
table_uri,
nil,
nil,
&cursor,
)
}
The Issue
When conn.open_session()
is called, I get the error:
“WT_session → session.open_cursor should be passed either a URI or a cursor to duplicate, but not both”.
I’ve tried:
- Using
Maybe
types for the nullable parameters in the struct definition - Explicitly passing nil for unused parameters
- Various string handling approaches
Questions
- Am I handling the C function pointers correctly in the struct definitions?
- Is there a better way to handle nullable parameters in C bindings?
Relevant Information
- WiredTiger version: 11.3.0
- Odin version: Latest
- OS: Ubuntu Linux running on WSL
- The C header defines one of these parameters (*to_dup) using the
WT_HANDLE_NULLABLE
macro which looks like this-
#define WT_HANDLE_NULLABLE(typename) typename##_NULLABLE