| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 // DnsQueue is implemented as an almost FIFO circular buffer for text | |
| 6 // strings that don't have embedded nulls ('\0'). The "almost" element is that | |
| 7 // some duplicate strings may be removed (i.e., the string won't really be | |
| 8 // pushed *if* the class happens to notice that a duplicate is already in the | |
| 9 // queue). | |
| 10 // The buffers internal format is null terminated character strings | |
| 11 // (a.k.a., c_strings). | |
| 12 // It is written to be as fast as possible during push() operations, so | |
| 13 // that there will be minimal performance impact on a supplier thread. | |
| 14 // The push() operation will not block, and no memory allocation is involved | |
| 15 // (internally) during the push operations. | |
| 16 // The one caveat is that if there is insufficient space in the buffer to | |
| 17 // accept additional string via a push(), then the push() will fail, and | |
| 18 // the buffer will be unmodified. | |
| 19 | |
| 20 // This class was designed for use in DNS prefetch operations. During | |
| 21 // rendering, the supplier is the renderer (typically), and the consumer | |
| 22 // is a thread that sends messages to an async DNS resolver. | |
| 23 | |
| 24 #ifndef CHROME_RENDERER_NET_RENDER_DNS_QUEUE_H__ | |
| 25 #define CHROME_RENDERER_NET_RENDER_DNS_QUEUE_H__ | |
| 26 | |
| 27 #include <string> | |
| 28 | |
| 29 #include "base/basictypes.h" | |
| 30 #include "base/lock.h" | |
| 31 #include "base/scoped_ptr.h" | |
| 32 | |
| 33 class DnsQueue { | |
| 34 public: | |
| 35 // BufferSize is a signed type used for indexing into a buffer. | |
| 36 typedef int32 BufferSize; | |
| 37 | |
| 38 enum PushResult { SUCCESSFUL_PUSH, OVERFLOW_PUSH, REDUNDANT_PUSH }; | |
| 39 | |
| 40 // The size specified in the constructor creates a buffer large enough | |
| 41 // to hold at most one string of that length, or "many" | |
| 42 // strings of considerably shorter length. Note that strings | |
| 43 // are padded internally with a terminal '\0" while stored, | |
| 44 // so if you are trying to be precise and get N strings of | |
| 45 // length K to fit, you should actually construct a buffer with | |
| 46 // an internal size of N*(K+1). | |
| 47 explicit DnsQueue(BufferSize size); | |
| 48 ~DnsQueue(void); | |
| 49 | |
| 50 size_t Size() const { return size_; } | |
| 51 void Clear() { | |
| 52 size_ = 0; | |
| 53 readable_ = writeable_; | |
| 54 Validate(); | |
| 55 } | |
| 56 | |
| 57 // Push takes an unterminated string of the given length | |
| 58 // and inserts it into the queue for later | |
| 59 // extraction by read. For each successful push(), there | |
| 60 // can later be a corresponding read() to extracted the text. | |
| 61 // The string must not contain an embedded null terminator | |
| 62 // Exactly length chars are written, or the push fails (where | |
| 63 // "fails" means nothing is written). | |
| 64 // Returns true for success, false for failure (nothing written). | |
| 65 PushResult Push(const char* source, const size_t length); | |
| 66 | |
| 67 PushResult Push(std::string source) { | |
| 68 return Push(source.c_str(), source.length()); | |
| 69 } | |
| 70 | |
| 71 // Extract the next available string from the buffer. | |
| 72 // If the buffer is empty, then return false. | |
| 73 bool Pop(std::string* out_string); | |
| 74 | |
| 75 private: | |
| 76 bool Validate(); // Checks that all internal data is valid. | |
| 77 | |
| 78 const scoped_array<char> buffer_; // Circular buffer, plus extra char ('\0'). | |
| 79 const BufferSize buffer_size_; // Size one smaller than allocated space. | |
| 80 const BufferSize buffer_sentinel_; // Index of extra '\0' at end of buffer_. | |
| 81 | |
| 82 // If writable_ == readable_, then the buffer is empty. | |
| 83 BufferSize readable_; // Next readable char in buffer_. | |
| 84 BufferSize writeable_; // The next space in buffer_ to push. | |
| 85 | |
| 86 // Number of queued strings | |
| 87 size_t size_; | |
| 88 | |
| 89 DISALLOW_COPY_AND_ASSIGN(DnsQueue); | |
| 90 }; // class DnsQueue | |
| 91 | |
| 92 #endif // CHROME_RENDERER_NET_RENDER_DNS_QUEUE_H__ | |
| OLD | NEW |