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