Skip to content

Commit

Permalink
Add function to re initialize foreign type
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Holy <tim.holy@gmail.com>
  • Loading branch information
vchuravy and timholy committed Nov 23, 2022
1 parent b05fe3e commit 5eb6195
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/datatype.c
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,24 @@ JL_DLLEXPORT jl_datatype_t * jl_new_foreign_type(jl_sym_t *name,
return bt;
}

JL_DLLEXPORT int jl_reinit_foreign_type(jl_datatype_t *dt,
jl_markfunc_t markfunc,
jl_sweepfunc_t sweepfunc)
{
if (!jl_is_foreign_type(dt))
return 0;
jl_datatype_layout_t *layout = (jl_datatype_layout_t *)
jl_gc_perm_alloc(sizeof(jl_datatype_layout_t) + sizeof(jl_fielddescdyn_t),
0, 4, 0);
*layout = *(dt->layout);
jl_fielddescdyn_t * desc =
(jl_fielddescdyn_t *) ((char *)layout + sizeof(*layout));
desc->markfunc = markfunc;
desc->sweepfunc = sweepfunc;
dt->layout = layout;
return 1;
}

JL_DLLEXPORT int jl_is_foreign_type(jl_datatype_t *dt)
{
return jl_is_datatype(dt) && dt->layout && dt->layout->fielddesc_type == 3;
Expand Down
1 change: 1 addition & 0 deletions src/jl_exported_funcs.inc
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@
XX(jl_new_code_info_uninit) \
XX(jl_new_datatype) \
XX(jl_new_foreign_type) \
XX(jl_reinit_foreign_type) \
XX(jl_new_method_instance_uninit) \
XX(jl_new_method_table) \
XX(jl_new_method_uninit) \
Expand Down
6 changes: 6 additions & 0 deletions src/julia_gcext.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ JL_DLLEXPORT jl_datatype_t *jl_new_foreign_type(
int haspointers,
int large);


JL_DLLEXPORT int jl_reinit_foreign_type(
jl_datatype_t *dt,
jl_markfunc_t markfunc,
jl_sweepfunc_t sweepfunc);

JL_DLLEXPORT int jl_is_foreign_type(jl_datatype_t *dt);

JL_DLLEXPORT size_t jl_gc_max_internal_obj_size(void);
Expand Down
8 changes: 8 additions & 0 deletions test/gcext/Foreign/Manifest.toml
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"
6 changes: 6 additions & 0 deletions test/gcext/Foreign/Project.toml
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"
34 changes: 34 additions & 0 deletions test/gcext/Foreign/deps/foreignlib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This file is a part of Julia. License is MIT: https://julialang.org/license

#include "julia.h"
#include "julia_gcext.h"

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++;
}

void init_dt_gc(jl_datatype_t *dt)
{
jl_reinit_foreign_type(dt, mark, sweep);
nmarks = nsweeps = 0;
}

int nmark_counter()
{
return nmarks;
}

int nsweep_counter()
{
return nsweeps;
}
21 changes: 21 additions & 0 deletions test/gcext/Foreign/src/Foreign.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

module Foreign

using Libdl

const foreignlib = joinpath(dirname(joinpath(@__DIR__)), "deps", "foreignlib.so")

const FObj = ccall(:jl_new_foreign_type, Any, (Symbol, Module, Any, Any, Any, Cint, Cint),
:FObj, Foreign, Any, C_NULL, C_NULL, 0, 0)

FObj() = ccall(:jl_new_struct_uninit, Any, (Any,), FObj)

get_nmark() = ccall((:nmark_counter, foreignlib), Cint, ())
get_nsweep() = ccall((:nsweep_counter, foreignlib), Cint, ())

function __init__()
ccall((:init_dt_gc, foreignlib), Cvoid, (Any,), FObj)
end

end # module Foreign
15 changes: 12 additions & 3 deletions test/gcext/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,42 @@ SRCDIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
# get the executable suffix, if any
EXE := $(suffix $(abspath $(JULIA)))

DYLIB := .so

# get compiler and linker flags. (see: `contrib/julia-config.jl`)
JULIA_CONFIG := $(JULIA) -e 'include(joinpath(Sys.BINDIR, Base.DATAROOTDIR, "julia", "julia-config.jl"))' --
CPPFLAGS_ADD :=
CFLAGS_ADD = $(shell $(JULIA_CONFIG) --cflags)
LDFLAGS_ADD = -lm $(shell $(JULIA_CONFIG) --ldflags --ldlibs)
DYLIBFLAGS := --shared -fPIC

DEBUGFLAGS += -g

#=============================================================================

release: $(BIN)/gcext$(EXE)
debug: $(BIN)/gcext-debug$(EXE)
release: $(BIN)/gcext$(EXE) $(BIN)/Foreign/deps/foreignlib$(DYLIB)
debug: $(BIN)/gcext-debug$(EXE) $(BIN)/Foreign/deps/foreignlib-debug$(DYLIB)

$(BIN)/gcext$(EXE): $(SRCDIR)/gcext.c
$(CC) $^ -o $@ $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS) $(LDFLAGS_ADD) $(LDFLAGS)

$(BIN)/gcext-debug$(EXE): $(SRCDIR)/gcext.c
$(CC) $^ -o $@ $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS) $(LDFLAGS_ADD) $(LDFLAGS) $(DEBUGFLAGS)

$(BIN)/Foreign/deps/foreignlib$(DYLIB): $(SRCDIR)/Foreign/deps/foreignlib.c
$(CC) $^ -o $@ $(DYLIBFLAGS) $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS) $(LDFLAGS_ADD) $(LDFLAGS)

$(BIN)/Foreign/deps/foreignlib-debug$(DYLIB): $(SRCDIR)/Foreign/deps/foreignlib.c
$(CC) $^ -o $@ $(DYLIBFLAGS) $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS) $(LDFLAGS_ADD) $(LDFLAGS) $(DEBUGFLAGS)

ifneq ($(abspath $(BIN)),$(abspath $(SRCDIR)))
# for demonstration purposes, our demo code is also installed
# in $BIN, although this would likely not be typical
$(BIN)/LocalModule.jl: $(SRCDIR)/LocalModule.jl
cp $< $@
endif

check: $(BIN)/gcext$(EXE) $(BIN)/LocalTest.jl
check: $(BIN)/gcext$(EXE) $(BIN)/LocalTest.jl $(BIN)/Foreign/deps/foreignlib$(DYLIB)
$(JULIA) --depwarn=error $(SRCDIR)/gcext-test.jl $<
@echo SUCCESS

Expand Down
19 changes: 19 additions & 0 deletions test/gcext/gcext-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# tests the output of the embedding example is correct
using Test
using Pkg

if Sys.iswindows()
# libjulia needs to be in the same directory as the embedding executable or in path
Expand Down Expand Up @@ -40,3 +41,21 @@ end
@test checknum(lines[6], r"([0-9]+) corrupted auxiliary roots",
n -> n == 0)
end

@testset "Package with foreign type" begin
load_path = joinpath(@__DIR__, "Foreign")
push!(LOAD_PATH, load_path)
try
(@eval (using Foreign))
@test Base.invokelatest(Foreign.get_nmark) == 0
@test Base.invokelatest(Foreign.get_nsweep) == 0
x = [Base.invokelatest(Foreign.FObj) for _ in 1:10]
GC.gc(true)
x = nothing
GC.gc(true)
@test Base.invokelatest(Foreign.get_nmark) > 0
@test Base.invokelatest(Foreign.get_nsweep) > 0
finally
filter!(()(load_path), LOAD_PATH)
end
end

0 comments on commit 5eb6195

Please sign in to comment.