Chromium Code Reviews| Index: content/common/circular_memory_buffer.h |
| diff --git a/content/common/circular_memory_buffer.h b/content/common/circular_memory_buffer.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ab7c622d7feafe3d42a99332c2f31b7116c3b5a3 |
| --- /dev/null |
| +++ b/content/common/circular_memory_buffer.h |
| @@ -0,0 +1,71 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CONTENT_COMMON_CIRCULAR_MEMORY_BUFFER_H_ |
| +#define CONTENT_COMMON_CIRCULAR_MEMORY_BUFFER_H_ |
| + |
| +#include "base/basictypes.h" |
| + |
| +namespace content { |
| + |
| +// A wrapper around a memory buffer that allows circular read and write with a |
| +// selectable wrapping position. Buffer layout (after wrap; H is header): |
| +// ----------------------------------------------------------- |
| +// | H | Beginning | End | Middle | |
| +// ----------------------------------------------------------- |
| +// ^---- Non-wrapping -----^ ^--------- Wrapping ----------^ |
| +// The non-wrapping part is never overwritten. The wrapping part will be |
| +// 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
|
| +// header. It consists of the following information: |
| +// - Length written to the buffer (not including header). |
| +// - Wrapping position. |
| +// - End position of buffer. (If the last byte is at x, this will be x + 1.) |
| +// Users of wrappers around the same underlying buffer must ensure that writing |
| +// is finished before reading is started. |
| +class PartialCircularBuffer { |
| + public: |
| + // Use for reading. |buffer_size| is in bytes and must be larger than the |
| + // header size (see above). |
| + PartialCircularBuffer(const uint8* buffer, size_t buffer_size); |
| + |
| + // Use for writing. |buffer_size| is in bytes and must be larger than the |
| + // header size (see above). |
| + PartialCircularBuffer(uint8* buffer, |
| + size_t buffer_size, |
| + size_t wrap_position); |
| + |
| + size_t Read(void* buffer, size_t buffer_size); |
| + void Write(const void* buffer, size_t buffer_size); |
| + |
| + private: |
| + 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
|
| + size_t total_written; |
| + size_t wrap_position; |
| + size_t end_position; |
| + }; |
| + |
| + void InitRead(); |
| + void InitWrite(size_t wrap_position); |
| + |
| + void ReadHeaderData(); |
| + void WriteHeaderData(); |
| + void UpdateAndWriteHeaderData(size_t added_length, size_t new_position); |
| + |
| + // Used for reading and writing. |
| + size_t memory_buffer_size_; |
| + size_t position_; |
| + 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.
|
| + |
| + // Used for reading. |
| + const uint8* memory_buffer_read_; |
| + size_t total_read_; |
| + |
| + // Used for writing. |
| + uint8* memory_buffer_write_; |
| + |
| +}; |
| + |
| +} // namespace content |
| + |
| +#endif // CONTENT_COMMON_CIRCULAR_MEMORY_BUFFER_H_ |