/Users/deen/code/yugabyte-db/src/yb/rpc/circular_read_buffer.h
Line | Count | Source |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #ifndef YB_RPC_CIRCULAR_READ_BUFFER_H |
15 | | #define YB_RPC_CIRCULAR_READ_BUFFER_H |
16 | | |
17 | | #include "yb/rpc/stream.h" |
18 | | |
19 | | #include "yb/util/mem_tracker.h" |
20 | | |
21 | | namespace yb { |
22 | | namespace rpc { |
23 | | |
24 | | struct FreeMemory { |
25 | 1.17M | void operator()(void* data) const { |
26 | 1.17M | free(data); |
27 | 1.17M | } |
28 | | }; |
29 | | |
30 | | // StreamReadBuffer implementation that is based on circular buffer of fixed capacity. |
31 | | class CircularReadBuffer : public StreamReadBuffer { |
32 | | public: |
33 | | explicit CircularReadBuffer(size_t capacity, const MemTrackerPtr& parent_tracker); |
34 | | |
35 | | bool ReadyToRead() override; |
36 | | bool Empty() override; |
37 | | void Reset() override; |
38 | | Result<IoVecs> PrepareAppend() override; |
39 | | std::string ToString() const override; |
40 | | void DataAppended(size_t len) override; |
41 | | IoVecs AppendedVecs() override; |
42 | | bool Full() override; |
43 | | void Consume(size_t count, const Slice& prepend) override; |
44 | | size_t DataAvailable() override; |
45 | | |
46 | | private: |
47 | | ScopedTrackedConsumption consumption_; |
48 | | std::unique_ptr<char, FreeMemory> buffer_; |
49 | | const size_t capacity_; |
50 | | size_t pos_ = 0; |
51 | | size_t size_ = 0; |
52 | | Slice prepend_; |
53 | | bool had_prepend_ = false; |
54 | | }; |
55 | | |
56 | | } // namespace rpc |
57 | | } // namespace yb |
58 | | |
59 | | #endif // YB_RPC_CIRCULAR_READ_BUFFER_H |