Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/arm/executor_runner/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 1 addition & 52 deletions examples/arm/executor_runner/arm_executor_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <memory>
#include <vector>

#include "arm_memory_allocator.h"
#include "arm_perf_monitor.h"

#if defined(ET_BUNDLE_IO)
Expand Down Expand Up @@ -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<BufferCleanup> prepare_input_tensors(
Method& method,
MemoryAllocator& allocator,
Expand Down
46 changes: 46 additions & 0 deletions examples/arm/executor_runner/arm_memory_allocator.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
35 changes: 35 additions & 0 deletions examples/arm/executor_runner/arm_memory_allocator.h
Original file line number Diff line number Diff line change
@@ -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 <executorch/runtime/core/memory_allocator.h>

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_;
};
Loading