@@ -220,6 +220,12 @@ struct LinearAllocator : Allocator
220
220
uint32_t pre_alloc_addr_;
221
221
// /> runtime global size of the currently used memory, used as pointer to next free allocation
222
222
Global<U32x1> alloc_addr_;
223
+ // /> compile-time total memory consumption
224
+ uint32_t pre_alloc_total_mem_ = 0 ;
225
+ // /> runtime total memory consumption
226
+ Global<U32x1> alloc_total_mem_;
227
+ // /> runtime peak memory consumption
228
+ Global<U32x1> alloc_peak_mem_;
223
229
224
230
public:
225
231
LinearAllocator (const memory::AddressSpace &memory, uint32_t start_addr)
@@ -229,6 +235,8 @@ struct LinearAllocator : Allocator
229
235
M_insist (start_addr != 0 , " memory address 0 is reserved as `nullptr`" );
230
236
#ifdef M_ENABLE_SANITY_FIELDS
231
237
alloc_addr_.val ().discard (); // artificial use of `alloc_addr_` to silence diagnostics if allocator is not used
238
+ alloc_total_mem_.val ().discard (); // artificial use of `alloc_total_mem_` to silence diagnostics if allocator is not used
239
+ alloc_peak_mem_.val ().discard (); // artificial use of `alloc_peak_mem_` to silence diagnostics if allocator is not used
232
240
#endif
233
241
}
234
242
@@ -245,6 +253,7 @@ struct LinearAllocator : Allocator
245
253
align_pre_memory (alignment);
246
254
void *ptr = static_cast <uint8_t *>(memory_.addr ()) + pre_alloc_addr_;
247
255
pre_alloc_addr_ += bytes; // advance memory size by bytes
256
+ pre_alloc_total_mem_ += bytes;
248
257
M_insist (memory_.size () >= pre_alloc_addr_, " allocation must fit in memory" );
249
258
return ptr;
250
259
}
@@ -257,6 +266,7 @@ struct LinearAllocator : Allocator
257
266
align_pre_memory (alignment);
258
267
Ptr<void > ptr (U32x1 (pre_alloc_addr_).template to <void *>());
259
268
pre_alloc_addr_ += bytes; // advance memory size by bytes
269
+ pre_alloc_total_mem_ += bytes;
260
270
M_insist (memory_.size () >= pre_alloc_addr_, " allocation must fit in memory" );
261
271
return ptr;
262
272
}
@@ -266,7 +276,9 @@ struct LinearAllocator : Allocator
266
276
if (alignment != 1U )
267
277
align_memory (alignment);
268
278
Var<Ptr<void >> ptr (alloc_addr_.template to <void *>());
269
- alloc_addr_ += bytes; // advance memory size by bytes
279
+ alloc_addr_ += bytes.clone (); // advance memory size by bytes
280
+ alloc_total_mem_ += bytes;
281
+ alloc_peak_mem_ = Select (alloc_peak_mem_ > alloc_addr_, alloc_peak_mem_, alloc_addr_);
270
282
Wasm_insist (memory_.size () >= alloc_addr_, " allocation must fit in memory" );
271
283
return ptr;
272
284
}
@@ -285,6 +297,10 @@ struct LinearAllocator : Allocator
285
297
pre_allocations_performed_ = true ;
286
298
}
287
299
300
+ uint32_t pre_allocated_memory_consumption () const override { return pre_alloc_total_mem_; }
301
+ U32x1 allocated_memory_consumption () const override { return alloc_total_mem_; }
302
+ U32x1 allocated_memory_peak () const override { return alloc_peak_mem_; }
303
+
288
304
private:
289
305
/* * Aligns the memory for pre-allocations with alignment requirement `align`. */
290
306
void align_pre_memory (uint32_t alignment) {
0 commit comments