@@ -552,6 +552,21 @@ alloc_hmu_ex(gc_heap_t *heap, gc_size_t size)
552552 return alloc_hmu (heap , size );
553553}
554554
555+ /* Convert object pointer to HMU pointer - handles aligned allocations */
556+ hmu_t *
557+ obj_to_hmu (gc_object_t obj )
558+ {
559+ /* Check for aligned allocation magic signature */
560+ if (gc_is_aligned_allocation (obj )) {
561+ /* This is an aligned allocation, read offset */
562+ uint32_t * offset_ptr = (uint32_t * )((char * )obj - 8 );
563+ return (hmu_t * )((char * )obj - * offset_ptr );
564+ }
565+
566+ /* Normal allocation: standard offset */
567+ return (hmu_t * )((gc_uint8 * )(obj )- OBJ_PREFIX_SIZE ) - 1 ;
568+ }
569+
555570#if BH_ENABLE_GC_VERIFY == 0
556571gc_object_t
557572gc_alloc_vo (void * vheap , gc_size_t size )
@@ -612,6 +627,124 @@ gc_alloc_vo_internal(void *vheap, gc_size_t size, const char *file, int line)
612627 return ret ;
613628}
614629
630+ #if BH_ENABLE_GC_VERIFY == 0
631+ gc_object_t
632+ gc_alloc_vo_aligned (void * vheap , gc_size_t size , gc_size_t alignment )
633+ #else
634+ gc_object_t
635+ gc_alloc_vo_aligned_internal (void * vheap , gc_size_t size , gc_size_t alignment ,
636+ const char * file , int line )
637+ #endif
638+ {
639+ gc_heap_t * heap = (gc_heap_t * )vheap ;
640+ hmu_t * hmu = NULL ;
641+ gc_object_t ret = NULL ;
642+ gc_size_t tot_size , tot_size_unaligned ;
643+ gc_uint8 * base_obj ;
644+ uintptr_t aligned_addr ;
645+ uint32_t offset , alignment_log2 ;
646+ uint32_t max_alignment ;
647+
648+ /* Get system page size for maximum alignment check */
649+ max_alignment = (uint32_t )os_getpagesize ();
650+
651+ /* Validation */
652+ if (alignment == 0 || (alignment & (alignment - 1 )) != 0 ) {
653+ /* Zero or not power of 2 */
654+ return NULL ;
655+ }
656+
657+ if (alignment < GC_MIN_ALIGNMENT ) {
658+ alignment = GC_MIN_ALIGNMENT ;
659+ }
660+
661+ if (alignment > max_alignment ) {
662+ /* Exceeds page size */
663+ return NULL ;
664+ }
665+
666+ if (size % alignment != 0 ) {
667+ /* POSIX requirement: size must be multiple of alignment */
668+ return NULL ;
669+ }
670+
671+ if (size > SIZE_MAX - alignment - HMU_SIZE - OBJ_PREFIX_SIZE
672+ - OBJ_SUFFIX_SIZE - 8 ) {
673+ /* Would overflow */
674+ return NULL ;
675+ }
676+
677+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
678+ if (heap -> is_heap_corrupted ) {
679+ LOG_ERROR ("[GC_ERROR]Heap is corrupted, allocate memory failed.\n" );
680+ return NULL ;
681+ }
682+ #endif
683+
684+ /* Calculate total size needed */
685+ tot_size_unaligned =
686+ HMU_SIZE + OBJ_PREFIX_SIZE + size + OBJ_SUFFIX_SIZE + alignment - 1 + 8 ;
687+ tot_size = GC_ALIGN_8 (tot_size_unaligned );
688+
689+ if (tot_size < size ) {
690+ /* Integer overflow */
691+ return NULL ;
692+ }
693+
694+ LOCK_HEAP (heap );
695+
696+ hmu = alloc_hmu_ex (heap , tot_size );
697+ if (!hmu )
698+ goto finish ;
699+
700+ bh_assert (hmu_get_size (hmu ) >= tot_size );
701+ tot_size = hmu_get_size (hmu );
702+
703+ #if GC_STAT_DATA != 0
704+ heap -> total_size_allocated += tot_size ;
705+ #endif
706+
707+ /* Get base object pointer */
708+ base_obj = (gc_uint8 * )hmu + HMU_SIZE + OBJ_PREFIX_SIZE ;
709+
710+ /* Find next aligned address, leaving 8 bytes for metadata */
711+ aligned_addr = (((uintptr_t )base_obj + 8 + alignment - 1 )
712+ & ~(uintptr_t )(alignment - 1 ));
713+ ret = (gc_object_t )aligned_addr ;
714+
715+ /* Verify we have enough space */
716+ bh_assert ((gc_uint8 * )ret + size + OBJ_SUFFIX_SIZE
717+ <= (gc_uint8 * )hmu + tot_size );
718+
719+ /* Calculate offset from HMU to returned pointer */
720+ offset = (uint32_t )((char * )ret - (char * )hmu );
721+
722+ /* Calculate log2 of alignment for magic value */
723+ alignment_log2 = 0 ;
724+ while ((1U << alignment_log2 ) < alignment ) {
725+ alignment_log2 ++ ;
726+ }
727+
728+ /* Store offset 8 bytes before returned pointer */
729+ * ((uint32_t * )((char * )ret - 8 )) = offset ;
730+
731+ /* Store magic with encoded alignment */
732+ * ((uint32_t * )((char * )ret - 4 )) =
733+ ALIGNED_ALLOC_MAGIC_VALUE | alignment_log2 ;
734+
735+ /* Initialize HMU */
736+ hmu_set_ut (hmu , HMU_VO );
737+ hmu_unfree_vo (hmu );
738+
739+ #if BH_ENABLE_GC_VERIFY != 0
740+ hmu_init_prefix_and_suffix (hmu , tot_size , file , line );
741+ #endif
742+
743+ finish :
744+ UNLOCK_HEAP (heap );
745+ return ret ;
746+ }
747+
615748#if BH_ENABLE_GC_VERIFY == 0
616749gc_object_t
617750gc_realloc_vo (void * vheap , void * ptr , gc_size_t size )
@@ -644,6 +777,13 @@ gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size, const char *file,
644777 }
645778#endif
646779
780+ /* Check if this is an aligned allocation - not supported */
781+ if (gc_is_aligned_allocation (obj_old )) {
782+ LOG_ERROR ("[GC_ERROR]gc_realloc_vo does not support aligned "
783+ "allocations\n" );
784+ return NULL ;
785+ }
786+
647787 if (obj_old ) {
648788 hmu_old = obj_to_hmu (obj_old );
649789 tot_size_old = hmu_get_size (hmu_old );
0 commit comments