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..9f7e17b136084dd010cd4100492a821314ce9e5a |
--- /dev/null |
+++ b/content/common/partial_circular_buffer.h |
@@ -0,0 +1,63 @@ |
+// 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" |
+ |
+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: |
+ struct BufferData { |
+ uint32 total_written; |
+ uint32 wrap_position; |
+ uint32 end_position; |
+ uint8 data[1]; |
+ }; |
+ |
+ 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_ |