Skip to content

Commit 67c5c32

Browse files
authored
Remove MMTk counted malloc (#24)
This PR changes the implementation of malloc methods for MMTk. We no longer use malloc and counted malloc methods from MMTk for those. Instead, we maintain a global counter for the size, and report the size to MMTk.
1 parent e5fc5dd commit 67c5c32

File tree

2 files changed

+48
-42
lines changed

2 files changed

+48
-42
lines changed

src/gc.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@
3333
extern void maybe_collect(jl_ptls_t ptls);
3434
extern void run_finalizer(jl_task_t *ct, void *o, void *ff);
3535
extern void *jl_malloc_aligned(size_t sz, size_t align);
36+
extern void *jl_realloc_aligned(void *d, size_t sz, size_t oldsz, size_t align);
37+
extern void jl_free_aligned(void *p);
3638
extern void *jl_gc_counted_calloc(size_t nm, size_t sz);
3739
extern void jl_gc_counted_free_with_size(void *p, size_t sz);
3840
extern void *jl_gc_counted_realloc_with_old_size(void *p, size_t old, size_t sz);
39-
extern void *jl_realloc_aligned(void *d, size_t sz, size_t oldsz, size_t align);
4041
extern void jl_gc_add_finalizer_th(jl_ptls_t ptls, jl_value_t *v, jl_function_t *f);
4142
extern void jl_finalize_th(jl_task_t *ct, jl_value_t *o);
4243
extern jl_weakref_t *jl_gc_new_weakref_th(jl_ptls_t ptls, jl_value_t *value);
@@ -47,6 +48,7 @@ extern void gc_premark(jl_ptls_t ptls2);
4748
extern void *gc_managed_realloc_(jl_ptls_t ptls, void *d, size_t sz, size_t oldsz,
4849
int isaligned, jl_value_t *owner, int8_t can_collect);
4950
extern size_t jl_array_nbytes(jl_array_t *a);
51+
extern void run_finalizers(jl_task_t *ct);
5052

5153
#ifdef OBJPROFILE
5254
void objprofile_count(void *ty, int old, int sz) JL_NOTSAFEPOINT;

src/mmtk-gc.c

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -53,31 +53,56 @@ static inline void malloc_maybe_collect(jl_ptls_t ptls, size_t sz)
5353
}
5454
}
5555

56-
5756
// malloc wrappers, aligned allocation
58-
// ---
57+
// We currently just duplicate what Julia GC does. We will in the future replace the malloc calls with MMTK's malloc.
5958

59+
#if defined(_OS_WINDOWS_)
60+
inline void *jl_malloc_aligned(size_t sz, size_t align)
61+
{
62+
return _aligned_malloc(sz ? sz : 1, align);
63+
}
64+
inline void *jl_realloc_aligned(void *p, size_t sz, size_t oldsz,
65+
size_t align)
66+
{
67+
(void)oldsz;
68+
return _aligned_realloc(p, sz ? sz : 1, align);
69+
}
70+
inline void jl_free_aligned(void *p) JL_NOTSAFEPOINT
71+
{
72+
_aligned_free(p);
73+
}
74+
#else
6075
inline void *jl_malloc_aligned(size_t sz, size_t align)
6176
{
62-
return mmtk_malloc_aligned(sz ? sz : 1, align); // XXX sz
77+
#if defined(_P64) || defined(__APPLE__)
78+
if (align <= 16)
79+
return malloc(sz);
80+
#endif
81+
void *ptr;
82+
if (posix_memalign(&ptr, align, sz))
83+
return NULL;
84+
return ptr;
6385
}
6486
inline void *jl_realloc_aligned(void *d, size_t sz, size_t oldsz,
6587
size_t align)
6688
{
67-
void *res = jl_malloc_aligned(sz, align);
68-
if (res != NULL) {
69-
memcpy(res, d, oldsz > sz ? sz : oldsz);
70-
mmtk_free_aligned(d);
89+
#if defined(_P64) || defined(__APPLE__)
90+
if (align <= 16)
91+
return realloc(d, sz);
92+
#endif
93+
void *b = jl_malloc_aligned(sz, align);
94+
if (b != NULL) {
95+
memcpy(b, d, oldsz > sz ? sz : oldsz);
96+
free(d);
7197
}
72-
return res;
98+
return b;
7399
}
74100
inline void jl_free_aligned(void *p) JL_NOTSAFEPOINT
75101
{
76-
mmtk_free_aligned(p);
102+
free(p);
77103
}
104+
#endif
78105

79-
80-
// finalizers
81106
// ---
82107

83108
JL_DLLEXPORT void jl_gc_run_pending_finalizers(jl_task_t *ct)
@@ -195,15 +220,14 @@ void jl_gc_free_array(jl_array_t *a) JL_NOTSAFEPOINT
195220
if (a->flags.how == 2) {
196221
char *d = (char*)a->data - a->offset*a->elsize;
197222
if (a->flags.isaligned)
198-
mmtk_free_aligned(d);
223+
jl_free_aligned(d);
199224
else
200-
mmtk_free(d);
225+
free(d);
201226
gc_num.freed += jl_array_nbytes(a);
202227
gc_num.freecall++;
203228
}
204229
}
205230

206-
207231
// roots
208232
// ---
209233

@@ -384,11 +408,7 @@ JL_DLLEXPORT void *jl_gc_counted_malloc(size_t sz)
384408
if (pgcstack && ct->world_age) {
385409
jl_ptls_t ptls = ct->ptls;
386410
malloc_maybe_collect(ptls, sz);
387-
jl_atomic_store_relaxed(&ptls->gc_num.allocd,
388-
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + sz);
389-
jl_atomic_store_relaxed(&ptls->gc_num.malloc,
390-
jl_atomic_load_relaxed(&ptls->gc_num.malloc) + 1);
391-
return mmtk_counted_malloc(sz);
411+
jl_atomic_fetch_add_relaxed(&JULIA_MALLOC_BYTES, sz);
392412
}
393413
return malloc(sz);
394414
}
@@ -399,12 +419,8 @@ JL_DLLEXPORT void *jl_gc_counted_calloc(size_t nm, size_t sz)
399419
jl_task_t *ct = jl_current_task;
400420
if (pgcstack && ct->world_age) {
401421
jl_ptls_t ptls = ct->ptls;
402-
malloc_maybe_collect(ptls, sz);
403-
jl_atomic_store_relaxed(&ptls->gc_num.allocd,
404-
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + nm*sz);
405-
jl_atomic_store_relaxed(&ptls->gc_num.malloc,
406-
jl_atomic_load_relaxed(&ptls->gc_num.malloc) + 1);
407-
return mmtk_counted_calloc(nm, sz);
422+
malloc_maybe_collect(ptls, nm * sz);
423+
jl_atomic_fetch_add_relaxed(&JULIA_MALLOC_BYTES, nm * sz);
408424
}
409425
return calloc(nm, sz);
410426
}
@@ -413,16 +429,10 @@ JL_DLLEXPORT void jl_gc_counted_free_with_size(void *p, size_t sz)
413429
{
414430
jl_gcframe_t **pgcstack = jl_get_pgcstack();
415431
jl_task_t *ct = jl_current_task;
432+
free(p);
416433
if (pgcstack && ct->world_age) {
417-
jl_ptls_t ptls = ct->ptls;
418-
jl_atomic_store_relaxed(&ptls->gc_num.freed,
419-
jl_atomic_load_relaxed(&ptls->gc_num.freed) + sz);
420-
jl_atomic_store_relaxed(&ptls->gc_num.freecall,
421-
jl_atomic_load_relaxed(&ptls->gc_num.freecall) + 1);
422-
mmtk_free_with_size(p, sz);
423-
return;
434+
jl_atomic_fetch_add_relaxed(&JULIA_MALLOC_BYTES, -sz);
424435
}
425-
free(p);
426436
}
427437

428438
JL_DLLEXPORT void *jl_gc_counted_realloc_with_old_size(void *p, size_t old, size_t sz)
@@ -433,16 +443,10 @@ JL_DLLEXPORT void *jl_gc_counted_realloc_with_old_size(void *p, size_t old, size
433443
jl_ptls_t ptls = ct->ptls;
434444
malloc_maybe_collect(ptls, sz);
435445
if (sz < old)
436-
jl_atomic_store_relaxed(&ptls->gc_num.freed,
437-
jl_atomic_load_relaxed(&ptls->gc_num.freed) + (old - sz));
446+
jl_atomic_fetch_add_relaxed(&JULIA_MALLOC_BYTES, old - sz);
438447
else
439-
jl_atomic_store_relaxed(&ptls->gc_num.allocd,
440-
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + (sz - old));
441-
jl_atomic_store_relaxed(&ptls->gc_num.realloc,
442-
jl_atomic_load_relaxed(&ptls->gc_num.realloc) + 1);
443-
return mmtk_realloc_with_old_size(p, sz, old);
448+
jl_atomic_fetch_add_relaxed(&JULIA_MALLOC_BYTES, sz - old);
444449
}
445-
// TODO: correct?
446450
return realloc(p, sz);
447451
}
448452

0 commit comments

Comments
 (0)