OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 CONTENT_COMMON_PARTIAL_CIRCULAR_BUFFER_H_ |
| 6 #define CONTENT_COMMON_PARTIAL_CIRCULAR_BUFFER_H_ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 |
| 10 namespace content { |
| 11 |
| 12 // A wrapper around a memory buffer that allows circular read and write with a |
| 13 // selectable wrapping position. Buffer layout (after wrap; H is header): |
| 14 // ----------------------------------------------------------- |
| 15 // | H | Beginning | End | Middle | |
| 16 // ----------------------------------------------------------- |
| 17 // ^---- Non-wrapping -----^ ^--------- Wrapping ----------^ |
| 18 // The non-wrapping part is never overwritten. The wrapping part will be |
| 19 // circular. The very first part is the header (see the BufferData struct |
| 20 // below). It consists of the following information: |
| 21 // - Length written to the buffer (not including header). |
| 22 // - Wrapping position. |
| 23 // - End position of buffer. (If the last byte is at x, this will be x + 1.) |
| 24 // Users of wrappers around the same underlying buffer must ensure that writing |
| 25 // is finished before reading is started. |
| 26 class PartialCircularBuffer { |
| 27 public: |
| 28 // Use for reading. |buffer_size| is in bytes and must be larger than the |
| 29 // header size (see above). |
| 30 PartialCircularBuffer(void* buffer, size_t buffer_size); |
| 31 |
| 32 // Use for writing. |buffer_size| is in bytes and must be larger than the |
| 33 // header size (see above). |
| 34 PartialCircularBuffer(void* buffer, |
| 35 size_t buffer_size, |
| 36 size_t wrap_position); |
| 37 |
| 38 size_t Read(void* buffer, size_t buffer_size); |
| 39 void Write(const void* buffer, size_t buffer_size); |
| 40 |
| 41 private: |
| 42 struct BufferData { |
| 43 uint32 total_written; |
| 44 uint32 wrap_position; |
| 45 uint32 end_position; |
| 46 uint8 data[1]; |
| 47 }; |
| 48 |
| 49 void DoWrite(void* dest, const void* src, size_t num); |
| 50 |
| 51 // Used for reading and writing. |
| 52 BufferData* buffer_data_; |
| 53 size_t memory_buffer_size_; |
| 54 size_t data_size_; |
| 55 size_t position_; |
| 56 |
| 57 // Used for reading. |
| 58 size_t total_read_; |
| 59 }; |
| 60 |
| 61 } // namespace content |
| 62 |
| 63 #endif // CONTENT_COMMON_PARTIAL_CIRCULAR_BUFFER_H_ |
OLD | NEW |