@@ -782,14 +782,28 @@ static void jl_write_values(jl_serializer_state *s)
782782#define JL_ARRAY_ALIGN (jl_value , nbytes ) LLT_ALIGN(jl_value, nbytes)
783783 jl_array_t * ar = (jl_array_t * )v ;
784784 jl_value_t * et = jl_tparam0 (jl_typeof (v ));
785+ size_t alen = jl_array_len (ar );
786+ size_t datasize = alen * ar -> elsize ;
787+ size_t tot = datasize ;
788+ int isbitsunion = jl_array_isbitsunion (ar );
789+ if (isbitsunion )
790+ tot += alen ;
791+ else if (ar -> elsize == 1 )
792+ tot += 1 ;
785793 int ndimwords = jl_array_ndimwords (ar -> flags .ndims );
786- size_t tsz = JL_ARRAY_ALIGN ( sizeof (jl_array_t ) + ndimwords * sizeof (size_t ), JL_CACHE_BYTE_ALIGNMENT );
794+ size_t headersize = sizeof (jl_array_t ) + ndimwords * sizeof (size_t );
787795 // copy header
788- ios_write (s -> s , (char * )v , tsz );
796+ ios_write (s -> s , (char * )v , headersize );
797+ size_t alignment_amt = 0 , align16_padding = 0 ;
798+ if (!ar -> flags .ptrarray && ar -> elsize >= 4 ) {
799+ alignment_amt = JL_SMALL_BYTE_ALIGNMENT ;
800+ align16_padding = JL_ARRAY_ALIGN (headersize , JL_SMALL_BYTE_ALIGNMENT ) - headersize ;
801+ }
802+ // use the same cutoff as array.c for stricter alignment
803+ if (headersize + tot + align16_padding > GC_MAX_SZCLASS )
804+ alignment_amt = JL_CACHE_BYTE_ALIGNMENT ;
789805 // make some header modifications in-place
790806 jl_array_t * newa = (jl_array_t * )& s -> s -> buf [reloc_offset ];
791- size_t alen = jl_array_len (ar );
792- size_t tot = alen * ar -> elsize ;
793807 if (newa -> flags .ndims == 1 )
794808 newa -> maxsize = alen ;
795809 newa -> offset = 0 ;
@@ -799,8 +813,9 @@ static void jl_write_values(jl_serializer_state *s)
799813
800814 // write data
801815 if (!ar -> flags .ptrarray && !ar -> flags .hasptr ) {
802- uintptr_t data = LLT_ALIGN (ios_pos (s -> const_data ), 16 );
803- // realign stream to max(data-align(array), sizeof(void*))
816+ if (alignment_amt < 16 )
817+ alignment_amt = 16 ;
818+ uintptr_t data = LLT_ALIGN (ios_pos (s -> const_data ), alignment_amt );
804819 write_padding (s -> const_data , data - ios_pos (s -> const_data ));
805820 // write data and relocations
806821 newa -> data = NULL ; // relocation offset
@@ -815,22 +830,22 @@ static void jl_write_values(jl_serializer_state *s)
815830 write_pointer (s -> const_data );
816831 }
817832 else {
818- int isbitsunion = jl_array_isbitsunion (ar );
819- if (ar -> elsize == 1 && !isbitsunion )
820- tot += 1 ;
821833 ios_write (s -> const_data , (char * )jl_array_data (ar ), tot );
822- if (isbitsunion )
823- ios_write (s -> const_data , jl_array_typetagdata (ar ), alen );
824834 }
825835 }
826836 else {
827- newa -> data = (void * )tsz ; // relocation offset
837+ if (alignment_amt ) {
838+ size_t aligned_hdr_sz = JL_ARRAY_ALIGN (headersize , alignment_amt );
839+ write_padding (s -> s , aligned_hdr_sz - headersize );
840+ headersize = aligned_hdr_sz ;
841+ }
842+ newa -> data = (void * )headersize ; // relocation offset
828843 arraylist_push (& s -> relocs_list , (void * )(reloc_offset + offsetof(jl_array_t , data ))); // relocation location
829844 arraylist_push (& s -> relocs_list , (void * )(((uintptr_t )DataRef << RELOC_TAG_OFFSET ) + item )); // relocation target
830845 if (ar -> flags .hasptr ) {
831846 // copy all of the data first
832847 const char * data = (const char * )jl_array_data (ar );
833- ios_write (s -> s , data , tot );
848+ ios_write (s -> s , data , datasize );
834849 // the rewrite all of the embedded pointers to null+relocation
835850 uint16_t elsz = ar -> elsize ;
836851 size_t j , np = ((jl_datatype_t * )et )-> layout -> npointers ;
@@ -840,12 +855,12 @@ static void jl_write_values(jl_serializer_state *s)
840855 size_t offset = i * elsz + jl_ptr_offset (((jl_datatype_t * )et ), j ) * sizeof (jl_value_t * );
841856 jl_value_t * fld = * (jl_value_t * * )& data [offset ];
842857 if (fld != NULL ) {
843- arraylist_push (& s -> relocs_list , (void * )(uintptr_t )(reloc_offset + tsz + offset )); // relocation location
858+ arraylist_push (& s -> relocs_list , (void * )(uintptr_t )(reloc_offset + headersize + offset )); // relocation location
844859 arraylist_push (& s -> relocs_list , (void * )backref_id (s , fld )); // relocation target
845- memset (& s -> s -> buf [reloc_offset + tsz + offset ], 0 , sizeof (fld )); // relocation offset (none)
860+ memset (& s -> s -> buf [reloc_offset + headersize + offset ], 0 , sizeof (fld )); // relocation offset (none)
846861 }
847862 else {
848- assert (* (jl_value_t * * )& s -> s -> buf [reloc_offset + tsz + offset ] == NULL );
863+ assert (* (jl_value_t * * )& s -> s -> buf [reloc_offset + headersize + offset ] == NULL );
849864 }
850865 }
851866 }
0 commit comments