Skip to content

Commit bf833c8

Browse files
r-barnesmeta-codesync[bot]
authored andcommitted
folly->format -> fmt::format in fbcode/cachelib/allocator/memory
Summary: folly::format is deprecated in favor of fmt::format. This diff replaces all instances of folly::format with fmt::format. - If you approve of this diff, please use the "Accept & Ship" button :-) ___ overriding_review_checks_triggers_an_audit_and_retroactive_review Oncall Short Name: algopt Differential Revision: D104748915 fbshipit-source-id: b1751b32ad006b219b14c44d94a0bff8bb6fe9d2
1 parent 5a11067 commit bf833c8

6 files changed

Lines changed: 118 additions & 114 deletions

File tree

cachelib/allocator/memory/AllocationClass.cpp

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "cachelib/allocator/memory/AllocationClass.h"
1818

19+
#include <fmt/core.h>
1920
#include <folly/Try.h>
2021
#include <folly/logging/xlog.h>
2122

@@ -25,7 +26,6 @@
2526

2627
#pragma GCC diagnostic push
2728
#pragma GCC diagnostic ignored "-Wconversion"
28-
#include <folly/Format.h>
2929
#include <folly/Random.h>
3030
#pragma GCC diagnostic pop
3131

@@ -61,35 +61,35 @@ AllocationClass::AllocationClass(ClassId classId,
6161
void AllocationClass::checkState() const {
6262
// classId_ and allocationSize_ must be valid.
6363
if (classId_ < 0) {
64-
throw std::invalid_argument(
65-
folly::sformat("Invalid Class Id {}", classId_));
64+
throw std::invalid_argument(fmt::format("Invalid Class Id {}", classId_));
6665
}
6766

6867
// Check size against FreeAlloc to ensure that we have sufficient memory
6968
// for the intrusive list's hook.
7069
if (allocationSize_ < sizeof(FreeAlloc) || allocationSize_ > Slab::kSize) {
7170
throw std::invalid_argument(
72-
folly::sformat("Invalid alloc size {}", allocationSize_));
71+
fmt::format("Invalid alloc size {}", allocationSize_));
7372
}
7473

7574
const auto header = slabAlloc_.getSlabHeader(currSlab_);
7675
if (currSlab_ != nullptr && header == nullptr) {
77-
throw std::invalid_argument(folly::sformat(
78-
"Could not locate header for our current slab {}", currSlab_));
76+
throw std::invalid_argument(
77+
fmt::format("Could not locate header for our current slab {}",
78+
fmt::ptr(currSlab_)));
7979
}
8080

8181
if (header != nullptr && header->classId != classId_) {
82-
throw std::invalid_argument(folly::sformat(
83-
"ClassId of currSlab {} is not the same as our classId {}",
84-
header->classId, classId_));
82+
throw std::invalid_argument(
83+
fmt::format("ClassId of currSlab {} is not the same as our classId {}",
84+
header->classId, classId_));
8585
}
8686

8787
if (currSlab_ != nullptr &&
8888
std::find(allocatedSlabs_.begin(), allocatedSlabs_.end(), currSlab_) ==
8989
allocatedSlabs_.end()) {
90-
throw std::invalid_argument(folly::sformat(
91-
"Current allocation slab {} is not in allocated slabs list",
92-
currSlab_));
90+
throw std::invalid_argument(
91+
fmt::format("Current allocation slab {} is not in allocated slabs list",
92+
fmt::ptr(currSlab_)));
9393
}
9494
}
9595

@@ -252,8 +252,8 @@ SlabReleaseContext AllocationClass::startSlabRelease(
252252
LockHolder startSlabReleaseLockHolder(startSlabReleaseLock_);
253253
const auto* hintSlab = slabAlloc_.getSlabForMemory(hint);
254254
if (hint != nullptr && !slabAlloc_.isValidSlab(hintSlab)) {
255-
throw std::invalid_argument(
256-
folly::sformat("Invalid hint {} for slab release {}", hint, hintSlab));
255+
throw std::invalid_argument(fmt::format(
256+
"Invalid hint {} for slab release {}", hint, fmt::ptr(hintSlab)));
257257
}
258258

259259
const Slab* slab;
@@ -270,10 +270,11 @@ SlabReleaseContext AllocationClass::startSlabRelease(
270270
// slab header must be valid and NOT marked for release
271271
if (header == nullptr || header->classId != getId() ||
272272
header->poolId != getPoolId() || header->isMarkedForRelease()) {
273-
throw std::invalid_argument(folly::sformat(
273+
throw std::invalid_argument(fmt::format(
274274
"Slab Header {} is in invalid state for release. id = {}, "
275275
"markedForRelease = {}, classId = {}",
276-
header, header == nullptr ? Slab::kInvalidClassId : header->classId,
276+
fmt::ptr(header),
277+
header == nullptr ? Slab::kInvalidClassId : header->classId,
277278
header == nullptr ? false : header->isMarkedForRelease(), getId()));
278279
}
279280

@@ -300,9 +301,9 @@ SlabReleaseContext AllocationClass::startSlabRelease(
300301
// error, return to caller. This should not happen. throw a run time
301302
// error.
302303
throw std::runtime_error(
303-
folly::sformat("Slab {} belongs to class {}. But its not present in "
304-
"the free list or allocated list.",
305-
slab, getId()));
304+
fmt::format("Slab {} belongs to class {}. But its not present in "
305+
"the free list or allocated list.",
306+
fmt::ptr(slab), getId()));
306307
}
307308
*allocIt = allocatedSlabs_.back();
308309
allocatedSlabs_.pop_back();
@@ -329,9 +330,9 @@ SlabReleaseContext AllocationClass::startSlabRelease(
329330
slabReleaseAllocMap_.erase(getSlabPtrValue(slab));
330331
});
331332
throw exception::SlabReleaseAborted(
332-
folly::sformat("Slab Release aborted "
333-
"during pruning free allocs. Slab address: {}",
334-
slab));
333+
fmt::format("Slab Release aborted "
334+
"during pruning free allocs. Slab address: {}",
335+
fmt::ptr(slab)));
335336
}
336337
std::vector<void*> activeAllocations = std::move(results.second);
337338
return lock_->lock_combine([&]() {
@@ -354,7 +355,7 @@ SlabReleaseContext AllocationClass::startSlabRelease(
354355

355356
void* AllocationClass::getAllocForIdx(const Slab* slab, size_t idx) const {
356357
if (idx >= getAllocsPerSlab()) {
357-
throw std::invalid_argument(folly::sformat("Invalid index {}", idx));
358+
throw std::invalid_argument(fmt::format("Invalid index {}", idx));
358359
}
359360
return slab->memoryAtOffset(idx * allocationSize_);
360361
}
@@ -494,8 +495,9 @@ bool AllocationClass::allFreed(const Slab* slab) const {
494495
return lock_->lock_combine([this, slab]() {
495496
const auto it = slabReleaseAllocMap_.find(getSlabPtrValue(slab));
496497
if (it == slabReleaseAllocMap_.end()) {
497-
throw std::runtime_error(folly::sformat(
498-
"Slab {} is not in the active slab release allocation map.", slab));
498+
throw std::runtime_error(fmt::format(
499+
"Slab {} is not in the active slab release allocation map.",
500+
fmt::ptr(slab)));
499501
}
500502

501503
const auto& allocState = it->second;
@@ -522,7 +524,7 @@ void AllocationClass::waitUntilAllFreed(const Slab* slab) {
522524

523525
void AllocationClass::abortSlabRelease(const SlabReleaseContext& context) {
524526
if (context.isReleased()) {
525-
throw std::invalid_argument(folly::sformat("context is already released"));
527+
throw std::invalid_argument(fmt::format("context is already released"));
526528
}
527529
auto slab = context.getSlab();
528530
const auto slabPtrVal = getSlabPtrValue(slab);
@@ -567,8 +569,9 @@ void AllocationClass::completeSlabRelease(const SlabReleaseContext& context) {
567569
// slab header must be valid and marked for release
568570
if (header == nullptr || header->classId != getId() ||
569571
!header->isMarkedForRelease()) {
570-
throw std::runtime_error(folly::sformat(
571-
"The slab at {} with header at {} is invalid", slab, header));
572+
throw std::runtime_error(
573+
fmt::format("The slab at {} with header at {} is invalid",
574+
fmt::ptr(slab), fmt::ptr(header)));
572575
}
573576
});
574577

@@ -595,23 +598,23 @@ void AllocationClass::checkSlabInRelease(const SlabReleaseContext& ctx,
595598
const void* memory) const {
596599
const auto* header = slabAlloc_.getSlabHeader(memory);
597600
if (header == nullptr || header->classId != classId_) {
598-
throw std::invalid_argument(folly::sformat(
601+
throw std::invalid_argument(fmt::format(
599602
"trying to check memory {} (with ClassId {}), not belonging to this "
600603
"AllocationClass (ClassID {})",
601604
memory,
602605
header ? header->classId : Slab::kInvalidClassId,
603606
classId_));
604607
}
605608
if (!header->isMarkedForRelease()) {
606-
throw std::invalid_argument(folly::sformat(
609+
throw std::invalid_argument(fmt::format(
607610
"trying whether memory at {} (with ClassID {}) is freed, but header is "
608611
"not marked for release",
609612
memory,
610613
header->classId));
611614
}
612615
const auto* slab = slabAlloc_.getSlabForMemory(memory);
613616
if (slab != ctx.getSlab()) {
614-
throw std::invalid_argument(folly::sformat(
617+
throw std::invalid_argument(fmt::format(
615618
"trying to check memory {} (with ClassId {}), against an invalid slab "
616619
"release context (ClassID {})",
617620
memory,
@@ -629,9 +632,9 @@ bool AllocationClass::isAllocFreedLocked(const SlabReleaseContext& /*ctx*/,
629632
// this should not happen
630633
if (it == slabReleaseAllocMap_.end()) {
631634
throw std::runtime_error(
632-
folly::sformat("Invalid slabReleaseAllocMap "
633-
"state when checking if memory is freed. Memory: {}",
634-
memory));
635+
fmt::format("Invalid slabReleaseAllocMap "
636+
"state when checking if memory is freed. Memory: {}",
637+
memory));
635638
}
636639

637640
const auto& allocState = it->second;
@@ -662,7 +665,7 @@ void AllocationClass::free(void* memory) {
662665
const auto* header = slabAlloc_.getSlabHeader(memory);
663666
auto* slab = slabAlloc_.getSlabForMemory(memory);
664667
if (header == nullptr || header->classId != classId_) {
665-
throw std::invalid_argument(folly::sformat(
668+
throw std::invalid_argument(fmt::format(
666669
"trying to free memory {} (with ClassId {}), not belonging to this "
667670
"AllocationClass (ClassId {})",
668671
memory, header ? header->classId : Slab::kInvalidClassId, classId_));
@@ -676,7 +679,7 @@ void AllocationClass::free(void* memory) {
676679

677680
// this should not happen.
678681
if (it == slabReleaseAllocMap_.end()) {
679-
throw std::runtime_error(folly::sformat(
682+
throw std::runtime_error(fmt::format(
680683
"Invalid slabReleaseAllocMap "
681684
"state when attempting to free an allocation. Memory: {}",
682685
memory));
@@ -686,7 +689,7 @@ void AllocationClass::free(void* memory) {
686689
const auto idx = getAllocIdx(slab, memory);
687690
if (allocState[idx]) {
688691
throw std::invalid_argument(
689-
folly::sformat("Allocation {} is already marked as free", memory));
692+
fmt::format("Allocation {} is already marked as free", memory));
690693
}
691694
slabAlloc_.asanPoisonMemoryRegion(memory, allocationSize_);
692695
allocState[idx] = true;
@@ -750,8 +753,8 @@ AllocationClass::createSlabReleaseAllocMapLocked(const Slab* slab) {
750753
slabReleaseAllocMap_.insert({slabPtrVal, std::move(allocState)});
751754
if (!res.second) {
752755
// this should never happen. we must always be able to insert.
753-
throw std::runtime_error(
754-
folly::sformat("failed to insert allocState map for slab {}", slab));
756+
throw std::runtime_error(fmt::format(
757+
"failed to insert allocState map for slab {}", fmt::ptr(slab)));
755758
}
756759
return res.first;
757760
}

cachelib/allocator/memory/MemoryAllocator.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#include "cachelib/allocator/memory/MemoryAllocator.h"
1818

19-
#include <folly/Format.h>
19+
#include <fmt/core.h>
2020

2121
#include <algorithm>
2222
#include <utility>
@@ -42,10 +42,10 @@ std::pair<uint32_t, uint32_t> validateAndClampItemSizeRange(
4242
}
4343
if (maxItemSize > Slab::kSize) {
4444
throw std::invalid_argument(
45-
folly::sformat("maxItemSize must be <= {} because allocation size "
46-
"must be <= {} (Slab::kSize)",
47-
Slab::kSize,
48-
Slab::kSize));
45+
fmt::format("maxItemSize must be <= {} because allocation size "
46+
"must be <= {} (Slab::kSize)",
47+
Slab::kSize,
48+
Slab::kSize));
4949
}
5050

5151
// Handling items smaller than Slab::kMinAllocSize as Slab::kMinAllocSize
@@ -127,7 +127,7 @@ PoolId MemoryAllocator::addPool(folly::StringPiece name,
127127

128128
if (ensureProvisionable &&
129129
cachelib::Slab::kSize * poolAllocSizes.size() > size) {
130-
throw std::invalid_argument(folly::sformat(
130+
throw std::invalid_argument(fmt::format(
131131
"Pool {} cannot have at least one slab for each allocation class. "
132132
"{} bytes required, {} bytes given.",
133133
name,
@@ -231,12 +231,12 @@ std::set<uint32_t> MemoryAllocator::generateAllocSizes(
231231
bool reduceFragmentation) {
232232
if (maxSize > Slab::kSize) {
233233
throw std::invalid_argument(
234-
folly::sformat("maximum alloc size {} is more than the slab size {}",
235-
maxSize, Slab::kSize));
234+
fmt::format("maximum alloc size {} is more than the slab size {}",
235+
maxSize, Slab::kSize));
236236
}
237237

238238
if (factor <= 1.0) {
239-
throw std::invalid_argument(folly::sformat("invalid factor {}", factor));
239+
throw std::invalid_argument(fmt::format("invalid factor {}", factor));
240240
}
241241

242242
// Returns the next chunk size. Uses the previous size and factor to select a
@@ -254,7 +254,7 @@ std::set<uint32_t> MemoryAllocator::generateAllocSizes(
254254
kAlignment);
255255
if (newSize == tmpPrevSize) {
256256
throw std::invalid_argument(
257-
folly::sformat("invalid incFactor {}", incFactor));
257+
fmt::format("invalid incFactor {}", incFactor));
258258
}
259259

260260
if (newSize > Slab::kSize) {
@@ -286,7 +286,7 @@ std::set<uint32_t> MemoryAllocator::generateAllocSizes(
286286
kAlignment);
287287
if (prevSize == size) {
288288
throw std::invalid_argument(
289-
folly::sformat("invalid incFactor {}", factor));
289+
fmt::format("invalid incFactor {}", factor));
290290
}
291291
}
292292
}
@@ -337,10 +337,10 @@ std::set<uint32_t> MemoryAllocator::generateOptimalAllocSizesForItemRange(
337337
allocSizes.insert(alignedSize);
338338
if (allocSizes.size() > kMaxClasses) {
339339
throw std::runtime_error(
340-
folly::sformat("Optimal number of allocations required is at least "
341-
"{} which is more than the "
342-
"maximum number of allocation classes allowed {}",
343-
allocSizes.size(), kMaxClasses));
340+
fmt::format("Optimal number of allocations required is at least "
341+
"{} which is more than the "
342+
"maximum number of allocation classes allowed {}",
343+
allocSizes.size(), kMaxClasses));
344344
}
345345
}
346346
XDCHECK_LE(maxItemSize, *allocSizes.rbegin());
@@ -358,8 +358,8 @@ std::set<uint32_t> MemoryAllocator::generateEvenlyDistributedAllocSizes(
358358

359359
if (numClassesToAdd > kMaxClasses) {
360360
throw std::invalid_argument(
361-
folly::sformat("numClassesToAdd {} exceeds maximum allowed {}",
362-
numClassesToAdd, kMaxClasses));
361+
fmt::format("numClassesToAdd {} exceeds maximum allowed {}",
362+
numClassesToAdd, kMaxClasses));
363363
}
364364

365365
std::set<uint32_t> allocSizes;

0 commit comments

Comments
 (0)