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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/streams/stream_registry.h" 5 #include "content/browser/streams/stream_registry.h"
6 6
7 #include "content/browser/streams/stream.h" 7 #include "content/browser/streams/stream.h"
8 8
9 namespace content { 9 namespace content {
10 10
11 StreamRegistry::StreamRegistry() { 11 namespace {
12 // The maximum size of memory each StreamRegistry instance is allowed to use
13 // for its Stream instances.
14 const uint64 kDefaultMaxMemoryUsage = 1024 * 1024 * 1024U; // 1GiB
15 }
16
17 StreamRegistry::StreamRegistry()
18 : total_memory_usage_(0),
19 max_memory_usage_(kDefaultMaxMemoryUsage) {
12 } 20 }
13 21
14 StreamRegistry::~StreamRegistry() { 22 StreamRegistry::~StreamRegistry() {
15 } 23 }
16 24
17 void StreamRegistry::RegisterStream(scoped_refptr<Stream> stream) { 25 void StreamRegistry::RegisterStream(scoped_refptr<Stream> stream) {
18 DCHECK(CalledOnValidThread()); 26 DCHECK(CalledOnValidThread());
19 DCHECK(stream.get()); 27 DCHECK(stream.get());
20 DCHECK(!stream->url().is_empty()); 28 DCHECK(!stream->url().is_empty());
21 streams_[stream->url()] = stream; 29 streams_[stream->url()] = stream;
(...skipping 13 matching lines...) Expand all
35 scoped_refptr<Stream> stream(GetStream(src_url)); 43 scoped_refptr<Stream> stream(GetStream(src_url));
36 if (stream.get()) { 44 if (stream.get()) {
37 streams_[url] = stream; 45 streams_[url] = stream;
38 return true; 46 return true;
39 } 47 }
40 return false; 48 return false;
41 } 49 }
42 50
43 void StreamRegistry::UnregisterStream(const GURL& url) { 51 void StreamRegistry::UnregisterStream(const GURL& url) {
44 DCHECK(CalledOnValidThread()); 52 DCHECK(CalledOnValidThread());
53
54 StreamMap::iterator iter = streams_.find(url);
55 if (iter == streams_.end())
56 return;
57
58 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.
45 streams_.erase(url); 59 streams_.erase(url);
46 } 60 }
47 61
62 bool StreamRegistry::CanIncreaseMemoryUsage(
63 const GURL& url, size_t last_size, size_t new_size) {
64 DCHECK(CalledOnValidThread());
65
66 StreamMap::iterator iter = streams_.find(url);
67 // A Stream must be registered with its parent registry to get memory.
68 if (iter == streams_.end())
69 return false;
70
71 DCHECK_LE(last_size, total_memory_usage_);
72 size_t usage_of_others = total_memory_usage_ - last_size;
73
74 if (std::numeric_limits<size_t>::max() - usage_of_others < new_size ||
75 max_memory_usage_ < usage_of_others + new_size)
76 return false;
77
78 total_memory_usage_ = usage_of_others + new_size;
79 return true;
80 }
81
48 } // namespace content 82 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698