Index: content/common/partial_circular_buffer.h |
diff --git a/content/common/partial_circular_buffer.h b/content/common/partial_circular_buffer.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c7ae1d97b619cbee4734cb221030e44a5c70f7a8 |
--- /dev/null |
+++ b/content/common/partial_circular_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_PARTIAL_CIRCULAR_BUFFER_H_ |
+#define CONTENT_COMMON_PARTIAL_CIRCULAR_BUFFER_H_ |
+ |
+#include "base/basictypes.h" |
+#include "base/gtest_prod_util.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 very first part is the header (see the BufferData struct |
+// below). 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(void* buffer, size_t buffer_size); |
+ |
+ // Use for writing. |buffer_size| is in bytes and must be larger than the |
+ // header size (see above). |
+ PartialCircularBuffer(void* 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: |
+ FRIEND_TEST_ALL_PREFIXES(PartialCircularBufferTest, TestHeaderSize); |
+ |
+// The reason of packing the struct is to simplify test code since the header |
+// and data size will the same for all platforms. |
+#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
|
+#pragma pack(4) |
+ struct BufferData { |
+ uint32 total_written; |
+ uint32 wrap_position; |
+ uint32 end_position; |
+ uint8 data[1]; |
+ }; |
+#pragma pack(pop) |
+ |
+ void DoWrite(void* dest, const void* src, size_t num); |
+ |
+ // Used for reading and writing. |
+ BufferData* buffer_data_; |
+ size_t memory_buffer_size_; |
+ size_t data_size_; |
+ size_t position_; |
+ |
+ // Used for reading. |
+ size_t total_read_; |
+}; |
+ |
+} // namespace content |
+ |
+#endif // CONTENT_COMMON_PARTIAL_CIRCULAR_BUFFER_H_ |