Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Unified Diff: content/common/circular_memory_buffer.h

Issue 13473005: Adding partially circular memory buffer wrapper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Code review + added some lost code. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | content/common/circular_memory_buffer.cc » ('j') | content/common/circular_memory_buffer.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_
« no previous file with comments | « no previous file | content/common/circular_memory_buffer.cc » ('j') | content/common/circular_memory_buffer.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698