media/mtransport/simpletokenbucket.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /* Original author: bcampen@mozilla.com */
michael@0 8
michael@0 9 #include "simpletokenbucket.h"
michael@0 10
michael@0 11 #include <stdint.h>
michael@0 12
michael@0 13 #include "prinrval.h"
michael@0 14
michael@0 15 namespace mozilla {
michael@0 16
michael@0 17 SimpleTokenBucket::SimpleTokenBucket(size_t bucket_size,
michael@0 18 size_t tokens_per_second) :
michael@0 19 max_tokens_(bucket_size),
michael@0 20 num_tokens_(bucket_size),
michael@0 21 tokens_per_second_(tokens_per_second),
michael@0 22 last_time_tokens_added_(PR_IntervalNow()) {
michael@0 23 }
michael@0 24
michael@0 25 size_t SimpleTokenBucket::getTokens(size_t num_requested_tokens) {
michael@0 26 // Only fill if there isn't enough to satisfy the request.
michael@0 27 // If we get tokens so seldomly that we are able to roll the timer all
michael@0 28 // the way around its range, then we lose that entire range of time
michael@0 29 // for token accumulation. Probably not the end of the world.
michael@0 30 if (num_requested_tokens > num_tokens_) {
michael@0 31 PRIntervalTime now = PR_IntervalNow();
michael@0 32
michael@0 33 // If we roll over the max, since everything in this calculation is the same
michael@0 34 // unsigned type, this will still yield the elapsed time (unless we've
michael@0 35 // wrapped more than once).
michael@0 36 PRIntervalTime elapsed_ticks = now - last_time_tokens_added_;
michael@0 37
michael@0 38 uint32_t elapsed_milli_sec = PR_IntervalToMilliseconds(elapsed_ticks);
michael@0 39 size_t tokens_to_add = (elapsed_milli_sec * tokens_per_second_)/1000;
michael@0 40
michael@0 41 // Only update our timestamp if we added some tokens
michael@0 42 // TODO:(bcampen@mozilla.com) Should we attempt to "save" leftover time?
michael@0 43 if (tokens_to_add) {
michael@0 44 num_tokens_ += tokens_to_add;
michael@0 45 if (num_tokens_ > max_tokens_) {
michael@0 46 num_tokens_ = max_tokens_;
michael@0 47 }
michael@0 48
michael@0 49 last_time_tokens_added_ = now;
michael@0 50 }
michael@0 51
michael@0 52 if (num_requested_tokens > num_tokens_) {
michael@0 53 return num_tokens_;
michael@0 54 }
michael@0 55 }
michael@0 56
michael@0 57 num_tokens_ -= num_requested_tokens;
michael@0 58 return num_requested_tokens;
michael@0 59 }
michael@0 60
michael@0 61 } // namespace mozilla
michael@0 62

mercurial