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
3 // found in the LICENSE file.
5 #ifndef CHROME_COMMON_PARTIAL_CIRCULAR_BUFFER_H_
6 #define CHROME_COMMON_PARTIAL_CIRCULAR_BUFFER_H_
8 #include "base/basictypes.h"
9 #include "base/gtest_prod_util.h"
11 // A wrapper around a memory buffer that allows circular read and write with a
12 // selectable wrapping position. Buffer layout (after wrap; H is header):
13 // -----------------------------------------------------------
14 // | H | Beginning | End | Middle |
15 // -----------------------------------------------------------
16 // ^---- Non-wrapping -----^ ^--------- Wrapping ----------^
17 // The non-wrapping part is never overwritten. The wrapping part will be
18 // circular. The very first part is the header (see the BufferData struct
19 // below). It consists of the following information:
20 // - Length written to the buffer (not including header).
21 // - Wrapping position.
22 // - End position of buffer. (If the last byte is at x, this will be x + 1.)
23 // Users of wrappers around the same underlying buffer must ensure that writing
24 // is finished before reading is started.
25 class PartialCircularBuffer
{
27 // Use for reading. |buffer_size| is in bytes and must be larger than the
28 // header size (see above).
29 PartialCircularBuffer(void* buffer
, uint32 buffer_size
);
31 // Use for writing. |buffer_size| is in bytes and must be larger than the
32 // header size (see above). If |append| is true, the header data is not reset
33 // and writing will continue were left off, |wrap_position| is then ignored.
34 PartialCircularBuffer(void* buffer
,
39 uint32
Read(void* buffer
, uint32 buffer_size
);
40 void Write(const void* buffer
, uint32 buffer_size
);
43 friend class PartialCircularBufferTest
;
55 void DoWrite(const uint8
* input
, uint32 input_size
);
57 // Used for reading and writing.
58 BufferData
* buffer_data_
;
59 uint32 memory_buffer_size_
;
67 #endif // CHROME_COMMON_PARTIAL_CIRCULAR_BUFFER_H_