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