Add MB/KB/Byte conversion macros

This commit is contained in:
Brad Davis 2016-04-19 13:38:27 -07:00
parent e4233f87ec
commit 2689f2cc4a
2 changed files with 42 additions and 0 deletions

View file

@ -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

View file

@ -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 <stdint.h>
#include <functional>
#include "../SharedUtil.h"
#include "../NumericalConstants.h"
template <size_t MS = 1000>
class OnceEvery {
public:
OnceEvery(std::function<void()> 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<void()> _f;
};