Skip to content

Commit

Permalink
Only unload native library once at the end, not always in the desctru…
Browse files Browse the repository at this point in the history
…ctor
  • Loading branch information
sbrunk committed Oct 6, 2024
1 parent 6cc1aa5 commit 8b577af
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
27 changes: 17 additions & 10 deletions duckdb/_c_api/c_api.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,18 @@ fn get_libname() -> StringLiteral:
return "libduckdb.so"


@value
struct LibDuckDB:
var lib: DLHandle

fn __init__(inout self, path: String = get_libname()):
self.lib = DLHandle(path)

fn __del__(owned self):
self.lib.close()
@staticmethod
fn destroy(inout existing):
existing.lib.close()

fn __copyinit__(inout self, existing: Self):
self.lib = existing.lib

# ===--------------------------------------------------------------------===#
# Open/Connect
Expand Down Expand Up @@ -1136,11 +1139,13 @@ struct LibDuckDB:
* type: The child type of list type to create.
* returns: The logical type.
"""
return self.lib.get_function[fn (duckdb_logical_type) -> duckdb_logical_type](
"duckdb_create_list_type"
)(type)
return self.lib.get_function[
fn (duckdb_logical_type) -> duckdb_logical_type
]("duckdb_create_list_type")(type)

fn duckdb_create_array_type(self, type: duckdb_logical_type, array_size: idx_t) -> duckdb_logical_type:
fn duckdb_create_array_type(
self, type: duckdb_logical_type, array_size: idx_t
) -> duckdb_logical_type:
"""Creates an array type from its child type.
The resulting type should be destroyed with `duckdb_destroy_logical_type`.
Expand All @@ -1152,7 +1157,9 @@ struct LibDuckDB:
"duckdb_create_array_type"
)(type, array_size)

fn duckdb_create_map_type(self, key_type: duckdb_logical_type, value_type: duckdb_logical_type) -> duckdb_logical_type:
fn duckdb_create_map_type(
self, key_type: duckdb_logical_type, value_type: duckdb_logical_type
) -> duckdb_logical_type:
"""Creates a map type from its key type and value type.
The resulting type should be destroyed with `duckdb_destroy_logical_type`.
Expand Down Expand Up @@ -1183,7 +1190,8 @@ struct LibDuckDB:
self,
member_types: UnsafePointer[duckdb_logical_type],
member_names: UnsafePointer[UnsafePointer[C_char]],
member_count: idx_t) -> duckdb_logical_type:
member_count: idx_t,
) -> duckdb_logical_type:
"""Creates a STRUCT type from the passed member name and type arrays.
The resulting type should be destroyed with `duckdb_destroy_logical_type`.
Expand Down Expand Up @@ -1268,7 +1276,6 @@ struct LibDuckDB:
fn (duckdb_logical_type) -> duckdb_logical_type
]("duckdb_map_type_value_type")(type)


# fn duckdb_struct_type_child_count TODO
# fn duckdb_struct_type_child_name TODO
# fn duckdb_struct_type_child_type TODO
Expand Down
7 changes: 4 additions & 3 deletions duckdb/_c_api/libduckdb.mojo
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from duckdb._c_api.c_api import LibDuckDB
from sys.ffi import _get_global


fn _init_global(ignored: UnsafePointer[NoneType]) -> UnsafePointer[NoneType]:
var ptr = UnsafePointer[LibDuckDB].alloc(1)
ptr[] = LibDuckDB()
return ptr.bitcast[NoneType]()


fn _destroy_global(duckdb: UnsafePointer[NoneType]):
# var p = duckdb.bitcast[LibDuckDB]()
# LibDuckDB.destroy(p[])
var p = duckdb.bitcast[LibDuckDB]()
LibDuckDB.destroy(p[])
duckdb.free()


Expand All @@ -33,4 +34,4 @@ struct _DuckDBInterfaceImpl:


fn _impl() -> LibDuckDB:
return _get_global_duckdb_itf().libDuckDB()
return _get_global_duckdb_itf().libDuckDB()

0 comments on commit 8b577af

Please sign in to comment.