Skip to content
Open
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
13 changes: 12 additions & 1 deletion folly/concurrency/CacheLocality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,18 @@ class SimpleAllocator {
// To support array aggregate initialization without an implicit constructor.
struct Ctor {};

SimpleAllocator(Ctor, size_t sz) : sz_(sz) {}
SimpleAllocator(Ctor, size_t sz) : sz_(sz) {
static_assert(
sizeof(void*) <= 64,
"SimpleAllocator assumes sizeof(void*) fits in maximum size class");
if (sz_ < sizeof(void*)) {
folly::throw_exception<std::invalid_argument>(fmt::format(
"SimpleAllocator size {} is too small (minimum: {})",
sz_,
sizeof(void*)));
}
}

~SimpleAllocator() {
std::lock_guard g(m_);
for (auto& block : blocks_) {
Expand Down
3 changes: 3 additions & 0 deletions folly/concurrency/CacheLocality.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ class LLCAccessSpreader {
* AccessSpreader can allocate memory in smaller-than cacheline increments, and
* be assured that it won't cause more false sharing than it otherwise would.
*
* Allocations smaller than sizeof(void*) (typically 8 bytes) are automatically
* rounded up to ensure correct internal bookkeeping.
*
* Note that allocation and deallocation takes a per-size-class lock.
*
* Memory allocated with coreMalloc() must be freed with coreFree().
Expand Down
23 changes: 23 additions & 0 deletions folly/concurrency/test/CacheLocalityTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1237,3 +1237,26 @@ TEST(CoreAllocator, Basic) {
}
mems.clear();
}

TEST(CoreAllocator, MinimumAllocationSize) {
// coreMalloc should handle sizes smaller than sizeof(void*) by rounding up
constexpr size_t kNumStripes = 32;

// Test that small allocations (< 8 bytes) work correctly
// The Allocator class should round these up to 8 bytes
auto res1 = coreMalloc(1, kNumStripes, 0);
EXPECT_NE(nullptr, res1);
memset(res1, 0xFF, 1); // Should not crash
coreFree(res1);

auto res4 = coreMalloc(4, kNumStripes, 0);
EXPECT_NE(nullptr, res4);
memset(res4, 0xFF, 4); // Should not crash
coreFree(res4);

// Verify that 8-byte allocation works (minimum valid size)
auto res8 = coreMalloc(8, kNumStripes, 0);
EXPECT_NE(nullptr, res8);
memset(res8, 0xFF, 8);
coreFree(res8);
}