Skip to content

Commit b1c99bc

Browse files
committed
feat(mem-alloc): modify obj_to_hmu for aligned detection
Convert obj_to_hmu to function that detects aligned allocations via magic value and calculates correct HMU offset.
1 parent ffeb1e5 commit b1c99bc

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

core/shared/mem-alloc/ems/ems_alloc.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,23 @@ 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+
MEM_ALLOC_API_INTER hmu_t *
557+
obj_to_hmu(gc_object_t obj)
558+
{
559+
uint32_t *magic_ptr = (uint32_t *)((char *)obj - 4);
560+
561+
/* Check for aligned allocation magic signature */
562+
if ((*magic_ptr & ALIGNED_ALLOC_MAGIC_MASK) == ALIGNED_ALLOC_MAGIC_VALUE) {
563+
/* This is an aligned allocation, read offset */
564+
uint32_t *offset_ptr = (uint32_t *)((char *)obj - 8);
565+
return (hmu_t *)((char *)obj - *offset_ptr);
566+
}
567+
568+
/* Normal allocation: standard offset */
569+
return (hmu_t *)((gc_uint8 *)(obj) - OBJ_PREFIX_SIZE) - 1;
570+
}
571+
555572
#if BH_ENABLE_GC_VERIFY == 0
556573
gc_object_t
557574
gc_alloc_vo(void *vheap, gc_size_t size)

core/shared/mem-alloc/ems/ems_gc_internal.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ hmu_verify(void *vheap, hmu_t *hmu);
127127
#define HMU_SIZE (sizeof(hmu_t))
128128

129129
#define hmu_to_obj(hmu) (gc_object_t)(SKIP_OBJ_PREFIX((hmu_t *)(hmu) + 1))
130-
#define obj_to_hmu(obj) ((hmu_t *)((gc_uint8 *)(obj)-OBJ_PREFIX_SIZE) - 1)
130+
131+
/* obj_to_hmu function - handles both normal and aligned allocations */
132+
MEM_ALLOC_API_INTER hmu_t *
133+
obj_to_hmu(gc_object_t obj);
131134

132135
#define HMU_UT_SIZE 2
133136
#define HMU_UT_OFFSET 30

0 commit comments

Comments
 (0)