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