Commit 394de48d2f for aom
commit 394de48d2f4d24d8012de5520e2a59e1f8d0105f
Author: Frank Galligan <fgalligan@google.com>
Date: Mon Nov 9 23:27:19 2026 -0800
aom_mem: use calloc for zero-initialized allocs
Use calloc() in aom_calloc() for buffers with aligned_size > 4096 B so
the allocator can return freshly mapped zeroed pages without a second
memset() pass, while keeping small allocations on malloc() + memset().
Extract align_and_set_malloc_addr() shared between aom_memalign() and
aom_calloc(), and add unit tests in test/aom_mem_test.cc covering
small, boundary (4096 B), large, zero-size, and
AOM_MAX_ALLOCABLE_MEMORY paths.
Speedup vs origin/main (AMD EPYC 7B13, --cpu-used=6, bit-exact):
-2.67% 1080p allintra (8T), -0.34% (1T) / -0.93% (8T) 720p allintra.
Binary size (x86_64 -O3): libaom.a .text +0.0 KiB (+0.00%),
.rodata +0.1 KiB (+0.01%); stripped aomenc +0.0 KiB (+0.00%).
Memory (peak RSS, 1080p allintra): 1T -580 KiB (-1.21%),
8T +68 KiB (+0.12%).
720p good 1T -1380 KiB (-0.55%), rt 1T -976 KiB (-2.20%).
TAG=agy
CONV=b929a630-a6cd-45a0-979c-1767c30178ee
Change-Id: I3fe63e8075672c07a0e29209f809c6d63557489f
diff --git a/aom_mem/aom_mem.c b/aom_mem/aom_mem.c
index f4126e34bd..9ceb18bd25 100644
--- a/aom_mem/aom_mem.c
+++ b/aom_mem/aom_mem.c
@@ -52,11 +52,8 @@ static void *GetActualMallocAddress(void *const mem) {
return (void *)(*malloc_addr_location);
}
-void *aom_memalign(size_t align, size_t size) {
+static void *align_and_set_malloc_addr(void *const addr, size_t align) {
void *x = NULL;
- if (!check_size_argument_overflow(1, size, align)) return NULL;
- const size_t aligned_size = size + GetAllocationPaddingSize(align);
- void *const addr = malloc(aligned_size);
if (addr) {
x = aom_align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, align);
SetActualMallocAddress(x, addr);
@@ -64,13 +61,29 @@ void *aom_memalign(size_t align, size_t size) {
return x;
}
+void *aom_memalign(size_t align, size_t size) {
+ if (!check_size_argument_overflow(1, size, align)) return NULL;
+ const size_t aligned_size = size + GetAllocationPaddingSize(align);
+ void *const addr = malloc(aligned_size);
+ return align_and_set_malloc_addr(addr, align);
+}
+
void *aom_malloc(size_t size) { return aom_memalign(DEFAULT_ALIGNMENT, size); }
void *aom_calloc(size_t num, size_t size) {
if (!check_size_argument_overflow(num, size, DEFAULT_ALIGNMENT)) return NULL;
const size_t total_size = num * size;
- void *const x = aom_malloc(total_size);
- if (x) memset(x, 0, total_size);
+ const size_t aligned_size =
+ total_size + GetAllocationPaddingSize(DEFAULT_ALIGNMENT);
+ // Use calloc() for larger buffers so freshly mapped/sbrk'd pages avoid an
+ // explicit zero-fill pass; keep malloc() + memset() for small buffers.
+ const int use_calloc = aligned_size > 4096;
+ void *const addr =
+ use_calloc ? calloc(1, aligned_size) : malloc(aligned_size);
+ void *const x = align_and_set_malloc_addr(addr, DEFAULT_ALIGNMENT);
+ if (x && !use_calloc) {
+ memset(x, 0, total_size);
+ }
return x;
}
diff --git a/test/aom_mem_test.cc b/test/aom_mem_test.cc
index 2496342940..3118a91053 100644
--- a/test/aom_mem_test.cc
+++ b/test/aom_mem_test.cc
@@ -9,13 +9,49 @@
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/
-#include "aom_mem/aom_mem.h"
-
-#include <cstdio>
#include <cstddef>
+#include <cstdint>
+#include <cstring>
+#include <memory>
+#include "aom_mem/aom_mem.h"
#include "gtest/gtest.h"
+namespace {
+
+struct AomFreeDeleter {
+ void operator()(void *ptr) const { aom_free(ptr); }
+};
+
+using AomMemPtr = std::unique_ptr<void, AomFreeDeleter>;
+
+constexpr size_t kMinAlignment = 2 * sizeof(void *);
+
+void ExpectCallocZeroedAndAligned(size_t num, size_t size) {
+ SCOPED_TRACE(testing::Message() << "num=" << num << " size=" << size);
+ const size_t total_size = num * size;
+ // Poison a matching allocation so recycled heap memory is non-zero.
+ {
+ AomMemPtr poisoned(aom_malloc(total_size));
+ ASSERT_NE(poisoned.get(), nullptr);
+ if (total_size > 0) {
+ std::memset(poisoned.get(), 0xA5, total_size);
+ }
+ }
+
+ AomMemPtr ptr(aom_calloc(num, size));
+ ASSERT_NE(ptr.get(), nullptr);
+ EXPECT_EQ(reinterpret_cast<std::uintptr_t>(ptr.get()) % kMinAlignment,
+ static_cast<size_t>(0));
+
+ const auto *bytes = static_cast<const unsigned char *>(ptr.get());
+ for (size_t i = 0; i < total_size; ++i) {
+ ASSERT_EQ(bytes[i], 0) << "at byte " << i;
+ }
+}
+
+} // namespace
+
TEST(AomMemTest, Overflow) {
// Allocations are aligned > 1 so SIZE_MAX should always fail.
ASSERT_EQ(aom_malloc(SIZE_MAX), nullptr);
@@ -26,9 +62,42 @@ TEST(AomMemTest, Overflow) {
ASSERT_EQ(aom_memalign(64, SIZE_MAX), nullptr);
ASSERT_EQ(aom_memalign(64, SIZE_MAX - 64), nullptr);
ASSERT_EQ(aom_memalign(64, SIZE_MAX - 64 - sizeof(size_t) + 2), nullptr);
+
+ AomMemPtr max_ptr(
+ aom_calloc(1, static_cast<size_t>(AOM_MAX_ALLOCABLE_MEMORY)));
+ EXPECT_EQ(max_ptr.get(), nullptr);
}
TEST(AomMemTest, NullParams) {
ASSERT_EQ(aom_memset16(nullptr, 0, 0), nullptr);
aom_free(nullptr);
}
+
+TEST(AomMemTest, Calloc) {
+ const size_t max_pad = kMinAlignment - 1 + sizeof(size_t);
+ // Small allocations (aligned_size <= 4096, malloc + memset path).
+ ASSERT_NO_FATAL_FAILURE(ExpectCallocZeroedAndAligned(1, 256));
+ ASSERT_NO_FATAL_FAILURE(ExpectCallocZeroedAndAligned(64, 4));
+ ASSERT_NO_FATAL_FAILURE(ExpectCallocZeroedAndAligned(1, 4096 - max_pad));
+
+ // Large allocations (aligned_size > 4096, calloc path).
+ ASSERT_NO_FATAL_FAILURE(ExpectCallocZeroedAndAligned(1, 4096 - max_pad + 1));
+ ASSERT_NO_FATAL_FAILURE(ExpectCallocZeroedAndAligned(64, 64));
+ ASSERT_NO_FATAL_FAILURE(ExpectCallocZeroedAndAligned(1, 8192));
+ ASSERT_NO_FATAL_FAILURE(ExpectCallocZeroedAndAligned(1024, 256));
+ ASSERT_NO_FATAL_FAILURE(ExpectCallocZeroedAndAligned(1, 256 * 1024));
+
+ // Zero-size allocations should succeed and return aligned pointers.
+ const struct {
+ size_t num;
+ size_t size;
+ } kZeroSizeCases[] = {
+ { 0, 0 }, { 0, 16 }, { 16, 0 }, { 0, SIZE_MAX }, { SIZE_MAX, 0 },
+ };
+ for (const auto &test_case : kZeroSizeCases) {
+ AomMemPtr ptr(aom_calloc(test_case.num, test_case.size));
+ ASSERT_NE(ptr.get(), nullptr);
+ EXPECT_EQ(reinterpret_cast<std::uintptr_t>(ptr.get()) % kMinAlignment,
+ static_cast<size_t>(0));
+ }
+}