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

Side by Side Diff: chrome/renderer/net/render_dns_queue.cc

Issue 2866026: Rename Dns prefetching files to Predictor files... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 5 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
(Empty)
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // See header file for description of DnsQueue class
6
7 #include "chrome/renderer/net/render_dns_queue.h"
8
9 #include "base/logging.h"
10 #include "base/stats_counters.h"
11
12 DnsQueue::DnsQueue(BufferSize size)
13 : buffer_(new char[size + 2]),
14 buffer_size_(size + 1),
15 buffer_sentinel_(size + 1),
16 size_(0) {
17 CHECK(0 < static_cast<BufferSize>(size + 3)); // Avoid overflow worries.
18 buffer_[buffer_sentinel_] = '\0'; // Guard byte to help reading data.
19 readable_ = writeable_ = 0; // Buffer starts empty.
20 }
21
22 DnsQueue::~DnsQueue(void) {
23 }
24
25 // Push takes an unterminated string plus its length.
26 // The string must not contain a null terminator.
27 // Exactly length chars are written, or nothing is written.
28 // Returns true for success, false there was no room to push.
29 DnsQueue::PushResult DnsQueue::Push(const char* source,
30 const size_t unsigned_length) {
31 BufferSize length = static_cast<BufferSize>(unsigned_length);
32 if (0 > length+1) // Avoid overflows in conversion to signed.
33 return OVERFLOW_PUSH;
34
35 // To save on sites with a LOT of links to the SAME domain, we have a
36 // a compaction hack that removes duplicates when we try to push() a
37 // match with the last push.
38 if (0 < size_ && readable_ + length < buffer_sentinel_ &&
39 0 == strncmp(source, &buffer_[readable_], unsigned_length) &&
40 '\0' == buffer_[readable_ + unsigned_length]) {
41 SIMPLE_STATS_COUNTER("DNS.PrefetchDnsRedundantPush");
42
43 // We already wrote this name to the queue, so we'll skip this repeat.
44 return REDUNDANT_PUSH;
45 }
46
47 // Calling convention precludes nulls.
48 DCHECK(!length || '\0' != source[length - 1]);
49
50 DCHECK(Validate());
51
52 BufferSize available_space = readable_ - writeable_;
53
54 if (0 >= available_space) {
55 available_space += buffer_size_;
56 }
57
58 if (length + 1 >= available_space) {
59 SIMPLE_STATS_COUNTER("DNS.PrefetchDnsQueueFull");
60 return OVERFLOW_PUSH; // Not enough space to push.
61 }
62
63 BufferSize dest = writeable_;
64 BufferSize space_till_wrap = buffer_sentinel_ - dest;
65 if (space_till_wrap < length + 1) {
66 // Copy until we run out of room at end of buffer.
67 std::memcpy(&buffer_[dest], source, space_till_wrap);
68 // Ensure caller didn't have embedded '\0' and also
69 // ensure trailing sentinel was in place.
70 // Relies on sentinel.
71 DCHECK(static_cast<size_t>(space_till_wrap) == strlen(&buffer_[dest]));
72
73 length -= space_till_wrap;
74 source += space_till_wrap;
75 dest = 0; // Continue writing at start of buffer.
76 }
77
78 // Copy any remaining portion of source.
79 std::memcpy(&buffer_[dest], source, length);
80 DCHECK(dest + length < buffer_sentinel_);
81 buffer_[dest + length] = '\0'; // We need termination in our buffer.
82 // Preclude embedded '\0'.
83 DCHECK(static_cast<size_t>(length) == strlen(&buffer_[dest]));
84
85 dest += length + 1;
86 if (dest == buffer_sentinel_)
87 dest = 0;
88
89 writeable_ = dest;
90 size_++;
91 DCHECK(Validate());
92 return SUCCESSFUL_PUSH;
93 }
94
95 // Extracts the next available string from the buffer.
96 // The returned string is null terminated, and hence has length
97 // that is exactly one greater than the written string.
98 // If the buffer is empty, then the Pop and returns false.
99 bool DnsQueue::Pop(std::string* out_string) {
100 DCHECK(Validate());
101 // Sentinel will preclude memory reads beyond buffer's end.
102 DCHECK('\0' == buffer_[buffer_sentinel_]);
103
104 if (readable_ == writeable_) {
105 return false; // buffer was empty
106 }
107
108 // Constructor *may* rely on sentinel for null termination.
109 (*out_string) = &buffer_[readable_];
110 // Our sentinel_ at end of buffer precludes an overflow in cast.
111 BufferSize first_fragment_size = static_cast<BufferSize> (out_string->size());
112
113 BufferSize terminal_null;
114 if (readable_ + first_fragment_size >= buffer_sentinel_) {
115 // Sentinel was used, so we need the portion after the wrap.
116 out_string->append(&buffer_[0]); // Fragment at start of buffer.
117 // Sentinel precludes overflow in cast to signed type.
118 terminal_null = static_cast<BufferSize>(out_string->size())
119 - first_fragment_size;
120 } else {
121 terminal_null = readable_ + first_fragment_size;
122 }
123 DCHECK('\0' == buffer_[terminal_null]);
124
125 BufferSize new_readable = terminal_null + 1;
126 if (buffer_sentinel_ == new_readable)
127 new_readable = 0;
128
129 readable_ = new_readable;
130 size_--;
131 if (readable_ == writeable_ || 0 == size_) {
132 // Queue is empty, so reset to start of buffer to help with peeking.
133 readable_ = writeable_ = 0;
134 }
135 DCHECK(Validate());
136 return true;
137 }
138
139 bool DnsQueue::Validate() {
140 return (readable_ >= 0) &&
141 readable_ < buffer_sentinel_ &&
142 writeable_ >= 0 &&
143 writeable_ < buffer_sentinel_ &&
144 '\0' == buffer_[buffer_sentinel_] &&
145 ((0 == size_) == (readable_ == writeable_));
146 }
OLDNEW
« no previous file with comments | « chrome/renderer/net/render_dns_queue.h ('k') | chrome/renderer/net/render_dns_queue_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698