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.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
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 |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef js_SliceBudget_h |
michael@0 | 8 | #define js_SliceBudget_h |
michael@0 | 9 | |
michael@0 | 10 | #include <stdint.h> |
michael@0 | 11 | |
michael@0 | 12 | namespace js { |
michael@0 | 13 | |
michael@0 | 14 | /* |
michael@0 | 15 | * This class records how much work has been done in a given collection slice, so that |
michael@0 | 16 | * we can return before pausing for too long. Some slices are allowed to run for |
michael@0 | 17 | * unlimited time, and others are bounded. To reduce the number of gettimeofday |
michael@0 | 18 | * calls, we only check the time every 1000 operations. |
michael@0 | 19 | */ |
michael@0 | 20 | struct JS_PUBLIC_API(SliceBudget) |
michael@0 | 21 | { |
michael@0 | 22 | int64_t deadline; /* in microseconds */ |
michael@0 | 23 | intptr_t counter; |
michael@0 | 24 | |
michael@0 | 25 | static const intptr_t CounterReset = 1000; |
michael@0 | 26 | |
michael@0 | 27 | static const int64_t Unlimited = 0; |
michael@0 | 28 | static int64_t TimeBudget(int64_t millis); |
michael@0 | 29 | static int64_t WorkBudget(int64_t work); |
michael@0 | 30 | |
michael@0 | 31 | /* Equivalent to SliceBudget(UnlimitedBudget). */ |
michael@0 | 32 | SliceBudget(); |
michael@0 | 33 | |
michael@0 | 34 | /* Instantiate as SliceBudget(Time/WorkBudget(n)). */ |
michael@0 | 35 | SliceBudget(int64_t budget); |
michael@0 | 36 | |
michael@0 | 37 | void reset() { |
michael@0 | 38 | deadline = INT64_MAX; |
michael@0 | 39 | counter = INTPTR_MAX; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | void step(intptr_t amt = 1) { |
michael@0 | 43 | counter -= amt; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | bool checkOverBudget(); |
michael@0 | 47 | |
michael@0 | 48 | bool isOverBudget() { |
michael@0 | 49 | if (counter >= 0) |
michael@0 | 50 | return false; |
michael@0 | 51 | return checkOverBudget(); |
michael@0 | 52 | } |
michael@0 | 53 | }; |
michael@0 | 54 | |
michael@0 | 55 | } // namespace js |
michael@0 | 56 | |
michael@0 | 57 | #endif /* js_SliceBudget_h */ |