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..5a865fa46069cefd4200eb969c108cd8851183b7 |
--- /dev/null |
+++ b/content/common/circular_memory_buffer.h |
@@ -0,0 +1,58 @@ |
+// 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) is the 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 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
|
+ public: |
+ CircularMemoryBuffer(void* buffer, size_t buffer_size); |
+ |
+ // Initializes reading from the beginning of the buffer. |
+ 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.
|
+ |
+ // Initializes writing from the beginning of the buffer. |
+ void InitWrite(size_t wrap_position); |
+ |
+ size_t Read(void* buffer, size_t buffer_size); |
+ void Write(const void* buffer, size_t buffer_size); |
+ |
+ private: |
+ void GetHeaderData(); |
+ void SetHeaderData(size_t length, size_t wrap_position, size_t position); |
+ void UpdateHeaderData(size_t added_length, size_t new_position); |
+ |
+ // Used for reading and writing. |
+ 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.
|
+ size_t memory_buffer_size_; |
+ size_t position_; |
+ size_t wrap_position_; |
+ |
+ // Used for reading. |
+ size_t end_position_; |
+ 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
|
+ size_t total_read_; |
+}; |
+ |
+} // namespace content |
+ |
+#endif // CONTENT_COMMON_CIRCULAR_MEMORY_BUFFER_H_ |