Commit a02ad8b
refactor(mem-alloc): improve aligned allocation design and documentation
This commit addresses several design improvements for aligned allocation:
1. **Size=0 behavior consistency**:
- wasm_runtime_aligned_alloc(0, N) now allocates N bytes (smallest valid size)
- Matches wasm_runtime_malloc(0) behavior which allocates 1 byte
- Ensures size is always a multiple of alignment
2. **Helper function for alignment detection**:
- Add gc_is_aligned_allocation() inline helper in ems_gc_internal.h
- Encapsulates magic marker check logic (DRY principle)
- Used in obj_to_hmu() and gc_realloc_vo_internal()
- Single source of truth for alignment detection
3. **Global API visibility macro**:
- Add WASM_RUNTIME_API_INTERN to bh_platform.h
- Centralizes internal API visibility control
- MEM_ALLOC_API_INTER now uses WASM_RUNTIME_API_INTERN
- Consistent with WASM_RUNTIME_API_EXTERN pattern
4. **Comprehensive documentation**:
- Replace brief comment with 90+ line documentation block
- Explain POSIX aligned_alloc() specification (C11 §7.22.3.1)
- Describe WAMR implementation strategy in detail:
* Validation, over-allocation, alignment adjustment
* Magic marker storage (0xA11C0000 | offset)
* Realloc prevention mechanism
- Include memory layout diagram with ASCII art
- Document constraints, limitations, and usage examples
- Helps future maintainers understand design decisions
Files changed:
- core/iwasm/common/wasm_memory.c: Fix size=0 behavior
- core/shared/utils/bh_platform.h: Add WASM_RUNTIME_API_INTERN
- core/shared/mem-alloc/ems/ems_gc_internal.h: Add helper + docs
- core/shared/mem-alloc/ems/ems_alloc.c: Use helper function
- tests/unit/mem-alloc/mem_alloc_test.c: Update test expectations
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>1 parent 95157c2 commit a02ad8b
File tree
5 files changed
+142
-31
lines changed- core
- iwasm/common
- tests/unit/mem-alloc
5 files changed
+142
-31
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1073 | 1073 | | |
1074 | 1074 | | |
1075 | 1075 | | |
1076 | | - | |
1077 | | - | |
1078 | | - | |
1079 | | - | |
1080 | | - | |
1081 | 1076 | | |
1082 | 1077 | | |
1083 | 1078 | | |
1084 | 1079 | | |
1085 | 1080 | | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
1086 | 1090 | | |
1087 | 1091 | | |
1088 | 1092 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
556 | 556 | | |
557 | 557 | | |
558 | 558 | | |
559 | | - | |
560 | | - | |
561 | 559 | | |
562 | | - | |
| 560 | + | |
563 | 561 | | |
564 | 562 | | |
565 | 563 | | |
| |||
779 | 777 | | |
780 | 778 | | |
781 | 779 | | |
782 | | - | |
783 | | - | |
784 | | - | |
785 | | - | |
786 | | - | |
787 | | - | |
788 | | - | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
789 | 784 | | |
790 | 785 | | |
791 | 786 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
| 18 | + | |
23 | 19 | | |
24 | 20 | | |
25 | 21 | | |
| |||
102 | 98 | | |
103 | 99 | | |
104 | 100 | | |
105 | | - | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
106 | 104 | | |
107 | | - | |
| 105 | + | |
| 106 | + | |
108 | 107 | | |
109 | | - | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
110 | 112 | | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
115 | 165 | | |
116 | | - | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
117 | 188 | | |
118 | | - | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
119 | 192 | | |
120 | 193 | | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
121 | 214 | | |
122 | 215 | | |
123 | 216 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
21 | 38 | | |
22 | 39 | | |
23 | 40 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
406 | 406 | | |
407 | 407 | | |
408 | 408 | | |
409 | | - | |
| 409 | + | |
410 | 410 | | |
411 | | - | |
| 411 | + | |
| 412 | + | |
412 | 413 | | |
| 414 | + | |
413 | 415 | | |
414 | 416 | | |
415 | 417 | | |
| |||
0 commit comments