File: dns_prefetch_queue.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (97 lines) | stat: -rw-r--r-- 3,780 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// DnsQueue is implemented as an almost FIFO circular buffer for text
// strings that don't have embedded nulls ('\0').  The "almost" element is that
// some duplicate strings may be removed (i.e., the string won't really be
// pushed *if* the class happens to notice that a duplicate is already in the
// queue).
// The buffers internal format is null terminated character strings
// (a.k.a., c_strings).
// It is written to be as fast as possible during push() operations, so
// that there will be minimal performance impact on a supplier thread.
// The push() operation will not block, and no memory allocation is involved
// (internally) during the push operations.
// The one caveat is that if there is insufficient space in the buffer to
// accept additional string via a push(), then the push() will fail, and
// the buffer will be unmodified.

// This class was designed for use in DNS prefetch operations.  During
// rendering, the supplier is the renderer (typically), and the consumer
// is a thread that sends messages to an async DNS resolver.

#ifndef COMPONENTS_NETWORK_HINTS_RENDERER_DNS_PREFETCH_QUEUE_H__
#define COMPONENTS_NETWORK_HINTS_RENDERER_DNS_PREFETCH_QUEUE_H__

#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <string>

#include "base/macros.h"

namespace network_hints {

// A queue of DNS lookup requests for internal use within the network_hints
// component.
class DnsQueue {
 public:
  // BufferSize is a signed type used for indexing into a buffer.
  typedef int32_t BufferSize;

  enum PushResult { SUCCESSFUL_PUSH, OVERFLOW_PUSH, REDUNDANT_PUSH };

  // The size specified in the constructor creates a buffer large enough
  // to hold at most one string of that length, or "many"
  // strings of considerably shorter length.  Note that strings
  // are padded internally with a terminal '\0" while stored,
  // so if you are trying to be precise and get N strings of
  // length K to fit, you should actually construct a buffer with
  // an internal size of N*(K+1).
  explicit DnsQueue(BufferSize size);
  ~DnsQueue(void);

  size_t Size() const { return size_; }
  void Clear();

  // Push takes an unterminated string of the given length
  // and inserts it into the queue for later
  // extraction by read.  For each successful push(), there
  // can later be a corresponding read() to extracted the text.
  // The string must not contain an embedded null terminator
  // Exactly length chars are written, or the push fails (where
  // "fails" means nothing is written).
  // Returns true for success, false for failure (nothing written).
  PushResult Push(const char* source, const size_t length);

  PushResult Push(const std::string& source) {
    return Push(source.c_str(), source.length());
  }

  // Extract the next available string from the buffer.
  // If the buffer is empty, then return false.
  bool Pop(std::string* out_string);

 private:
  bool Validate();  // Checks that all internal data is valid.

  // Circular buffer, plus extra char ('\0').
  const std::unique_ptr<char[]> buffer_;
  const BufferSize buffer_size_;  // Size one smaller than allocated space.
  const BufferSize buffer_sentinel_;  // Index of extra '\0' at end of buffer_.

  // If writable_ == readable_, then the buffer is empty.
  BufferSize readable_;  // Next readable char in buffer_.
  BufferSize writeable_;  // The next space in buffer_ to push.

  // Number of queued strings
  size_t size_;

  DISALLOW_COPY_AND_ASSIGN(DnsQueue);
};  // class DnsQueue

}  // namespace network_hints

#endif  // COMPONENTS_NETWORK_HINTS_RENDERER_DNS_PREFETCH_QUEUE_H__