OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_COMMON_PARTIAL_CIRCULAR_BUFFER_H_ | 5 #ifndef CHROME_COMMON_PARTIAL_CIRCULAR_BUFFER_H_ |
6 #define CHROME_COMMON_PARTIAL_CIRCULAR_BUFFER_H_ | 6 #define CHROME_COMMON_PARTIAL_CIRCULAR_BUFFER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include <stdint.h> |
9 | 9 |
10 // A wrapper around a memory buffer that allows circular read and write with a | 10 // A wrapper around a memory buffer that allows circular read and write with a |
11 // selectable wrapping position. Buffer layout (after wrap; H is header): | 11 // selectable wrapping position. Buffer layout (after wrap; H is header): |
12 // ----------------------------------------------------------- | 12 // ----------------------------------------------------------- |
13 // | H | Beginning | End | Middle | | 13 // | H | Beginning | End | Middle | |
14 // ----------------------------------------------------------- | 14 // ----------------------------------------------------------- |
15 // ^---- Non-wrapping -----^ ^--------- Wrapping ----------^ | 15 // ^---- Non-wrapping -----^ ^--------- Wrapping ----------^ |
16 // The non-wrapping part is never overwritten. The wrapping part will be | 16 // The non-wrapping part is never overwritten. The wrapping part will be |
17 // circular. The very first part is the header (see the BufferData struct | 17 // circular. The very first part is the header (see the BufferData struct |
18 // below). It consists of the following information: | 18 // below). It consists of the following information: |
19 // - Length written to the buffer (not including header). | 19 // - Length written to the buffer (not including header). |
20 // - Wrapping position. | 20 // - Wrapping position. |
21 // - End position of buffer. (If the last byte is at x, this will be x + 1.) | 21 // - End position of buffer. (If the last byte is at x, this will be x + 1.) |
22 // Users of wrappers around the same underlying buffer must ensure that writing | 22 // Users of wrappers around the same underlying buffer must ensure that writing |
23 // is finished before reading is started. | 23 // is finished before reading is started. |
24 class PartialCircularBuffer { | 24 class PartialCircularBuffer { |
25 public: | 25 public: |
26 // Use for reading. |buffer_size| is in bytes and must be larger than the | 26 // Use for reading. |buffer_size| is in bytes and must be larger than the |
27 // header size (see above). | 27 // header size (see above). |
28 PartialCircularBuffer(void* buffer, uint32 buffer_size); | 28 PartialCircularBuffer(void* buffer, uint32_t buffer_size); |
29 | 29 |
30 // Use for writing. |buffer_size| is in bytes and must be larger than the | 30 // Use for writing. |buffer_size| is in bytes and must be larger than the |
31 // header size (see above). If |append| is true, the header data is not reset | 31 // header size (see above). If |append| is true, the header data is not reset |
32 // and writing will continue were left off, |wrap_position| is then ignored. | 32 // and writing will continue were left off, |wrap_position| is then ignored. |
33 PartialCircularBuffer(void* buffer, | 33 PartialCircularBuffer(void* buffer, |
34 uint32 buffer_size, | 34 uint32_t buffer_size, |
35 uint32 wrap_position, | 35 uint32_t wrap_position, |
36 bool append); | 36 bool append); |
37 | 37 |
38 uint32 Read(void* buffer, uint32 buffer_size); | 38 uint32_t Read(void* buffer, uint32_t buffer_size); |
39 void Write(const void* buffer, uint32 buffer_size); | 39 void Write(const void* buffer, uint32_t buffer_size); |
40 | 40 |
41 private: | 41 private: |
42 friend class PartialCircularBufferTest; | 42 friend class PartialCircularBufferTest; |
43 | 43 |
44 #pragma pack(push) | 44 #pragma pack(push) |
45 #pragma pack(4) | 45 #pragma pack(4) |
46 struct BufferData { | 46 struct BufferData { |
47 uint32 total_written; | 47 uint32_t total_written; |
48 uint32 wrap_position; | 48 uint32_t wrap_position; |
49 uint32 end_position; | 49 uint32_t end_position; |
50 uint8 data[1]; | 50 uint8_t data[1]; |
51 }; | 51 }; |
52 #pragma pack(pop) | 52 #pragma pack(pop) |
53 | 53 |
54 void DoWrite(const uint8* input, uint32 input_size); | 54 void DoWrite(const uint8_t* input, uint32_t input_size); |
55 | 55 |
56 // Used for reading and writing. | 56 // Used for reading and writing. |
57 BufferData* buffer_data_; | 57 BufferData* buffer_data_; |
58 uint32 memory_buffer_size_; | 58 uint32_t memory_buffer_size_; |
59 uint32 data_size_; | 59 uint32_t data_size_; |
60 uint32 position_; | 60 uint32_t position_; |
61 | 61 |
62 // Used for reading. | 62 // Used for reading. |
63 uint32 total_read_; | 63 uint32_t total_read_; |
64 }; | 64 }; |
65 | 65 |
66 #endif // CHROME_COMMON_PARTIAL_CIRCULAR_BUFFER_H_ | 66 #endif // CHROME_COMMON_PARTIAL_CIRCULAR_BUFFER_H_ |
OLD | NEW |