Skip to content

Commit 6f992ab

Browse files
authored
Refactoring code in mk_symbol to remove stock specific code (JuliaLang#57214)
This PR lifts up some code that uses specific information about the stock GC (`GC_OLD_MARKED` in the header bits). MMTk sets the log bits for immortal objects in a side table, but this requires that they are allocated with `jl_gc_permobj` instead of `jl_gc_perm_alloc`, since the latter allocates a memory slot and not necessarily an object.
1 parent f0446c6 commit 6f992ab

File tree

10 files changed

+35
-28
lines changed

10 files changed

+35
-28
lines changed

src/datatype.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ STATIC_INLINE void jl_maybe_allocate_singleton_instance(jl_datatype_t *st) JL_NO
336336
if (st->instance)
337337
return;
338338
if (jl_is_datatype_make_singleton(st)) {
339-
st->instance = jl_gc_permobj(0, st);
339+
st->instance = jl_gc_permobj(0, st, 0);
340340
}
341341
}
342342

@@ -575,7 +575,7 @@ void jl_get_genericmemory_layout(jl_datatype_t *st)
575575

576576
if (jl_is_addrspacecore(addrspace) && jl_unbox_uint8(addrspace) == 0) {
577577
if (kind == (jl_value_t*)jl_not_atomic_sym || kind == (jl_value_t*)jl_atomic_sym) {
578-
jl_genericmemory_t *zeroinst = (jl_genericmemory_t*)jl_gc_permobj(LLT_ALIGN(sizeof(jl_genericmemory_t), JL_SMALL_BYTE_ALIGNMENT) + (elsz ? elsz : isunion), st);
578+
jl_genericmemory_t *zeroinst = (jl_genericmemory_t*)jl_gc_permobj(LLT_ALIGN(sizeof(jl_genericmemory_t), JL_SMALL_BYTE_ALIGNMENT) + (elsz ? elsz : isunion), st, 0);
579579
zeroinst->length = 0;
580580
zeroinst->ptr = (char*)zeroinst + JL_SMALL_BYTE_ALIGNMENT;
581581
memset(zeroinst->ptr, 0, elsz ? elsz : isunion);
@@ -1385,13 +1385,13 @@ JL_DLLEXPORT int jl_atomic_storeonce_bits(jl_datatype_t *dt, char *dst, const jl
13851385
return success;
13861386
}
13871387

1388-
#define PERMBOXN_FUNC(nb) \
1388+
#define PERMBOXN_FUNC(nb) \
13891389
jl_value_t *jl_permbox##nb(jl_datatype_t *t, uintptr_t tag, uint##nb##_t x) \
1390-
{ /* n.b. t must be a concrete isbits datatype of the right size */ \
1391-
jl_value_t *v = jl_gc_permobj(LLT_ALIGN(nb, sizeof(void*)), t); \
1392-
if (tag) jl_set_typetagof(v, tag, GC_OLD_MARKED); \
1393-
*(uint##nb##_t*)jl_data_ptr(v) = x; \
1394-
return v; \
1390+
{ /* n.b. t must be a concrete isbits datatype of the right size */ \
1391+
jl_value_t *v = jl_gc_permobj(LLT_ALIGN(nb, sizeof(void*)), t, 0); \
1392+
if (tag) jl_set_typetagof(v, tag, GC_OLD_MARKED); \
1393+
*(uint##nb##_t*)jl_data_ptr(v) = x; \
1394+
return v; \
13951395
}
13961396
PERMBOXN_FUNC(8)
13971397
PERMBOXN_FUNC(16)

src/gc-interface.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,15 @@ JL_DLLEXPORT void *jl_gc_perm_alloc(size_t sz, int zero, unsigned align,
201201
// object header must be included in the object size. This object is allocated in an
202202
// immortal region that is never swept. The second parameter specifies the type of the
203203
// object being allocated and will be used to set the object header.
204+
// If the value passed as alignment is 0, then the result will be aligned according to the object
205+
// size: if sz is 0 it will be aligned to pointer size, to 2x pointer size if sz < 2*sizeof(void*),
206+
// or to 16 otherwise.
204207
//
205208
// !!! warning: Because permanently allocated objects are not swept, the GC will not
206209
// necessarily mark any objects that would have ordinarily been rooted by
207210
// the allocated object. All objects stored in fields of this object
208211
// must be either permanently allocated or have other roots.
209-
struct _jl_value_t *jl_gc_permobj(size_t sz, void *ty) JL_NOTSAFEPOINT;
212+
struct _jl_value_t *jl_gc_permobj(size_t sz, void *ty, unsigned align) JL_NOTSAFEPOINT;
210213
// This function notifies the GC about memory addresses that are set when loading the boot image.
211214
// The GC may use that information to, for instance, determine that such objects should
212215
// be treated as marked and belonged to the old generation in nursery collections.

src/gc-mmtk.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,11 +1028,13 @@ void *jl_gc_perm_alloc(size_t sz, int zero, unsigned align, unsigned offset)
10281028
return jl_gc_perm_alloc_nolock(sz, zero, align, offset);
10291029
}
10301030

1031-
jl_value_t *jl_gc_permobj(size_t sz, void *ty) JL_NOTSAFEPOINT
1031+
jl_value_t *jl_gc_permobj(size_t sz, void *ty, unsigned align) JL_NOTSAFEPOINT
10321032
{
10331033
const size_t allocsz = sz + sizeof(jl_taggedvalue_t);
1034-
unsigned align = (sz == 0 ? sizeof(void*) : (allocsz <= sizeof(void*) * 2 ?
1034+
if (align == 0) {
1035+
align = ((sz == 0) ? sizeof(void*) : (allocsz <= sizeof(void*) * 2 ?
10351036
sizeof(void*) * 2 : 16));
1037+
}
10361038
jl_taggedvalue_t *o = (jl_taggedvalue_t*)jl_gc_perm_alloc(allocsz, 0, align,
10371039
sizeof(void*) % align);
10381040

src/gc-stock.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3931,16 +3931,18 @@ void *jl_gc_perm_alloc(size_t sz, int zero, unsigned align, unsigned offset)
39313931
return p;
39323932
}
39333933

3934-
jl_value_t *jl_gc_permobj(size_t sz, void *ty) JL_NOTSAFEPOINT
3934+
jl_value_t *jl_gc_permobj(size_t sz, void *ty, unsigned align) JL_NOTSAFEPOINT
39353935
{
39363936
const size_t allocsz = sz + sizeof(jl_taggedvalue_t);
3937-
unsigned align = (sz == 0 ? sizeof(void*) : (allocsz <= sizeof(void*) * 2 ?
3937+
if (align == 0) {
3938+
align = ((sz == 0) ? sizeof(void*) : (allocsz <= sizeof(void*) * 2 ?
39383939
sizeof(void*) * 2 : 16));
3940+
}
39393941
jl_taggedvalue_t *o = (jl_taggedvalue_t*)jl_gc_perm_alloc(allocsz, 0, align,
39403942
sizeof(void*) % align);
3941-
uintptr_t tag = (uintptr_t)ty;
3942-
o->header = tag | GC_OLD_MARKED;
3943-
return jl_valueof(o);
3943+
jl_value_t* v = jl_valueof(o);
3944+
jl_set_typeof(v, (void*)(((uintptr_t)(ty) | GC_OLD_MARKED)));
3945+
return v;
39443946
}
39453947

39463948
JL_DLLEXPORT int jl_gc_enable_conservative_gc_support(void)

src/jltypes.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2956,7 +2956,7 @@ void jl_init_types(void) JL_GC_DISABLED
29562956
XX(simplevector);
29572957
jl_methtable_type = jl_new_uninitialized_datatype();
29582958

2959-
jl_emptysvec = (jl_svec_t*)jl_gc_permobj(sizeof(void*), jl_simplevector_type);
2959+
jl_emptysvec = (jl_svec_t*)jl_gc_permobj(sizeof(void*), jl_simplevector_type, 0);
29602960
jl_set_typetagof(jl_emptysvec, jl_simplevector_tag, GC_OLD_MARKED);
29612961
jl_svec_set_len_unsafe(jl_emptysvec, 0);
29622962

@@ -3088,7 +3088,7 @@ void jl_init_types(void) JL_GC_DISABLED
30883088
jl_typeofbottom_type = jl_new_datatype(jl_symbol("TypeofBottom"), core, type_type, jl_emptysvec,
30893089
jl_emptysvec, jl_emptysvec, jl_emptysvec, 0, 0, 0);
30903090
XX(typeofbottom);
3091-
jl_bottom_type = jl_gc_permobj(0, jl_typeofbottom_type);
3091+
jl_bottom_type = jl_gc_permobj(0, jl_typeofbottom_type, 0);
30923092
jl_set_typetagof(jl_bottom_type, jl_typeofbottom_tag, GC_OLD_MARKED);
30933093
jl_typeofbottom_type->instance = jl_bottom_type;
30943094

@@ -3138,7 +3138,7 @@ void jl_init_types(void) JL_GC_DISABLED
31383138
jl_typeofbottom_type->super = jl_wrap_Type(jl_bottom_type);
31393139
jl_typeofbottom_type->super->layout = jl_typeofbottom_type->layout; // the only abstract type with a layout
31403140
jl_emptytuple_type = (jl_datatype_t*)jl_apply_tuple_type(jl_emptysvec, 0);
3141-
jl_emptytuple = jl_gc_permobj(0, jl_emptytuple_type);
3141+
jl_emptytuple = jl_gc_permobj(0, jl_emptytuple_type, 0);
31423142
jl_emptytuple_type->instance = jl_emptytuple;
31433143

31443144
// non-primitive definitions follow

src/runtime_ccall.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ jl_value_t *jl_get_cfunction_trampoline(
295295
permanent = true;
296296
}
297297
if (permanent) {
298-
result = jl_gc_permobj(sizeof(jl_taggedvalue_t) + jl_datatype_size(result_type), result_type);
298+
result = jl_gc_permobj(sizeof(jl_taggedvalue_t) + jl_datatype_size(result_type), result_type, 0);
299299
memset(result, 0, jl_datatype_size(result_type));
300300
}
301301
else {

src/simplevector.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ JL_DLLEXPORT jl_svec_t *(ijl_svec)(size_t n, ...)
2222
jl_svec_t *(jl_perm_symsvec)(size_t n, ...)
2323
{
2424
if (n == 0) return jl_emptysvec;
25-
jl_svec_t *jv = (jl_svec_t*)jl_gc_permobj((n + 1) * sizeof(void*), jl_simplevector_type);
25+
jl_svec_t *jv = (jl_svec_t*)jl_gc_permobj((n + 1) * sizeof(void*), jl_simplevector_type, 0);
2626
jl_set_typetagof(jv, jl_simplevector_tag, jl_astaggedvalue(jv)->bits.gc);
2727
jl_svec_set_len_unsafe(jv, n);
2828
va_list args;

src/staticdata.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3832,7 +3832,7 @@ static void jl_restore_system_image_from_stream_(ios_t *f, jl_image_t *image, jl
38323832
// and we overwrite the name field (field 0) now so preserve it too
38333833
if (dt->instance) {
38343834
if (dt->instance == jl_nothing)
3835-
dt->instance = jl_gc_permobj(0, newdt);
3835+
dt->instance = jl_gc_permobj(0, newdt, 0);
38363836
newdt->instance = dt->instance;
38373837
}
38383838
static_assert(offsetof(jl_datatype_t, name) == 0, "");

src/symbol.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ static uintptr_t hash_symbol(const char *str, size_t len) JL_NOTSAFEPOINT
2929

3030
static size_t symbol_nbytes(size_t len) JL_NOTSAFEPOINT
3131
{
32-
return (sizeof(jl_taggedvalue_t) + sizeof(jl_sym_t) + len + 1 + 7) & -8;
32+
return ((sizeof(jl_sym_t) + len + 1 + 7) & -8);
3333
}
3434

3535
static jl_sym_t *mk_symbol(const char *str, size_t len) JL_NOTSAFEPOINT
3636
{
37-
jl_sym_t *sym;
3837
size_t nb = symbol_nbytes(len);
39-
jl_taggedvalue_t *tag = (jl_taggedvalue_t*)jl_gc_perm_alloc(nb, 0, sizeof(void*), 0);
40-
sym = (jl_sym_t*)jl_valueof(tag);
41-
// set to old marked so that we won't look at it in the GC or write barrier.
38+
// jl_sym_t is an object and needs to be allocated with jl_gc_permobj
39+
// but its type is set below with jl_set_typetagof since
40+
// jl_symbol_type might not have been initialized
41+
jl_sym_t *sym = (jl_sym_t*)jl_gc_permobj(nb, NULL, sizeof(void*));
4242
jl_set_typetagof(sym, jl_symbol_tag, GC_OLD_MARKED);
4343
jl_atomic_store_relaxed(&sym->left, NULL);
4444
jl_atomic_store_relaxed(&sym->right, NULL);

src/task.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,7 @@ jl_task_t *jl_init_root_task(jl_ptls_t ptls, void *stack_lo, void *stack_hi)
15351535
jl_set_pgcstack(&bootstrap_task.value.gcstack);
15361536
bootstrap_task.value.ptls = ptls;
15371537
if (jl_nothing == NULL) // make a placeholder
1538-
jl_nothing = jl_gc_permobj(0, jl_nothing_type);
1538+
jl_nothing = jl_gc_permobj(0, jl_nothing_type, 0);
15391539
jl_task_t *ct = (jl_task_t*)jl_gc_alloc(ptls, sizeof(jl_task_t), jl_task_type);
15401540
jl_set_typetagof(ct, jl_task_tag, 0);
15411541
memset(ct, 0, sizeof(jl_task_t));

0 commit comments

Comments
 (0)