void AllocationClass::checkState()

in cachelib/allocator/memory/AllocationClass.cpp [57:90]


void AllocationClass::checkState() const {
  // classId_ and allocationSize_ must be valid.
  if (classId_ < 0) {
    throw std::invalid_argument(
        folly::sformat("Invalid Class Id {}", classId_));
  }

  // Check size against FreeAlloc to ensure that we have sufficient memory
  // for the intrusive list's hook.
  if (allocationSize_ < sizeof(FreeAlloc) || allocationSize_ > Slab::kSize) {
    throw std::invalid_argument(
        folly::sformat("Invalid alloc size {}", allocationSize_));
  }

  const auto header = slabAlloc_.getSlabHeader(currSlab_);
  if (currSlab_ != nullptr && header == nullptr) {
    throw std::invalid_argument(folly::sformat(
        "Could not locate header for our current slab {}", currSlab_));
  }

  if (header != nullptr && header->classId != classId_) {
    throw std::invalid_argument(folly::sformat(
        "ClassId of currSlab {} is not the same as our classId {}",
        header->classId, classId_));
  }

  if (currSlab_ != nullptr &&
      std::find(allocatedSlabs_.begin(), allocatedSlabs_.end(), currSlab_) ==
          allocatedSlabs_.end()) {
    throw std::invalid_argument(folly::sformat(
        "Current allocation slab {} is not in allocated slabs list",
        currSlab_));
  }
}