Skip to content

Commit

Permalink
Annotation page/length comparisons and helpers as const.
Browse files Browse the repository at this point in the history
These do not depend on observable state of the program, other than their
parameters.

PiperOrigin-RevId: 346169103
Change-Id: Ie918dfc0d823bc77443c5f6caae642cc2502346e
  • Loading branch information
ckennelly authored and copybara-github committed Dec 7, 2020
1 parent 33ff976 commit bb86c5b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tcmalloc/huge_pages.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "tcmalloc/common.h"
#include "tcmalloc/internal/logging.h"
#include "tcmalloc/internal/optimization.h"
#include "tcmalloc/pages.h"

namespace tcmalloc {
Expand Down Expand Up @@ -83,13 +84,16 @@ struct HugeLength {

// Literal constructors (made explicit to avoid accidental uses when
// another unit was meant.)
TCMALLOC_ATTRIBUTE_CONST
inline constexpr HugeLength NHugePages(size_t n) { return HugeLength(n); }

TCMALLOC_ATTRIBUTE_CONST
inline constexpr HugeLength HLFromBytes(size_t bytes) {
return NHugePages(bytes / kHugePageSize);
}

// Rounds *up* to the nearest hugepage.
TCMALLOC_ATTRIBUTE_CONST
inline constexpr HugeLength HLFromPages(Length pages) {
return NHugePages((pages + kPagesPerHugePage - Length(1)) /
kPagesPerHugePage);
Expand All @@ -116,58 +120,72 @@ inline constexpr bool operator<(HugeLength lhs, HugeLength rhs) {
return lhs.n < rhs.n;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator>(HugeLength lhs, HugeLength rhs) {
return lhs.n > rhs.n;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator<=(HugeLength lhs, HugeLength rhs) {
return lhs.n <= rhs.n;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator<(HugePage lhs, HugePage rhs) {
return lhs.pn < rhs.pn;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator>(HugePage lhs, HugePage rhs) {
return lhs.pn > rhs.pn;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator>=(HugeLength lhs, HugeLength rhs) {
return lhs.n >= rhs.n;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator<=(HugePage lhs, HugePage rhs) {
return lhs.pn <= rhs.pn;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator>=(HugePage lhs, HugePage rhs) {
return lhs.pn >= rhs.pn;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator==(HugePage lhs, HugePage rhs) {
return lhs.pn == rhs.pn;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator!=(HugePage lhs, HugePage rhs) {
return !(lhs == rhs);
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator==(HugeLength lhs, HugeLength rhs) {
return lhs.n == rhs.n;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator!=(HugeLength lhs, HugeLength rhs) {
return lhs.n != rhs.n;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr size_t operator/(HugeLength lhs, HugeLength rhs) {
return lhs.n / rhs.n;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr HugeLength operator*(HugeLength lhs, size_t rhs) {
return NHugePages(lhs.n * rhs);
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr HugeLength operator/(HugeLength lhs, size_t rhs) {
return NHugePages(lhs.n / rhs);
}
Expand All @@ -177,23 +195,28 @@ inline HugeLength &operator*=(HugeLength &lhs, size_t rhs) {
return lhs;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr HugeLength operator%(HugeLength lhs, HugeLength rhs) {
return NHugePages(lhs.n % rhs.n);
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr HugePage operator+(HugePage lhs, HugeLength rhs) {
ASSERT(lhs.pn + rhs.n <= HugePage::kMaxPageNumber);
return HugePage{lhs.pn + rhs.n};
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr HugePage operator+(HugeLength lhs, HugePage rhs) {
return rhs + lhs;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr HugePage operator-(HugePage lhs, HugeLength rhs) {
return ASSERT(lhs.pn >= rhs.n), HugePage{lhs.pn - rhs.n};
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr HugeLength operator-(HugePage lhs, HugePage rhs) {
return ASSERT(lhs.pn >= rhs.pn), NHugePages(lhs.pn - rhs.pn);
}
Expand All @@ -204,6 +227,7 @@ inline HugePage &operator+=(HugePage &lhs, HugeLength rhs) {
return lhs;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr HugeLength operator+(HugeLength lhs, HugeLength rhs) {
return NHugePages(lhs.n + rhs.n);
}
Expand All @@ -213,6 +237,7 @@ inline HugeLength &operator+=(HugeLength &lhs, HugeLength rhs) {
return lhs;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr HugeLength operator-(HugeLength lhs, HugeLength rhs) {
return ASSERT(lhs.n >= rhs.n), NHugePages(lhs.n - rhs.n);
}
Expand All @@ -231,10 +256,12 @@ inline void PrintTo(const HugeLength &n, ::std::ostream *os) {
*os << n.raw_num() << "hps";
}

TCMALLOC_ATTRIBUTE_CONST
inline HugePage HugePageContaining(PageId p) {
return {p.index() >> (kHugePageShift - kPageShift)};
}

TCMALLOC_ATTRIBUTE_CONST
inline HugePage HugePageContaining(void *p) {
return HugePageContaining(PageIdContaining(p));
}
Expand Down
8 changes: 8 additions & 0 deletions tcmalloc/internal/optimization.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@
#endif
#endif

// Annotations for functions that are not affected by nor affect observable
// state of the program.
#if ABSL_HAVE_ATTRIBUTE(const)
#define TCMALLOC_ATTRIBUTE_CONST __attribute__((const))
#else
#define TCMALLOC_ATTRIBUTE_CONST
#endif

#endif // TCMALLOC_INTERNAL_OPTIMIZATION_H_
28 changes: 28 additions & 0 deletions tcmalloc/pages.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "absl/strings/string_view.h"
#include "tcmalloc/common.h"
#include "tcmalloc/internal/logging.h"
#include "tcmalloc/internal/optimization.h"

namespace tcmalloc {
namespace tcmalloc_internal {
Expand Down Expand Up @@ -142,17 +143,20 @@ class PageId {
uintptr_t pn_;
};

TCMALLOC_ATTRIBUTE_CONST
inline constexpr Length LengthFromBytes(size_t bytes) {
return Length(bytes >> kPageShift);
}

// Convert byte size into pages. This won't overflow, but may return
// an unreasonably large value if bytes is huge enough.
TCMALLOC_ATTRIBUTE_CONST
inline constexpr Length BytesToLengthCeil(size_t bytes) {
return Length((bytes >> kPageShift) +
((bytes & (kPageSize - 1)) > 0 ? 1 : 0));
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr Length BytesToLengthFloor(size_t bytes) {
return Length(bytes >> kPageShift);
}
Expand All @@ -165,65 +169,82 @@ inline PageId& operator++(PageId& p) { // NOLINT(runtime/references)
return p += Length(1);
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator<(PageId lhs, PageId rhs) {
return lhs.pn_ < rhs.pn_;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator>(PageId lhs, PageId rhs) {
return lhs.pn_ > rhs.pn_;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator<=(PageId lhs, PageId rhs) {
return lhs.pn_ <= rhs.pn_;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator>=(PageId lhs, PageId rhs) {
return lhs.pn_ >= rhs.pn_;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator==(PageId lhs, PageId rhs) {
return lhs.pn_ == rhs.pn_;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator!=(PageId lhs, PageId rhs) {
return lhs.pn_ != rhs.pn_;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr PageId operator+(PageId lhs, Length rhs) { return lhs += rhs; }

TCMALLOC_ATTRIBUTE_CONST
inline constexpr PageId operator+(Length lhs, PageId rhs) { return rhs += lhs; }

TCMALLOC_ATTRIBUTE_CONST
inline constexpr PageId operator-(PageId lhs, Length rhs) { return lhs -= rhs; }

TCMALLOC_ATTRIBUTE_CONST
inline constexpr Length operator-(PageId lhs, PageId rhs) {
ASSERT(lhs.pn_ >= rhs.pn_);
return Length(lhs.pn_ - rhs.pn_);
}

TCMALLOC_ATTRIBUTE_CONST
inline PageId PageIdContaining(const void* p) {
return PageId(reinterpret_cast<uintptr_t>(p) >> kPageShift);
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator<(Length lhs, Length rhs) {
return lhs.n_ < rhs.n_;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator>(Length lhs, Length rhs) {
return lhs.n_ > rhs.n_;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator<=(Length lhs, Length rhs) {
return lhs.n_ <= rhs.n_;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator>=(Length lhs, Length rhs) {
return lhs.n_ >= rhs.n_;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator==(Length lhs, Length rhs) {
return lhs.n_ == rhs.n_;
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr bool operator!=(Length lhs, Length rhs) {
return lhs.n_ != rhs.n_;
}
Expand All @@ -232,31 +253,38 @@ inline Length& operator++(Length& l) { return l += Length(1); }

inline Length& operator--(Length& l) { return l -= Length(1); }

TCMALLOC_ATTRIBUTE_CONST
inline constexpr Length operator+(Length lhs, Length rhs) {
return Length(lhs.raw_num() + rhs.raw_num());
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr Length operator-(Length lhs, Length rhs) {
return Length(lhs.raw_num() - rhs.raw_num());
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr Length operator*(Length lhs, size_t rhs) {
return Length(lhs.raw_num() * rhs);
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr Length operator*(size_t lhs, Length rhs) {
return Length(lhs * rhs.raw_num());
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr size_t operator/(Length lhs, Length rhs) {
return lhs.raw_num() / rhs.raw_num();
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr Length operator/(Length lhs, size_t rhs) {
ASSERT(rhs != 0);
return Length(lhs.raw_num() / rhs);
}

TCMALLOC_ATTRIBUTE_CONST
inline constexpr Length operator%(Length lhs, Length rhs) {
ASSERT(rhs.raw_num() != 0);
return Length(lhs.raw_num() % rhs.raw_num());
Expand Down

0 comments on commit bb86c5b

Please sign in to comment.