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, size_t 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 size_t buffer_size, | |
37 size_t wrap_position); | |
38 | |
39 size_t Read(void* buffer, size_t buffer_size); | |
40 void Write(const void* buffer, size_t buffer_size); | |
41 | |
42 private: | |
43 FRIEND_TEST_ALL_PREFIXES(PartialCircularBufferTest, TestHeaderSize); | |
44 | |
45 // The reason of packing the struct is to simplify test code since the header | |
46 // and data size will the same for all platforms. | |
47 #pragma pack(push) | |
tommi (sloooow) - chröme
2013/04/11 16:37:39
I still think we should have the packing in there.
Henrik Grunell
2013/04/14 07:06:49
Does this fall in the "IPC related" bin? It's an i
| |
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, size_t num); | |
58 | |
59 // Used for reading and writing. | |
60 BufferData* buffer_data_; | |
61 size_t memory_buffer_size_; | |
62 size_t data_size_; | |
63 size_t position_; | |
64 | |
65 // Used for reading. | |
66 size_t total_read_; | |
67 }; | |
68 | |
69 } // namespace content | |
70 | |
71 #endif // CONTENT_COMMON_PARTIAL_CIRCULAR_BUFFER_H_ | |
OLD | NEW |