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) is the header. It consists of the | |
20 // 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 CircularMemoryBuffer { | |
tommi (sloooow) - chröme
2013/04/02 19:34:08
Can we reflect the non-wrapping part better in the
Henrik Grunell
2013/04/03 12:47:27
Yes that sounds good. PartialCircularBuffer is the
| |
27 public: | |
28 CircularMemoryBuffer(void* buffer, size_t buffer_size); | |
29 | |
30 // Initializes reading from the beginning of the buffer. | |
31 void InitRead(); | |
tommi (sloooow) - chröme
2013/04/02 19:34:08
Could we have two separate constructors instead of
Henrik Grunell
2013/04/03 12:47:27
Yes, that sounds good. Done.
| |
32 | |
33 // Initializes writing from the beginning of the buffer. | |
34 void InitWrite(size_t wrap_position); | |
35 | |
36 size_t Read(void* buffer, size_t buffer_size); | |
37 void Write(const void* buffer, size_t buffer_size); | |
38 | |
39 private: | |
40 void GetHeaderData(); | |
41 void SetHeaderData(size_t length, size_t wrap_position, size_t position); | |
42 void UpdateHeaderData(size_t added_length, size_t new_position); | |
43 | |
44 // Used for reading and writing. | |
45 int8* memory_buffer_; | |
tommi (sloooow) - chröme
2013/04/02 19:34:08
nit: I think we tend to use uint8 for raw bytes of
Henrik Grunell
2013/04/03 12:47:27
Done.
| |
46 size_t memory_buffer_size_; | |
47 size_t position_; | |
48 size_t wrap_position_; | |
49 | |
50 // Used for reading. | |
51 size_t end_position_; | |
52 size_t total_written_; | |
tommi (sloooow) - chröme
2013/04/02 19:34:08
is total_written_ used for reading?
Henrik Grunell
2013/04/03 12:47:27
Yes, it stores the header info how much was writte
| |
53 size_t total_read_; | |
54 }; | |
55 | |
56 } // namespace content | |
57 | |
58 #endif // CONTENT_COMMON_CIRCULAR_MEMORY_BUFFER_H_ | |
OLD | NEW |