Skip to content

Commit e06a591

Browse files
vchuravytimholyfingolfin
committed
Allow re-initialization and caching of foreign types (#47407)
Co-authored-by: Tim Holy <tim.holy@gmail.com> Co-authored-by: Max Horn <max@quendi.de>
1 parent cbfdb3f commit e06a591

File tree

17 files changed

+254
-6
lines changed

17 files changed

+254
-6
lines changed

src/datatype.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,22 @@ JL_DLLEXPORT jl_datatype_t * jl_new_foreign_type(jl_sym_t *name,
826826
return bt;
827827
}
828828

829+
JL_DLLEXPORT int jl_reinit_foreign_type(jl_datatype_t *dt,
830+
jl_markfunc_t markfunc,
831+
jl_sweepfunc_t sweepfunc)
832+
{
833+
if (!jl_is_foreign_type(dt))
834+
return 0;
835+
const jl_datatype_layout_t *layout = dt->layout;
836+
jl_fielddescdyn_t * desc =
837+
(jl_fielddescdyn_t *) ((char *)layout + sizeof(*layout));
838+
assert(!desc->markfunc);
839+
assert(!desc->sweepfunc);
840+
desc->markfunc = markfunc;
841+
desc->sweepfunc = sweepfunc;
842+
return 1;
843+
}
844+
829845
JL_DLLEXPORT int jl_is_foreign_type(jl_datatype_t *dt)
830846
{
831847
return jl_is_datatype(dt) && dt->layout && dt->layout->fielddesc_type == 3;

src/jl_exported_funcs.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@
343343
XX(jl_new_code_info_uninit) \
344344
XX(jl_new_datatype) \
345345
XX(jl_new_foreign_type) \
346+
XX(jl_reinit_foreign_type) \
346347
XX(jl_new_method_instance_uninit) \
347348
XX(jl_new_method_table) \
348349
XX(jl_new_method_uninit) \

src/julia_gcext.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ JL_DLLEXPORT jl_datatype_t *jl_new_foreign_type(
4949
int haspointers,
5050
int large);
5151

52+
53+
#define HAVE_JL_REINIT_FOREIGN_TYPE 1
54+
JL_DLLEXPORT int jl_reinit_foreign_type(
55+
jl_datatype_t *dt,
56+
jl_markfunc_t markfunc,
57+
jl_sweepfunc_t sweepfunc);
58+
5259
JL_DLLEXPORT int jl_is_foreign_type(jl_datatype_t *dt);
5360

5461
JL_DLLEXPORT size_t jl_gc_max_internal_obj_size(void);

src/staticdata.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ External links:
7777

7878
#include "julia.h"
7979
#include "julia_internal.h"
80+
#include "julia_gcext.h"
8081
#include "builtin_proto.h"
8182
#include "processor.h"
8283
#include "serialize.h"
@@ -1249,6 +1250,9 @@ static void jl_write_values(jl_serializer_state *s) JL_GC_DISABLED
12491250
ios_write(s->s, (char*)v, sizeof(void*) + jl_string_len(v));
12501251
write_uint8(s->s, '\0'); // null-terminated strings for easier C-compatibility
12511252
}
1253+
else if (jl_is_foreign_type(t) == 1) {
1254+
jl_error("Cannot serialize instances of foreign datatypes");
1255+
}
12521256
else if (jl_datatype_nfields(t) == 0) {
12531257
// The object has no fields, so we just snapshot its byte representation
12541258
assert(!t->layout->npointers);
@@ -1438,10 +1442,14 @@ static void jl_write_values(jl_serializer_state *s) JL_GC_DISABLED
14381442
if (dt->layout != NULL) {
14391443
size_t nf = dt->layout->nfields;
14401444
size_t np = dt->layout->npointers;
1441-
size_t fieldsize = jl_fielddesc_size(dt->layout->fielddesc_type);
1445+
size_t fieldsize = 0;
1446+
uint8_t is_foreign_type = dt->layout->fielddesc_type == 3;
1447+
if (!is_foreign_type) {
1448+
fieldsize = jl_fielddesc_size(dt->layout->fielddesc_type);
1449+
}
14421450
char *flddesc = (char*)dt->layout;
14431451
size_t fldsize = sizeof(jl_datatype_layout_t) + nf * fieldsize;
1444-
if (dt->layout->first_ptr != -1)
1452+
if (!is_foreign_type && dt->layout->first_ptr != -1)
14451453
fldsize += np << dt->layout->fielddesc_type;
14461454
uintptr_t layout = LLT_ALIGN(ios_pos(s->const_data), sizeof(void*));
14471455
write_padding(s->const_data, layout - ios_pos(s->const_data)); // realign stream
@@ -1450,6 +1458,13 @@ static void jl_write_values(jl_serializer_state *s) JL_GC_DISABLED
14501458
arraylist_push(&s->relocs_list, (void*)(reloc_offset + offsetof(jl_datatype_t, layout))); // relocation location
14511459
arraylist_push(&s->relocs_list, (void*)(((uintptr_t)ConstDataRef << RELOC_TAG_OFFSET) + layout)); // relocation target
14521460
ios_write(s->const_data, flddesc, fldsize);
1461+
if (is_foreign_type) {
1462+
// make sure we have space for the extra hidden pointers
1463+
// zero them since they will need to be re-initialized externally
1464+
assert(fldsize == sizeof(jl_datatype_layout_t));
1465+
jl_fielddescdyn_t dyn = {0, 0};
1466+
ios_write(s->const_data, (char*)&dyn, sizeof(jl_fielddescdyn_t));
1467+
}
14531468
}
14541469
}
14551470
else if (jl_is_typename(v)) {

test/gcext/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/gcext
22
/gcext-debug
3+
/Foreign/deps
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This file is machine-generated - editing it directly is not advised
2+
3+
julia_version = "1.8.3"
4+
manifest_format = "2.0"
5+
project_hash = "e7199d961a5f4ebad68a3deaf5beaa7406a0afcb"
6+
7+
[[deps.Foreign]]
8+
deps = ["Libdl"]
9+
path = "../Foreign"
10+
uuid = "de1f6f7a-d7b3-400f-91c2-33f248ee89c4"
11+
version = "0.1.0"
12+
13+
[[deps.Libdl]]
14+
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name = "DependsOnForeign"
2+
uuid = "4b0716e0-dfb5-4e00-8b44-e2685a41517f"
3+
version = "0.1.0"
4+
5+
[deps]
6+
Foreign = "de1f6f7a-d7b3-400f-91c2-33f248ee89c4"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module DependsOnForeign
2+
3+
using Foreign
4+
5+
f(obj::FObj) = Base.pointer_from_objref(obj)
6+
precompile(f, (FObj,))
7+
8+
const FObjRef = Ref{FObj}()
9+
10+
function __init__()
11+
FObjRef[] = FObj()
12+
end
13+
14+
end # module DependsOnForeign

test/gcext/Foreign/Manifest.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# This file is machine-generated - editing it directly is not advised
2+
3+
julia_version = "1.9.0-DEV"
4+
manifest_format = "2.0"
5+
project_hash = "7b70172a2edbdc772ed789e79d4411d7528eae86"
6+
7+
[[deps.Libdl]]
8+
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

test/gcext/Foreign/Project.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name = "Foreign"
2+
uuid = "de1f6f7a-d7b3-400f-91c2-33f248ee89c4"
3+
version = "0.1.0"
4+
5+
[deps]
6+
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

0 commit comments

Comments
 (0)