Skip to content

Commit 1ad2fad

Browse files
committed
feat(mem-alloc): enhance aligned allocation metadata and documentation
1 parent e3aa5e0 commit 1ad2fad

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

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

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ hmu_verify(void *vheap, hmu_t *hmu);
9292

9393
/* Minimum alignment for allocations */
9494
#ifndef GC_MIN_ALIGNMENT
95-
#define GC_MIN_ALIGNMENT 8
95+
#define GC_MIN_ALIGNMENT 8
9696
#endif
9797

9898
#define GC_SMALLEST_SIZE \
@@ -101,9 +101,22 @@ hmu_verify(void *vheap, hmu_t *hmu);
101101
GC_ALIGN_8(HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE \
102102
+ (((x) > 8) ? (x) : 8))
103103

104-
/* Magic value for aligned allocation detection */
105-
#define ALIGNED_ALLOC_MAGIC_MASK 0xFFFF0000
106-
#define ALIGNED_ALLOC_MAGIC_VALUE 0xA11C0000
104+
/*
105+
* Aligned allocation uses metadata in the header to store the offset
106+
*
107+
* ### Memory Layout
108+
*
109+
* Aligned allocations use over-allocation with metadata storage:
110+
*
111+
* ```
112+
* [HMU][PREFIX][...padding...][METADATA][ALIGNED_OBJ][SUFFIX]
113+
* ^8 bytes ^returned pointer (aligned)
114+
* ```
115+
*
116+
* Magic value for aligned allocation detection
117+
*/
118+
#define ALIGNED_ALLOC_MAGIC_MASK 0xFFFF0000
119+
#define ALIGNED_ALLOC_MAGIC_VALUE 0xA11C0000
107120

108121
/**
109122
* hmu bit operation
@@ -125,9 +138,49 @@ hmu_verify(void *vheap, hmu_t *hmu);
125138
(((v) & (((((uint32)1 << size) - 1) << offset))) >> offset)
126139
/* clang-format on */
127140

141+
/* clang-format off */
128142
/**
129143
* gc object layout definition
144+
*
145+
* #### Header Bit Layout
146+
*
147+
* ```
148+
* 31 30 29 28 27 0
149+
* ┌──┬──┬──┬──┬───────────────────────────────────────────────────┐
150+
* │UT│UT│ P│ *│ Size or Type-Specific Data │
151+
* └──┴──┴──┴──┴───────────────────────────────────────────────────┘
152+
* ```
153+
*
154+
* #### Bit Fields Breakdown
155+
*
156+
* | Bits | Field | Description |
157+
* | --------- | ----------------------- | -------------------------------------------- |
158+
* | **31-30** | **UT** (Usage Type) | 2 bits for chunk type |
159+
* | **29** | **P** (Previous In Use) | 1 bit indicating if previous chunk is in use |
160+
* | **28** | **Type-specific** | Meaning depends on UT field |
161+
* | **27-0** | **Type-specific** | Size or other data depending on UT |
162+
*
163+
* #### Memory Layout in Heap
164+
*
165+
* ```
166+
* ┌─────────────────────────────────────────────────────────────┐
167+
* │ HMU Header (4 bytes) │
168+
* ├─────────────────────────────────────────────────────────────┤
169+
* │ OBJ_PREFIX (if BH_ENABLE_GC_VERIFY) │
170+
* │ - file_name pointer │
171+
* │ - line_no │
172+
* │ - size │
173+
* │ - padding values (for corruption detection) │
174+
* ├─────────────────────────────────────────────────────────────┤
175+
* │ User Data (aligned to 8 bytes) │
176+
* │ ... │
177+
* ├─────────────────────────────────────────────────────────────┤
178+
* │ OBJ_SUFFIX (if BH_ENABLE_GC_VERIFY) │
179+
* │ - padding values (for corruption detection) │
180+
* └─────────────────────────────────────────────────────────────┘
181+
* ```
130182
*/
183+
/* clang-format on */
131184

132185
#define HMU_SIZE (sizeof(hmu_t))
133186

0 commit comments

Comments
 (0)