Add MemoryAllocator used_size()/free_size() accessors (#20307)#20307
Add MemoryAllocator used_size()/free_size() accessors (#20307)#20307yctwo wants to merge 1 commit into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/20307
Note: Links to docs will display an error until the docs builds have been completed. ❌ 3 New Failures, 3 Unrelated Failures, 2 Unclassified FailuresAs of commit 35e7930 with merge base a581673 ( NEW FAILURES - The following jobs have failed:
UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:
BROKEN TRUNK - The following jobs failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@yctwo has exported this pull request. If you are a Meta employee, you can view the originating Diff in D108612649. |
|
|
|
This PR needs a
|
Summary: ## Problem `executorch::runtime::MemoryAllocator` is a bump allocator, but it does not expose how much of its buffer is currently consumed: the bump cursor (`cur_`) is private. Consumers that need the allocation high-water have to re-derive it from the returned block pointer and the base address. ## Duplication this removes This exact "used offset" math is already re-implemented by multiple `MemoryAllocator` subclasses: - [[https://www.internalfb.com/code/fbsource/xplat/executorch/examples/espressif/executor_runner/esp_memory_allocator.cpp?lines=19-20 | esp_memory_allocator.cpp]] — `used_ = (uint8_t*)ret + size - base_address()`, exposed via `used_size()` / `free_size()`. - [[https://www.internalfb.com/code/fbsource/xplat/executorch/examples/arm/executor_runner/arm_memory_allocator.cpp?lines=35-61 | arm_memory_allocator.cpp]] — tracks `used_` with equivalent alignment math, exposed via `used_size()` / `free_size()`. A per-op-locked shared scratch allocator on wrist firmware needs the same value (see T274998164). ## Change Add two **virtual** accessors to the base class, alongside the existing virtual `size()` / `base_address()`: - `used_size()` → `cur_ - begin_` (bytes allocated since construction or the last `reset()`). - `free_size()` → `end_ - cur_` (bytes still available, not accounting for any alignment padding a future allocation may add). Both return `size_t` -- the conventional byte-count type (`allocate()` already takes `size_t`), matching the same-named accessors the Arm/Esp examples above already expose. The base provides the default bump-cursor accounting. Subclasses that track usage differently (the Arm/Esp examples; internal heap-backed allocators) keep their own implementations -- this diff just adds `override` to those existing `size_t used_size()` / `free_size()` declarations so they bind to the new virtuals (no behavior change; all call sites are concrete-typed today). Differential Revision: D108612649
Summary:
Problem
executorch::runtime::MemoryAllocatoris a bump allocator, but it does notexpose how much of its buffer is currently consumed: the bump cursor (
cur_)is private. Consumers that need the allocation high-water have to re-derive it
from the returned block pointer and the base address.
Duplication this removes
This exact "used offset" math is already re-implemented by multiple
MemoryAllocatorsubclasses:used_ = (uint8_t*)ret + size - base_address(), exposed viaused_size()/free_size().used_with equivalent alignment math, exposed viaused_size()/free_size().A per-op-locked shared scratch allocator on wrist firmware needs the same value
(see T274998164).
Change
Add two virtual accessors to the base class, alongside the existing virtual
size()/base_address():used_size()→cur_ - begin_(bytes allocated since construction or thelast
reset()).free_size()→end_ - cur_(bytes still available, not accounting for anyalignment padding a future allocation may add).
Both return
size_t-- the conventional byte-count type (allocate()alreadytakes
size_t), matching the same-named accessors the Arm/Esp examples abovealready expose.
The base provides the default bump-cursor accounting. Subclasses that track
usage differently (the Arm/Esp examples; internal heap-backed allocators) keep
their own implementations -- this diff just adds
overrideto those existingsize_t used_size()/free_size()declarations so they bind to the newvirtuals (no behavior change; all call sites are concrete-typed today).
Differential Revision: D108612649