@@ -3639,7 +3639,8 @@ JL_DLLEXPORT void *jl_gc_counted_malloc(size_t sz)
3639
3639
{
3640
3640
jl_gcframe_t * * pgcstack = jl_get_pgcstack ();
3641
3641
jl_task_t * ct = jl_current_task ;
3642
- if (pgcstack != NULL && ct -> world_age ) {
3642
+ void * data = malloc (sz );
3643
+ if (data != NULL && pgcstack != NULL && ct -> world_age ) {
3643
3644
jl_ptls_t ptls = ct -> ptls ;
3644
3645
maybe_collect (ptls );
3645
3646
jl_atomic_store_relaxed (& ptls -> gc_num .allocd ,
@@ -3654,14 +3655,15 @@ JL_DLLEXPORT void *jl_gc_counted_malloc(size_t sz)
3654
3655
jl_atomic_store_relaxed (& ptls -> gc_num .alloc_acc , 0 );
3655
3656
}
3656
3657
}
3657
- return malloc ( sz ) ;
3658
+ return data ;
3658
3659
}
3659
3660
3660
3661
JL_DLLEXPORT void * jl_gc_counted_calloc (size_t nm , size_t sz )
3661
3662
{
3662
3663
jl_gcframe_t * * pgcstack = jl_get_pgcstack ();
3663
3664
jl_task_t * ct = jl_current_task ;
3664
- if (pgcstack != NULL && ct -> world_age ) {
3665
+ void * data = calloc (nm , sz );
3666
+ if (data != NULL && pgcstack != NULL && ct -> world_age ) {
3665
3667
jl_ptls_t ptls = ct -> ptls ;
3666
3668
maybe_collect (ptls );
3667
3669
jl_atomic_store_relaxed (& ptls -> gc_num .allocd ,
@@ -3676,7 +3678,7 @@ JL_DLLEXPORT void *jl_gc_counted_calloc(size_t nm, size_t sz)
3676
3678
jl_atomic_store_relaxed (& ptls -> gc_num .alloc_acc , 0 );
3677
3679
}
3678
3680
}
3679
- return calloc ( nm , sz ) ;
3681
+ return data ;
3680
3682
}
3681
3683
3682
3684
JL_DLLEXPORT void jl_gc_counted_free_with_size (void * p , size_t sz )
@@ -3700,7 +3702,8 @@ JL_DLLEXPORT void *jl_gc_counted_realloc_with_old_size(void *p, size_t old, size
3700
3702
{
3701
3703
jl_gcframe_t * * pgcstack = jl_get_pgcstack ();
3702
3704
jl_task_t * ct = jl_current_task ;
3703
- if (pgcstack != NULL && ct -> world_age ) {
3705
+ void * data = realloc (p , sz );
3706
+ if (data != NULL && pgcstack != NULL && ct -> world_age ) {
3704
3707
jl_ptls_t ptls = ct -> ptls ;
3705
3708
maybe_collect (ptls );
3706
3709
if (!(sz < old ))
@@ -3730,7 +3733,7 @@ JL_DLLEXPORT void *jl_gc_counted_realloc_with_old_size(void *p, size_t old, size
3730
3733
}
3731
3734
}
3732
3735
}
3733
- return realloc ( p , sz ) ;
3736
+ return data ;
3734
3737
}
3735
3738
3736
3739
// allocation wrappers that save the size of allocations, to allow using
@@ -3799,6 +3802,15 @@ JL_DLLEXPORT void *jl_gc_managed_malloc(size_t sz)
3799
3802
size_t allocsz = LLT_ALIGN (sz , JL_CACHE_BYTE_ALIGNMENT );
3800
3803
if (allocsz < sz ) // overflow in adding offs, size was "negative"
3801
3804
jl_throw (jl_memory_exception );
3805
+
3806
+ int last_errno = errno ;
3807
+ #ifdef _OS_WINDOWS_
3808
+ DWORD last_error = GetLastError ();
3809
+ #endif
3810
+ void * b = malloc_cache_align (allocsz );
3811
+ if (b == NULL )
3812
+ jl_throw (jl_memory_exception );
3813
+
3802
3814
jl_atomic_store_relaxed (& ptls -> gc_num .allocd ,
3803
3815
jl_atomic_load_relaxed (& ptls -> gc_num .allocd ) + allocsz );
3804
3816
jl_atomic_store_relaxed (& ptls -> gc_num .malloc ,
@@ -3810,13 +3822,6 @@ JL_DLLEXPORT void *jl_gc_managed_malloc(size_t sz)
3810
3822
jl_atomic_fetch_add_relaxed (& gc_heap_stats .heap_size , alloc_acc + allocsz );
3811
3823
jl_atomic_store_relaxed (& ptls -> gc_num .alloc_acc , 0 );
3812
3824
}
3813
- int last_errno = errno ;
3814
- #ifdef _OS_WINDOWS_
3815
- DWORD last_error = GetLastError ();
3816
- #endif
3817
- void * b = malloc_cache_align (allocsz );
3818
- if (b == NULL )
3819
- jl_throw (jl_memory_exception );
3820
3825
#ifdef _OS_WINDOWS_
3821
3826
SetLastError (last_error );
3822
3827
#endif
@@ -3831,12 +3836,28 @@ static void *gc_managed_realloc_(jl_ptls_t ptls, void *d, size_t sz, size_t olds
3831
3836
{
3832
3837
if (can_collect )
3833
3838
maybe_collect (ptls );
3834
-
3839
+ int is_old_marked = jl_astaggedvalue ( owner ) -> bits . gc == GC_OLD_MARKED ;
3835
3840
size_t allocsz = LLT_ALIGN (sz , JL_CACHE_BYTE_ALIGNMENT );
3836
3841
if (allocsz < sz ) // overflow in adding offs, size was "negative"
3837
3842
jl_throw (jl_memory_exception );
3838
3843
3839
- if (jl_astaggedvalue (owner )-> bits .gc == GC_OLD_MARKED ) {
3844
+ int last_errno = errno ;
3845
+ #ifdef _OS_WINDOWS_
3846
+ DWORD last_error = GetLastError ();
3847
+ #endif
3848
+ void * b ;
3849
+ if (isaligned )
3850
+ b = realloc_cache_align (d , allocsz , oldsz );
3851
+ else
3852
+ b = realloc (d , allocsz );
3853
+ if (b == NULL )
3854
+ jl_throw (jl_memory_exception );
3855
+ #ifdef _OS_WINDOWS_
3856
+ SetLastError (last_error );
3857
+ #endif
3858
+ errno = last_errno ;
3859
+ // gc_managed_realloc_ is currently used exclusively for resizing array buffers.
3860
+ if (is_old_marked ) {
3840
3861
ptls -> gc_cache .perm_scanned_bytes += allocsz - oldsz ;
3841
3862
inc_live_bytes (allocsz - oldsz );
3842
3863
}
@@ -3867,21 +3888,6 @@ static void *gc_managed_realloc_(jl_ptls_t ptls, void *d, size_t sz, size_t olds
3867
3888
}
3868
3889
}
3869
3890
3870
- int last_errno = errno ;
3871
- #ifdef _OS_WINDOWS_
3872
- DWORD last_error = GetLastError ();
3873
- #endif
3874
- void * b ;
3875
- if (isaligned )
3876
- b = realloc_cache_align (d , allocsz , oldsz );
3877
- else
3878
- b = realloc (d , allocsz );
3879
- if (b == NULL )
3880
- jl_throw (jl_memory_exception );
3881
- #ifdef _OS_WINDOWS_
3882
- SetLastError (last_error );
3883
- #endif
3884
- errno = last_errno ;
3885
3891
maybe_record_alloc_to_profile ((jl_value_t * )b , sz , jl_gc_unknown_type_tag );
3886
3892
return b ;
3887
3893
}
0 commit comments