diff --git a/examples/arm/executor_runner/CMakeLists.txt b/examples/arm/executor_runner/CMakeLists.txt index 7666af45769..e2223b83f00 100644 --- a/examples/arm/executor_runner/CMakeLists.txt +++ b/examples/arm/executor_runner/CMakeLists.txt @@ -566,7 +566,7 @@ endif() add_executable(arm_executor_runner) target_sources( - arm_executor_runner PRIVATE arm_executor_runner.cpp arm_perf_monitor.cpp + arm_executor_runner PRIVATE arm_executor_runner.cpp arm_perf_monitor.cpp arm_memory_allocator.cpp ) # Include the target's bare-metal linker script diff --git a/examples/arm/executor_runner/arm_executor_runner.cpp b/examples/arm/executor_runner/arm_executor_runner.cpp index 27bbeee5465..3104ebcc862 100644 --- a/examples/arm/executor_runner/arm_executor_runner.cpp +++ b/examples/arm/executor_runner/arm_executor_runner.cpp @@ -19,6 +19,7 @@ #include #include +#include "arm_memory_allocator.h" #include "arm_perf_monitor.h" #if defined(ET_BUNDLE_IO) @@ -288,58 +289,6 @@ class Box { } }; -// Setup our own allocator that can show some extra stuff like used and free -// memory info -class ArmMemoryAllocator : public executorch::runtime::MemoryAllocator { - public: - ArmMemoryAllocator(uint32_t size, uint8_t* base_address) - : MemoryAllocator(size, base_address), used_(0), peak_used_(0) {} - - void* allocate(size_t size, size_t alignment = kDefaultAlignment) override { - void* ret = executorch::runtime::MemoryAllocator::allocate(size, alignment); - if (ret != nullptr) { - // Align with the same code as in MemoryAllocator::allocate() to keep - // used_ "in sync" As alignment is expected to be power of 2 (checked by - // MemoryAllocator::allocate()) we can check it the lower bits - // (same as alignment - 1) is zero or not. - if ((size & (alignment - 1)) == 0) { - // Already aligned. - used_ += size; - } else { - used_ = (used_ | (alignment - 1)) + 1 + size; - } - if (used_ > peak_used_) - peak_used_ = used_; - } - return ret; - } - - // Returns the used size of the allocator's memory buffer. - size_t used_size() const { - return used_; - } - - // Returns the peak memory usage of the allocator's memory buffer - // Peak usage is useful when doing multiple allocations & resets - size_t peak_used() const { - return peak_used_; - } - - // Returns the free size of the allocator's memory buffer. - size_t free_size() const { - return executorch::runtime::MemoryAllocator::size() - used_; - } - - void reset() { - executorch::runtime::MemoryAllocator::reset(); - used_ = 0; - } - - private: - size_t used_; - size_t peak_used_; -}; - Result prepare_input_tensors( Method& method, MemoryAllocator& allocator, diff --git a/examples/arm/executor_runner/arm_memory_allocator.cpp b/examples/arm/executor_runner/arm_memory_allocator.cpp new file mode 100644 index 00000000000..e22439a239d --- /dev/null +++ b/examples/arm/executor_runner/arm_memory_allocator.cpp @@ -0,0 +1,46 @@ +/* Copyright 2025 Arm Limited and/or its affiliates. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include "arm_memory_allocator.h" + +ArmMemoryAllocator::ArmMemoryAllocator(uint32_t size, uint8_t* base_address) + : MemoryAllocator(size, base_address), used_(0), peak_used_(0) {} + +void* ArmMemoryAllocator::allocate(size_t size, size_t alignment) { + void* ret = executorch::runtime::MemoryAllocator::allocate(size, alignment); + if (ret != nullptr) { + // Align with the same code as in MemoryAllocator::allocate() to keep + // used_ "in sync" As alignment is expected to be power of 2 (checked by + // MemoryAllocator::allocate()) we can check it the lower bits + // (same as alignment - 1) is zero or not. + if ((size & (alignment - 1)) == 0) { + // Already aligned. + used_ += size; + } else { + used_ = (used_ | (alignment - 1)) + 1 + size; + } + if (used_ > peak_used_) + peak_used_ = used_; + } + return ret; +} + +size_t ArmMemoryAllocator::used_size() const { + return used_; +} + +size_t ArmMemoryAllocator::peak_used() const { + return peak_used_; +} + +size_t ArmMemoryAllocator::free_size() const { + return executorch::runtime::MemoryAllocator::size() - used_; +} + +void ArmMemoryAllocator::reset() { + executorch::runtime::MemoryAllocator::reset(); + used_ = 0; +} diff --git a/examples/arm/executor_runner/arm_memory_allocator.h b/examples/arm/executor_runner/arm_memory_allocator.h new file mode 100644 index 00000000000..f7e8939c655 --- /dev/null +++ b/examples/arm/executor_runner/arm_memory_allocator.h @@ -0,0 +1,35 @@ +/* Copyright 2025 Arm Limited and/or its affiliates. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include + +using executorch::runtime::MemoryAllocator; + +#pragma once + +// Setup our own allocator that can show some extra stuff like used and free +// memory info +class ArmMemoryAllocator : public executorch::runtime::MemoryAllocator { + public: + ArmMemoryAllocator(uint32_t size, uint8_t* base_address); + + void* allocate(size_t size, size_t alignment = kDefaultAlignment) override; + + // Returns the used size of the allocator's memory buffer. + size_t used_size() const; + + // Returns the peak memory usage of the allocator's memory buffer + // Peak usage is useful when doing multiple allocations & resets + size_t peak_used() const; + + // Returns the free size of the allocator's memory buffer. + size_t free_size() const; + void reset(); + + private: + size_t used_; + size_t peak_used_; +};