summaryrefslogtreecommitdiff
path: root/absl/status/status.h
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2023-09-05 10:45:53 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2023-09-05 10:46:39 -0700
commit5c9f72faadaca7250b341b99da358e855a8d902e (patch)
tree068832fb35f3e3622368a3dafb17f3689ddd93bc /absl/status/status.h
parent461f1e49b395700ff4d7b0bb820df49e0f8ba5cb (diff)
Invert the "is inlined" bit of absl::Status
This change makes RepToPointer/PointerToRep have 0 instructions. This makes IsMovedFrom simpler (although this could always have left out the IsInlined check since that bit can never be set on the aligned pointer) In exchange, it makes CodeToInlinedRep slower, but does not inhibit replacing it with a constant. InlinedRepToCode is unaffected. PiperOrigin-RevId: 562826801 Change-Id: I2732f04ab293b773edc2efdec546b3a287b980c2
Diffstat (limited to 'absl/status/status.h')
-rw-r--r--absl/status/status.h23
1 files changed, 13 insertions, 10 deletions
diff --git a/absl/status/status.h b/absl/status/status.h
index 595064c0..2dac2fea 100644
--- a/absl/status/status.h
+++ b/absl/status/status.h
@@ -51,10 +51,15 @@
#ifndef ABSL_STATUS_STATUS_H_
#define ABSL_STATUS_STATUS_H_
+#include <cassert>
+#include <cstdint>
#include <ostream>
#include <string>
#include <utility>
+#include "absl/base/attributes.h"
+#include "absl/base/config.h"
+#include "absl/base/optimization.h"
#include "absl/functional/function_ref.h"
#include "absl/status/internal/status_internal.h"
#include "absl/strings/cord.h"
@@ -644,13 +649,13 @@ class Status final {
std::string ToStringSlow(StatusToStringMode mode) const;
// Status supports two different representations.
- // - When the low bit is off it is an inlined representation.
+ // - When the low bit is set it is an inlined representation.
// It uses the canonical error space, no message or payload.
// The error code is (rep_ >> 2).
// The (rep_ & 2) bit is the "moved from" indicator, used in IsMovedFrom().
- // - When the low bit is on it is an external representation.
+ // - When the low bit is off it is an external representation.
// In this case all the data comes from a heap allocated Rep object.
- // (rep_ - 1) is a status_internal::StatusRep* pointer to that structure.
+ // rep_ is a status_internal::StatusRep* pointer to that structure.
uintptr_t rep_;
};
@@ -839,18 +844,16 @@ inline status_internal::Payloads* Status::GetPayloads() {
return IsInlined(rep_) ? nullptr : RepToPointer(rep_)->payloads.get();
}
-inline bool Status::IsInlined(uintptr_t rep) { return (rep & 1) == 0; }
+inline bool Status::IsInlined(uintptr_t rep) { return (rep & 1) != 0; }
-inline bool Status::IsMovedFrom(uintptr_t rep) {
- return IsInlined(rep) && (rep & 2) != 0;
-}
+inline bool Status::IsMovedFrom(uintptr_t rep) { return (rep & 2) != 0; }
inline uintptr_t Status::MovedFromRep() {
return CodeToInlinedRep(absl::StatusCode::kInternal) | 2;
}
inline uintptr_t Status::CodeToInlinedRep(absl::StatusCode code) {
- return static_cast<uintptr_t>(code) << 2;
+ return (static_cast<uintptr_t>(code) << 2) + 1;
}
inline absl::StatusCode Status::InlinedRepToCode(uintptr_t rep) {
@@ -860,11 +863,11 @@ inline absl::StatusCode Status::InlinedRepToCode(uintptr_t rep) {
inline status_internal::StatusRep* Status::RepToPointer(uintptr_t rep) {
assert(!IsInlined(rep));
- return reinterpret_cast<status_internal::StatusRep*>(rep - 1);
+ return reinterpret_cast<status_internal::StatusRep*>(rep);
}
inline uintptr_t Status::PointerToRep(status_internal::StatusRep* rep) {
- return reinterpret_cast<uintptr_t>(rep) + 1;
+ return reinterpret_cast<uintptr_t>(rep);
}
inline void Status::Ref(uintptr_t rep) {