Skip to content

Commit 01a2bf3

Browse files
committed
Update base for Update on "[Executorch] Make module constructors uniform across"
Existing constructors dont compose well such that if you want data loader or data files constructor then you cannot get to override memory allocator. Fix that. Differential Revision: [D86120037](https://our.internmc.facebook.com/intern/diff/D86120037/) [ghstack-poisoned]
1 parent 7f89052 commit 01a2bf3

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

extension/memory_allocator/cpu_caching_malloc_allocator.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ class CPUCachingAllocator : public executorch::runtime::MemoryAllocator {
5959
// New invariants must be written.
6060
FlatHashMap<size_t, SmallVector<void*, 16>> available_map_;
6161
FlatHashMap<void*, size_t> allocation_map_;
62-
// Since allocation_map, which is a global instance, is mutated/read via
63-
// all public APIs we need a global mutex.
62+
// Since allocation_map_ and other member variables are mutated/read via
63+
// all public APIs, we need a mutex to protect concurrent access to these
64+
// instance members.
6465
std::mutex mutex_;
6566
size_t max_size_;
6667
size_t current_size_;

extension/memory_allocator/test/cpu_caching_malloc_allocator_test.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ TEST_F(CPUCachingAllocatorTest, SizeAlignmentAdjustment) {
280280
CPUCachingAllocator allocator(1024 * 1024); // 1MB max size
281281

282282
// Test that allocation sizes get properly aligned
283-
auto p1 = allocator.allocate(100, 256); // Size not aligned to 256
283+
auto p1 = allocator.allocate(100, 256); // Size aligned to 256
284284
EXPECT_NE(p1, nullptr);
285285
EXPECT_ALIGNED(p1, 256);
286286

@@ -289,9 +289,9 @@ TEST_F(CPUCachingAllocatorTest, SizeAlignmentAdjustment) {
289289
// allocation
290290
allocator.reset();
291291

292-
auto p3 = allocator.allocate(100, 256);
292+
auto p3 = allocator.allocate(100, 512);
293293
// Should reuse p1 due to alignment adjustment
294-
EXPECT_EQ(p1, p3);
294+
EXPECT_NE(p1, p3);
295295
}
296296

297297
TEST_F(CPUCachingAllocatorTest, ResetMultipleTimes) {
@@ -348,7 +348,8 @@ TEST_F(CPUCachingAllocatorTest, ResetCachesWhenUnderMaxSize) {
348348
EXPECT_NE(p1, nullptr);
349349
EXPECT_NE(p2, nullptr);
350350

351-
// Reset should cache the allocations since current_size (1024) <= max_size (2048)
351+
// Reset should cache the allocations since current_size (1024) <= max_size
352+
// (2048)
352353
allocator.reset();
353354

354355
// Subsequent allocations should reuse the cached pointers

0 commit comments

Comments
 (0)