Skip to content

Commit 447554d

Browse files
committed
Update base for Update on "[Executorch] Use temp allocator for allocating scratch memory"
This allows us to leverage temp memory allocator and if that allocator is caching allocator it reduces the allocaiton overhead. Differential Revision: [D85532076](https://our.internmc.facebook.com/intern/diff/D85532076/) [ghstack-poisoned]
1 parent e0f666b commit 447554d

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

extension/memory_allocator/cpu_caching_malloc_allocator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ void* CPUCachingAllocator::allocate(size_t size, size_t alignment) {
1919
return nullptr;
2020
}
2121
alignment = std::max(alignment, kCachingAllocatorDefaultAlignment);
22-
size_t adjusted_size =
22+
auto adjusted_size_value =
2323
executorch::extension::utils::get_aligned_size(size, alignment);
24-
if (adjusted_size == 0) {
24+
if (!adjusted_size_value.ok()) {
2525
return nullptr;
2626
}
27-
size = adjusted_size;
27+
size = adjusted_size_value.get();
2828

2929
std::lock_guard<std::mutex> guard(mutex_);
3030
const auto& it = available_map_.find(size);

extension/memory_allocator/malloc_memory_allocator.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ class MallocMemoryAllocator : public executorch::runtime::MemoryAllocator {
5252
return nullptr;
5353
}
5454

55-
size = executorch::extension::utils::get_aligned_size(size, alignment);
55+
auto adjusted_size_value =
56+
executorch::extension::utils::get_aligned_size(size, alignment);
57+
if (!adjusted_size_value.ok()) {
58+
return nullptr;
59+
}
60+
size = adjusted_size_value.get();
5661
void* mem_ptr = std::malloc(size);
5762
if (!mem_ptr) {
5863
ET_LOG(Error, "Malloc failed to allocate %zu bytes", size);

extension/memory_allocator/memory_allocator_utils.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
#include <cstdlib>
1414

1515
#include <executorch/runtime/core/error.h>
16+
#include <executorch/runtime/core/result.h>
1617
#include <executorch/runtime/platform/compiler.h>
1718

19+
using executorch::runtime::Error;
20+
using executorch::runtime::Result;
1821
namespace executorch::extension::utils {
1922

2023
// Util to get alighment adjusted allocation size
21-
inline size_t get_aligned_size(size_t size, size_t alignment) {
24+
inline Result<size_t> get_aligned_size(size_t size, size_t alignment) {
2225
// The minimum alignment that malloc() is guaranteed to provide.
2326
static constexpr size_t kMallocAlignment = alignof(std::max_align_t);
2427
if (alignment > kMallocAlignment) {
@@ -28,7 +31,7 @@ inline size_t get_aligned_size(size_t size, size_t alignment) {
2831
const size_t extra = alignment - 1;
2932
if ET_UNLIKELY (extra >= SIZE_MAX - size) {
3033
ET_LOG(Error, "Malloc size overflow: size=%zu + extra=%zu", size, extra);
31-
return 0;
34+
return Result<size_t>(Error::InvalidArgument);
3235
}
3336
size += extra;
3437
}

0 commit comments

Comments
 (0)