Skip to content

Commit cee4750

Browse files
committed
Splicing memory allocator from executor runner to allow for code reuse for other arm applications, e.g. zephyr
1 parent cd90531 commit cee4750

File tree

4 files changed

+84
-53
lines changed

4 files changed

+84
-53
lines changed

examples/arm/executor_runner/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ endif()
566566
add_executable(arm_executor_runner)
567567

568568
target_sources(
569-
arm_executor_runner PRIVATE arm_executor_runner.cpp arm_perf_monitor.cpp
569+
arm_executor_runner PRIVATE arm_executor_runner.cpp arm_perf_monitor.cpp arm_memory_allocator.cpp
570570
)
571571

572572
# Include the target's bare-metal linker script

examples/arm/executor_runner/arm_executor_runner.cpp

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <vector>
2121

2222
#include "arm_perf_monitor.h"
23+
#include "arm_memory_allocator.h"
2324

2425
#if defined(ET_BUNDLE_IO)
2526
#include <executorch/devtools/bundled_program/bundled_program.h>
@@ -288,58 +289,6 @@ class Box {
288289
}
289290
};
290291

291-
// Setup our own allocator that can show some extra stuff like used and free
292-
// memory info
293-
class ArmMemoryAllocator : public executorch::runtime::MemoryAllocator {
294-
public:
295-
ArmMemoryAllocator(uint32_t size, uint8_t* base_address)
296-
: MemoryAllocator(size, base_address), used_(0), peak_used_(0) {}
297-
298-
void* allocate(size_t size, size_t alignment = kDefaultAlignment) override {
299-
void* ret = executorch::runtime::MemoryAllocator::allocate(size, alignment);
300-
if (ret != nullptr) {
301-
// Align with the same code as in MemoryAllocator::allocate() to keep
302-
// used_ "in sync" As alignment is expected to be power of 2 (checked by
303-
// MemoryAllocator::allocate()) we can check it the lower bits
304-
// (same as alignment - 1) is zero or not.
305-
if ((size & (alignment - 1)) == 0) {
306-
// Already aligned.
307-
used_ += size;
308-
} else {
309-
used_ = (used_ | (alignment - 1)) + 1 + size;
310-
}
311-
if (used_ > peak_used_)
312-
peak_used_ = used_;
313-
}
314-
return ret;
315-
}
316-
317-
// Returns the used size of the allocator's memory buffer.
318-
size_t used_size() const {
319-
return used_;
320-
}
321-
322-
// Returns the peak memory usage of the allocator's memory buffer
323-
// Peak usage is useful when doing multiple allocations & resets
324-
size_t peak_used() const {
325-
return peak_used_;
326-
}
327-
328-
// Returns the free size of the allocator's memory buffer.
329-
size_t free_size() const {
330-
return executorch::runtime::MemoryAllocator::size() - used_;
331-
}
332-
333-
void reset() {
334-
executorch::runtime::MemoryAllocator::reset();
335-
used_ = 0;
336-
}
337-
338-
private:
339-
size_t used_;
340-
size_t peak_used_;
341-
};
342-
343292
Result<BufferCleanup> prepare_input_tensors(
344293
Method& method,
345294
MemoryAllocator& allocator,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Copyright 2025 Arm Limited and/or its affiliates.
2+
*
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE file in the root directory of this source tree.
5+
*/
6+
7+
#include "arm_memory_allocator.h"
8+
9+
ArmMemoryAllocator::ArmMemoryAllocator(uint32_t size, uint8_t* base_address)
10+
: MemoryAllocator(size, base_address), used_(0), peak_used_(0) {}
11+
12+
void* ArmMemoryAllocator::allocate(size_t size, size_t alignment = kDefaultAlignment) override {
13+
void* ret = executorch::runtime::MemoryAllocator::allocate(size, alignment);
14+
if (ret != nullptr) {
15+
// Align with the same code as in MemoryAllocator::allocate() to keep
16+
// used_ "in sync" As alignment is expected to be power of 2 (checked by
17+
// MemoryAllocator::allocate()) we can check it the lower bits
18+
// (same as alignment - 1) is zero or not.
19+
if ((size & (alignment - 1)) == 0) {
20+
// Already aligned.
21+
used_ += size;
22+
} else {
23+
used_ = (used_ | (alignment - 1)) + 1 + size;
24+
}
25+
if (used_ > peak_used_)
26+
peak_used_ = used_;
27+
}
28+
return ret;
29+
}
30+
31+
size_t ArmMemoryAllocator::used_size() const {
32+
return used_;
33+
}
34+
35+
size_t ArmMemoryAllocator::peak_used() const {
36+
return peak_used_;
37+
}
38+
39+
size_t ArmMemoryAllocator::free_size() const {
40+
return executorch::runtime::MemoryAllocator::size() - used_;
41+
}
42+
43+
void ArmMemoryAllocator::reset() {
44+
executorch::runtime::MemoryAllocator::reset();
45+
used_ = 0;
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* Copyright 2025 Arm Limited and/or its affiliates.
2+
*
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE file in the root directory of this source tree.
5+
*/
6+
7+
#include <executorch/runtime/core/memory_allocator.h>
8+
9+
using executorch::runtime::MemoryAllocator;
10+
11+
#pragma once
12+
13+
// Setup our own allocator that can show some extra stuff like used and free
14+
// memory info
15+
class ArmMemoryAllocator : public executorch::runtime::MemoryAllocator {
16+
public:
17+
ArmMemoryAllocator(uint32_t size, uint8_t* base_address);
18+
19+
void* allocate(size_t size, size_t alignment = kDefaultAlignment) override;
20+
21+
// Returns the used size of the allocator's memory buffer.
22+
size_t used_size() const;
23+
24+
// Returns the peak memory usage of the allocator's memory buffer
25+
// Peak usage is useful when doing multiple allocations & resets
26+
size_t peak_used() const;
27+
28+
// Returns the free size of the allocator's memory buffer.
29+
size_t free_size() const;
30+
void reset();
31+
32+
private:
33+
size_t used_;
34+
size_t peak_used_;
35+
};
36+

0 commit comments

Comments
 (0)