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
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,
6161void 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
355356void * 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
523525void 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}
0 commit comments