From 4f15ab146d2d2cf2f198ccedb3aea00efbb30af4 Mon Sep 17 00:00:00 2001 From: Paul Bone Date: Mon, 11 Dec 2023 11:01:33 +0000 Subject: [PATCH] Bug 1864828 - pt 1. Move operators to create literals with units into mfbt/ r=glandium Differential Revision: https://phabricator.services.mozilla.com/D194950 --- memory/build/Utils.h | 21 ------------------- memory/build/mozjemalloc.cpp | 1 + memory/gtest/TestJemalloc.cpp | 1 + mfbt/Literals.h | 39 +++++++++++++++++++++++++++++++++++ mfbt/moz.build | 1 + 5 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 mfbt/Literals.h diff --git a/memory/build/Utils.h b/memory/build/Utils.h index 6032bf23d0c6..b1a05e7ae3b0 100644 --- a/memory/build/Utils.h +++ b/memory/build/Utils.h @@ -47,27 +47,6 @@ Order CompareAddr(T* aAddr1, T* aAddr2) { return CompareInt(uintptr_t(aAddr1), uintptr_t(aAddr2)); } -// User-defined literals to make constants more legible -constexpr size_t operator"" _KiB(unsigned long long int aNum) { - return size_t(aNum) * 1024; -} - -constexpr size_t operator"" _KiB(long double aNum) { - return size_t(aNum * 1024); -} - -constexpr size_t operator"" _MiB(unsigned long long int aNum) { - return size_t(aNum) * 1024_KiB; -} - -constexpr size_t operator"" _MiB(long double aNum) { - return size_t(aNum * 1024_KiB); -} - -constexpr double operator""_percent(long double aPercent) { - return double(aPercent) / 100; -} - // Helper for (fast) comparison of fractions without involving divisions or // floats. class Fraction { diff --git a/memory/build/mozjemalloc.cpp b/memory/build/mozjemalloc.cpp index 193dc428a6da..7a8211747fec 100644 --- a/memory/build/mozjemalloc.cpp +++ b/memory/build/mozjemalloc.cpp @@ -150,6 +150,7 @@ #include "mozilla/DoublyLinkedList.h" #include "mozilla/HelperMacros.h" #include "mozilla/Likely.h" +#include "mozilla/Literals.h" #include "mozilla/MathAlgorithms.h" #include "mozilla/RandomNum.h" // Note: MozTaggedAnonymousMmap() could call an LD_PRELOADed mmap diff --git a/memory/gtest/TestJemalloc.cpp b/memory/gtest/TestJemalloc.cpp index a24e9e955fe0..7cb19674ccaf 100644 --- a/memory/gtest/TestJemalloc.cpp +++ b/memory/gtest/TestJemalloc.cpp @@ -4,6 +4,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "mozilla/Literals.h" #include "mozilla/mozalloc.h" #include "mozilla/UniquePtr.h" #include "mozilla/Unused.h" diff --git a/mfbt/Literals.h b/mfbt/Literals.h new file mode 100644 index 000000000000..d1d403afaeb3 --- /dev/null +++ b/mfbt/Literals.h @@ -0,0 +1,39 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/* Helpers for units on integer literals. */ + +#ifndef mozilla_Literals_h +#define mozilla_Literals_h + +#include + +// User-defined literals to make constants more legible. Use them by +// appending them to literals such as: +// +// size_t page_size = 4_KiB; +// +constexpr size_t operator"" _KiB(unsigned long long int aNum) { + return size_t(aNum) * 1024; +} + +constexpr size_t operator"" _KiB(long double aNum) { + return size_t(aNum * 1024); +} + +constexpr size_t operator"" _MiB(unsigned long long int aNum) { + return size_t(aNum) * 1024_KiB; +} + +constexpr size_t operator"" _MiB(long double aNum) { + return size_t(aNum * 1024_KiB); +} + +constexpr double operator""_percent(long double aPercent) { + return double(aPercent) / 100; +} + +#endif /* ! mozilla_Literals_h */ diff --git a/mfbt/moz.build b/mfbt/moz.build index 52e99e77dacd..1571473bfd12 100644 --- a/mfbt/moz.build +++ b/mfbt/moz.build @@ -63,6 +63,7 @@ EXPORTS.mozilla = [ "Latin1.h", "Likely.h", "LinkedList.h", + "Literals.h", "MacroArgs.h", "MacroForEach.h", "MathAlgorithms.h",