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_CIRCULAR_MEMORY_BUFFER_H_ | |
6 #define CONTENT_COMMON_CIRCULAR_MEMORY_BUFFER_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 | |
10 namespace content { | |
11 | |
12 // A wrapper around a memory buffer that allows circular read and write with a | |
13 // selectable wrapping position. Buffer layout (after wrap; H is header): | |
14 // ----------------------------------------------------------- | |
15 // | H | Beginning | End | Middle | | |
16 // ----------------------------------------------------------- | |
17 // ^---- Non-wrapping -----^ ^--------- Wrapping ----------^ | |
18 // The non-wrapping part is never overwritten. The wrapping part will be | |
19 // circular. The first 3 * 4 bytes (int32) = 12 bytes + possible padding is the | |
tommi (sloooow) - chröme
2013/04/03 14:11:32
see comment about this below. As is sizeof(header
Henrik Grunell
2013/04/05 11:21:08
Absolutely, I had forgot to update this after thos
| |
20 // header. It consists of the following information: | |
21 // - Length written to the buffer (not including header). | |
22 // - Wrapping position. | |
23 // - End position of buffer. (If the last byte is at x, this will be x + 1.) | |
24 // Users of wrappers around the same underlying buffer must ensure that writing | |
25 // is finished before reading is started. | |
26 class PartialCircularBuffer { | |
27 public: | |
28 // Use for reading. |buffer_size| is in bytes and must be larger than the | |
29 // header size (see above). | |
30 PartialCircularBuffer(const uint8* buffer, size_t buffer_size); | |
31 | |
32 // Use for writing. |buffer_size| is in bytes and must be larger than the | |
33 // header size (see above). | |
34 PartialCircularBuffer(uint8* buffer, | |
35 size_t buffer_size, | |
36 size_t wrap_position); | |
37 | |
38 size_t Read(void* buffer, size_t buffer_size); | |
39 void Write(const void* buffer, size_t buffer_size); | |
40 | |
41 private: | |
42 struct HeaderData { | |
tommi (sloooow) - chröme
2013/04/03 14:11:32
I don't think it's a good idea to use size_t here
Henrik Grunell
2013/04/05 11:21:08
Agree. Changed to uint32.
I can still set the pac
| |
43 size_t total_written; | |
44 size_t wrap_position; | |
45 size_t end_position; | |
46 }; | |
47 | |
48 void InitRead(); | |
49 void InitWrite(size_t wrap_position); | |
50 | |
51 void ReadHeaderData(); | |
52 void WriteHeaderData(); | |
53 void UpdateAndWriteHeaderData(size_t added_length, size_t new_position); | |
54 | |
55 // Used for reading and writing. | |
56 size_t memory_buffer_size_; | |
57 size_t position_; | |
58 HeaderData header_data_; | |
tommi (sloooow) - chröme
2013/04/03 14:11:32
maybe you should keep this variable last in the cl
Henrik Grunell
2013/04/05 11:21:08
It's now a pointer.
| |
59 | |
60 // Used for reading. | |
61 const uint8* memory_buffer_read_; | |
62 size_t total_read_; | |
63 | |
64 // Used for writing. | |
65 uint8* memory_buffer_write_; | |
66 | |
67 }; | |
68 | |
69 } // namespace content | |
70 | |
71 #endif // CONTENT_COMMON_CIRCULAR_MEMORY_BUFFER_H_ | |
OLD | NEW |