Tue, 06 Jan 2015 21:39:09 +0100
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.
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Original author: bcampen@mozilla.com */
9 /*
10 * This file defines a dirt-simple token bucket class.
11 */
13 #ifndef simpletokenbucket_h__
14 #define simpletokenbucket_h__
16 #include <stdint.h>
18 #include "prinrval.h"
20 #include "m_cpp_utils.h"
22 namespace mozilla {
24 class SimpleTokenBucket {
25 public:
26 /*
27 * Create a SimpleTokenBucket with a given maximum size and
28 * token replenishment rate.
29 * (eg; if you want a maximum rate of 5 per second over a 7 second
30 * period, call SimpleTokenBucket b(5*7, 5);)
31 */
32 SimpleTokenBucket(size_t bucket_size, size_t tokens_per_second);
34 /*
35 * Attempt to acquire a number of tokens. If successful, returns
36 * |num_tokens|, otherwise returns the number of tokens currently
37 * in the bucket.
38 * Note: To get the number of tokens in the bucket, pass something
39 * like UINT32_MAX.
40 */
41 size_t getTokens(size_t num_tokens);
43 protected: // Allow testing to touch these.
44 uint64_t max_tokens_;
45 uint64_t num_tokens_;
46 size_t tokens_per_second_;
47 PRIntervalTime last_time_tokens_added_;
49 DISALLOW_COPY_ASSIGN(SimpleTokenBucket);
50 };
51 } // namespace mozilla
53 #endif // simpletokenbucket_h__