Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1722)

Unified Diff: content/browser/streams/stream_registry.cc

Issue 22908008: Limit the total memory usage for Stream instances (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/streams/stream_registry.cc
diff --git a/content/browser/streams/stream_registry.cc b/content/browser/streams/stream_registry.cc
index 39d24b39f5f7b505484a472b67a2a490892a3151..e2750d4f3ac3a2aba892a53fb98694606f6ddd1c 100644
--- a/content/browser/streams/stream_registry.cc
+++ b/content/browser/streams/stream_registry.cc
@@ -8,7 +8,15 @@
namespace content {
-StreamRegistry::StreamRegistry() {
+namespace {
+// The maximum size of memory each StreamRegistry instance is allowed to use
+// for its Stream instances.
+const uint64 kDefaultMaxMemoryUsage = 1024 * 1024 * 1024U; // 1GiB
+}
+
+StreamRegistry::StreamRegistry()
+ : total_memory_usage_(0),
+ max_memory_usage_(kDefaultMaxMemoryUsage) {
}
StreamRegistry::~StreamRegistry() {
@@ -42,7 +50,33 @@ bool StreamRegistry::CloneStream(const GURL& url, const GURL& src_url) {
void StreamRegistry::UnregisterStream(const GURL& url) {
DCHECK(CalledOnValidThread());
+
+ StreamMap::iterator iter = streams_.find(url);
+ if (iter == streams_.end())
+ return;
+
+ total_memory_usage_ -= iter->second->last_total_buffered_bytes();
kinuko 2013/08/14 09:32:23 Can we DCHECK this doesn't make total_memory_usage
tyoshino (SeeGerritForStatus) 2013/08/15 04:15:33 Done.
streams_.erase(url);
}
+bool StreamRegistry::CanIncreaseMemoryUsage(
+ const GURL& url, size_t last_size, size_t new_size) {
+ DCHECK(CalledOnValidThread());
+
+ StreamMap::iterator iter = streams_.find(url);
+ // A Stream must be registered with its parent registry to get memory.
+ if (iter == streams_.end())
+ return false;
+
+ DCHECK_LE(last_size, total_memory_usage_);
+ size_t usage_of_others = total_memory_usage_ - last_size;
+
+ if (std::numeric_limits<size_t>::max() - usage_of_others < new_size ||
+ max_memory_usage_ < usage_of_others + new_size)
+ return false;
+
+ total_memory_usage_ = usage_of_others + new_size;
+ return true;
+}
+
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698