-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow re-initialization and caching of foreign types (#47407)
Co-authored-by: Tim Holy <[email protected]> Co-authored-by: Max Horn <[email protected]>
- Loading branch information
1 parent
93b9429
commit a397a1d
Showing
17 changed files
with
254 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/gcext | ||
/gcext-debug | ||
/Foreign/deps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# This file is machine-generated - editing it directly is not advised | ||
|
||
julia_version = "1.8.3" | ||
manifest_format = "2.0" | ||
project_hash = "e7199d961a5f4ebad68a3deaf5beaa7406a0afcb" | ||
|
||
[[deps.Foreign]] | ||
deps = ["Libdl"] | ||
path = "../Foreign" | ||
uuid = "de1f6f7a-d7b3-400f-91c2-33f248ee89c4" | ||
version = "0.1.0" | ||
|
||
[[deps.Libdl]] | ||
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
name = "DependsOnForeign" | ||
uuid = "4b0716e0-dfb5-4e00-8b44-e2685a41517f" | ||
version = "0.1.0" | ||
|
||
[deps] | ||
Foreign = "de1f6f7a-d7b3-400f-91c2-33f248ee89c4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module DependsOnForeign | ||
|
||
using Foreign | ||
|
||
f(obj::FObj) = Base.pointer_from_objref(obj) | ||
precompile(f, (FObj,)) | ||
|
||
const FObjRef = Ref{FObj}() | ||
|
||
function __init__() | ||
FObjRef[] = FObj() | ||
end | ||
|
||
end # module DependsOnForeign |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# This file is machine-generated - editing it directly is not advised | ||
|
||
julia_version = "1.9.0-DEV" | ||
manifest_format = "2.0" | ||
project_hash = "7b70172a2edbdc772ed789e79d4411d7528eae86" | ||
|
||
[[deps.Libdl]] | ||
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
name = "Foreign" | ||
uuid = "de1f6f7a-d7b3-400f-91c2-33f248ee89c4" | ||
version = "0.1.0" | ||
|
||
[deps] | ||
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// This file is a part of Julia. License is MIT: https://julialang.org/license | ||
|
||
#include "julia.h" | ||
#include "julia_gcext.h" | ||
|
||
// TODO make these atomics | ||
int nmarks = 0; | ||
int nsweeps = 0; | ||
|
||
uintptr_t mark(jl_ptls_t ptls, jl_value_t *p) | ||
{ | ||
nmarks += 1; | ||
return 0; | ||
} | ||
|
||
void sweep(jl_value_t *p) | ||
{ | ||
nsweeps++; | ||
} | ||
|
||
JL_DLLEXPORT jl_datatype_t *declare_foreign(jl_sym_t* name, jl_module_t *module, jl_datatype_t *parent) | ||
{ | ||
return jl_new_foreign_type(name, module, parent, mark, sweep, 1, 0); | ||
} | ||
|
||
// #define GC_MAX_SZCLASS (2032 - sizeof(void *)) | ||
|
||
JL_DLLEXPORT int reinit_foreign(jl_datatype_t *dt) | ||
{ | ||
int ret = jl_reinit_foreign_type(dt, mark, sweep); | ||
nmarks = nsweeps = 0; | ||
if (ret == 0) | ||
return 0; | ||
if (dt->layout->npointers != 1) | ||
return -1; | ||
if (dt->layout->size != 0) | ||
return -2; | ||
return ret; | ||
} | ||
|
||
JL_DLLEXPORT jl_value_t *allocate_foreign(jl_ptls_t ptls, size_t sz, jl_datatype_t *dt) | ||
{ | ||
jl_value_t* obj = jl_gc_alloc_typed(ptls, sz, dt); | ||
jl_gc_schedule_foreign_sweepfunc(ptls, obj); | ||
return obj; | ||
} | ||
|
||
JL_DLLEXPORT int nmark_counter() | ||
{ | ||
return nmarks; | ||
} | ||
|
||
JL_DLLEXPORT int nsweep_counter() | ||
{ | ||
return nsweeps; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# This file is a part of Julia. License is MIT: https://julialang.org/license | ||
|
||
module Foreign | ||
|
||
using Libdl | ||
|
||
const foreignlib = joinpath(ENV["BINDIR"], "foreignlib.$(dlext)") | ||
|
||
const FObj = ccall((:declare_foreign, foreignlib), Any, (Any, Any, Any), :FObj, @__MODULE__, Any) | ||
FObj() = ccall((:allocate_foreign, foreignlib), Any, (Ptr{Cvoid}, Csize_t, Any,), Core.getptls(), sizeof(Ptr{Cvoid}), FObj)::FObj | ||
|
||
export FObj | ||
|
||
get_nmark() = ccall((:nmark_counter, foreignlib), Cint, ()) | ||
get_nsweep() = ccall((:nsweep_counter, foreignlib), Cint, ()) | ||
|
||
function __init__() | ||
@assert ccall((:reinit_foreign, foreignlib), Cint, (Any,), FObj) == 1 | ||
end | ||
|
||
allocs(N) = [Foreign.FObj() for _ in 1:N] | ||
|
||
function test(N) | ||
x = allocs(N) | ||
Core.donotdelete(x) | ||
x = nothing | ||
end | ||
|
||
end # module Foreign |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# This file is machine-generated - editing it directly is not advised | ||
|
||
julia_version = "1.8.3" | ||
manifest_format = "2.0" | ||
project_hash = "e7199d961a5f4ebad68a3deaf5beaa7406a0afcb" | ||
|
||
[[deps.Foreign]] | ||
deps = ["Libdl"] | ||
path = "../Foreign" | ||
uuid = "de1f6f7a-d7b3-400f-91c2-33f248ee89c4" | ||
version = "0.1.0" | ||
|
||
[[deps.Libdl]] | ||
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
name = "ForeignObjSerialization" | ||
uuid = "2c015d96-a6ca-42f0-bc68-f9090de6bc2c" | ||
version = "0.1.0" | ||
|
||
[deps] | ||
Foreign = "de1f6f7a-d7b3-400f-91c2-33f248ee89c4" |
6 changes: 6 additions & 0 deletions
6
test/gcext/ForeignObjSerialization/src/ForeignObjSerialization.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module ForeignObjSerialization | ||
|
||
using Foreign | ||
const FObjRef = Ref{FObj}(FObj()) | ||
|
||
end # module ForeignObjSerialization |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters