From 2689f2cc4ae188a5fa3f5b65208798f516653949 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Tue, 19 Apr 2016 13:38:27 -0700 Subject: [PATCH] Add MB/KB/Byte conversion macros --- libraries/shared/src/NumericalConstants.h | 9 +++++++ libraries/shared/src/shared/OnceEvery.h | 33 +++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 libraries/shared/src/shared/OnceEvery.h diff --git a/libraries/shared/src/NumericalConstants.h b/libraries/shared/src/NumericalConstants.h index 6b37031d23..cf78f6dbf3 100644 --- a/libraries/shared/src/NumericalConstants.h +++ b/libraries/shared/src/NumericalConstants.h @@ -44,4 +44,13 @@ const int BYTES_PER_KILOBYTE = 1000; const int BYTES_PER_KILOBIT = BYTES_PER_KILOBYTE / BITS_IN_BYTE; const int KILO_PER_MEGA = 1000; +#define KB_TO_BYTES_SHIFT 10 +#define MB_TO_BYTES_SHIFT 20 + +#define MB_TO_BYTES(X) ((size_t)(X) << MB_TO_BYTES_SHIFT) +#define KB_TO_BYTES(X) ((size_t)(X) << KB_TO_BYTES_SHIFT) + +#define BYTES_TO_MB(X) (X >> MB_TO_BYTES_SHIFT) +#define BYTES_TO_KB(X) (X >> KB_TO_BYTES_SHIFT) + #endif // hifi_NumericalConstants_h diff --git a/libraries/shared/src/shared/OnceEvery.h b/libraries/shared/src/shared/OnceEvery.h new file mode 100644 index 0000000000..36531413ec --- /dev/null +++ b/libraries/shared/src/shared/OnceEvery.h @@ -0,0 +1,33 @@ +// +// Created by Bradley Austin Davis on 2016/04/19 +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include +#include + +#include "../SharedUtil.h" +#include "../NumericalConstants.h" + +template +class OnceEvery { +public: + OnceEvery(std::function f) : _f(f) { } + + bool maybeExecute() { + uint64_t now = usecTimestampNow(); + if ((now - _lastRun) > (MS * USECS_PER_MSEC)) { + _f(); + _lastRun = now; + return true; + } + return false; + } + +private: + uint64_t _lastRun { 0 }; + std::function _f; +};