| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 #ifndef NET_TOOLS_FLIP_SERVER_RING_BUFFER_H__ | |
| 6 #define NET_TOOLS_FLIP_SERVER_RING_BUFFER_H__ | |
| 7 | |
| 8 #include <memory> | |
| 9 | |
| 10 #include "base/compiler_specific.h" | |
| 11 #include "net/tools/balsa/buffer_interface.h" | |
| 12 | |
| 13 namespace net { | |
| 14 | |
| 15 // The ring buffer is a circular buffer, that is, reads or writes may wrap | |
| 16 // around the end of the linear memory contained by the class (and back to | |
| 17 // the beginning). This is a good choice when you want to use a fixed amount | |
| 18 // of buffering and don't want to be moving memory around a lot. | |
| 19 // | |
| 20 // What is the penalty for using this over a normal, linear buffer? | |
| 21 // Reading all the data may take two operations, and | |
| 22 // writing all the data may take two operations. | |
| 23 // | |
| 24 // In the proxy, this class is used as a fixed size buffer between | |
| 25 // clients and servers (so that the memory size is constrained). | |
| 26 | |
| 27 class RingBuffer : public BufferInterface { | |
| 28 public: | |
| 29 explicit RingBuffer(int buffer_size); | |
| 30 ~RingBuffer() override; | |
| 31 | |
| 32 // Resize the buffer to the size specified here. If the buffer_size passed | |
| 33 // in here is smaller than the amount of data in the buffer, then the oldest | |
| 34 // data will be dropped, but all other data will be saved. | |
| 35 // This means: If the buffer size is increasing, all data that was resident | |
| 36 // in the buffer prior to this call will be resident after this call. | |
| 37 void Resize(int buffer_size); | |
| 38 | |
| 39 // The following functions all override pure virtual functions | |
| 40 // in BufferInterface. See buffer_interface.h for a description | |
| 41 // of what they do if the function isn't documented here. | |
| 42 int ReadableBytes() const override; | |
| 43 int BufferSize() const override; | |
| 44 int BytesFree() const override; | |
| 45 | |
| 46 bool Empty() const override; | |
| 47 bool Full() const override; | |
| 48 | |
| 49 // returns the number of characters written. | |
| 50 // appends up-to-'size' bytes to the ringbuffer. | |
| 51 int Write(const char* bytes, int size) override; | |
| 52 | |
| 53 // Stores a pointer into the ring buffer in *ptr, and stores the number of | |
| 54 // characters which are allowed to be written in *size. | |
| 55 // If there are no writable bytes available, then *size will contain 0. | |
| 56 void GetWritablePtr(char** ptr, int* size) const override; | |
| 57 | |
| 58 // Stores a pointer into the ring buffer in *ptr, and stores the number of | |
| 59 // characters which are allowed to be read in *size. | |
| 60 // If there are no readable bytes available, then *size will contain 0. | |
| 61 void GetReadablePtr(char** ptr, int* size) const override; | |
| 62 | |
| 63 // Returns the number of bytes read into 'bytes'. | |
| 64 int Read(char* bytes, int size) override; | |
| 65 | |
| 66 // Removes all data from the ring buffer. | |
| 67 void Clear() override; | |
| 68 | |
| 69 // Reserves contiguous writable empty space in the buffer of size bytes. | |
| 70 // Since the point of this class is to have a fixed size buffer, be careful | |
| 71 // not to inadvertently resize the buffer using Reserve(). If the reserve | |
| 72 // size is <= BytesFree(), it is guaranteed that the buffer size will not | |
| 73 // change. | |
| 74 // This can be an expensive operation, it may new a buffer copy all existing | |
| 75 // data and delete the old data. Even if the existing buffer does not need | |
| 76 // to be resized, unread data may still need to be non-destructively copied | |
| 77 // to consolidate fragmented free space. If the size requested is less than | |
| 78 // or equal to BytesFree(), it is guaranteed that the buffer size will not | |
| 79 // change. | |
| 80 bool Reserve(int size) override; | |
| 81 | |
| 82 // Removes the oldest 'amount_to_advance' characters. | |
| 83 // If amount_to_consume > ReadableBytes(), this performs a Clear() instead. | |
| 84 void AdvanceReadablePtr(int amount_to_advance) override; | |
| 85 | |
| 86 // Moves the internal pointers around such that the amount of data specified | |
| 87 // here is expected to already be resident (as if it was Written). | |
| 88 void AdvanceWritablePtr(int amount_to_advance) override; | |
| 89 | |
| 90 protected: | |
| 91 int read_idx() const { return read_idx_; } | |
| 92 int write_idx() const { return write_idx_; } | |
| 93 int bytes_used() const { return bytes_used_; } | |
| 94 int buffer_size() const { return buffer_size_; } | |
| 95 const char* buffer() const { return buffer_.get(); } | |
| 96 | |
| 97 int set_read_idx(int idx) { return read_idx_ = idx; } | |
| 98 int set_write_idx(int idx) { return write_idx_ = idx; } | |
| 99 | |
| 100 private: | |
| 101 std::unique_ptr<char[]> buffer_; | |
| 102 int buffer_size_; | |
| 103 int bytes_used_; | |
| 104 int read_idx_; | |
| 105 int write_idx_; | |
| 106 | |
| 107 RingBuffer(const RingBuffer&); | |
| 108 void operator=(const RingBuffer&); | |
| 109 }; | |
| 110 | |
| 111 } // namespace net | |
| 112 | |
| 113 #endif // NET_TOOLS_FLIP_SERVER_RING_BUFFER_H__ | |
| OLD | NEW |