@@ -220,6 +220,12 @@ struct LinearAllocator : Allocator
220220 uint32_t pre_alloc_addr_;
221221 // /> runtime global size of the currently used memory, used as pointer to next free allocation
222222 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_;
223229
224230 public:
225231 LinearAllocator (const memory::AddressSpace &memory, uint32_t start_addr)
@@ -229,6 +235,8 @@ struct LinearAllocator : Allocator
229235 M_insist (start_addr != 0 , " memory address 0 is reserved as `nullptr`" );
230236#ifdef M_ENABLE_SANITY_FIELDS
231237 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
232240#endif
233241 }
234242
@@ -245,6 +253,7 @@ struct LinearAllocator : Allocator
245253 align_pre_memory (alignment);
246254 void *ptr = static_cast <uint8_t *>(memory_.addr ()) + pre_alloc_addr_;
247255 pre_alloc_addr_ += bytes; // advance memory size by bytes
256+ pre_alloc_total_mem_ += bytes;
248257 M_insist (memory_.size () >= pre_alloc_addr_, " allocation must fit in memory" );
249258 return ptr;
250259 }
@@ -257,6 +266,7 @@ struct LinearAllocator : Allocator
257266 align_pre_memory (alignment);
258267 Ptr<void > ptr (U32x1 (pre_alloc_addr_).template to <void *>());
259268 pre_alloc_addr_ += bytes; // advance memory size by bytes
269+ pre_alloc_total_mem_ += bytes;
260270 M_insist (memory_.size () >= pre_alloc_addr_, " allocation must fit in memory" );
261271 return ptr;
262272 }
@@ -266,7 +276,9 @@ struct LinearAllocator : Allocator
266276 if (alignment != 1U )
267277 align_memory (alignment);
268278 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_);
270282 Wasm_insist (memory_.size () >= alloc_addr_, " allocation must fit in memory" );
271283 return ptr;
272284 }
@@ -285,6 +297,10 @@ struct LinearAllocator : Allocator
285297 pre_allocations_performed_ = true ;
286298 }
287299
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+
288304 private:
289305 /* * Aligns the memory for pre-allocations with alignment requirement `align`. */
290306 void align_pre_memory (uint32_t alignment) {
0 commit comments