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

Side by Side Diff: content/common/partial_circular_buffer.h

Issue 13473005: Adding partially circular memory buffer wrapper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Code review and rebase. Created 7 years, 8 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | content/common/partial_circular_buffer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CONTENT_COMMON_PARTIAL_CIRCULAR_BUFFER_H_
6 #define CONTENT_COMMON_PARTIAL_CIRCULAR_BUFFER_H_
7
8 #include <algorithm>
9
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12
13 namespace content {
14
15 // A wrapper around a memory buffer that allows circular read and write with a
16 // selectable wrapping position. Buffer layout (after wrap; H is header):
17 // -----------------------------------------------------------
18 // | H | Beginning | End | Middle |
19 // -----------------------------------------------------------
20 // ^---- Non-wrapping -----^ ^--------- Wrapping ----------^
21 // The non-wrapping part is never overwritten. The wrapping part will be
22 // circular. The very first part is the header (see the BufferData struct
23 // below). It consists of the following information:
24 // - Length written to the buffer (not including header).
25 // - Wrapping position.
26 // - End position of buffer. (If the last byte is at x, this will be x + 1.)
27 // Users of wrappers around the same underlying buffer must ensure that writing
28 // is finished before reading is started.
29 class PartialCircularBuffer {
30 public:
31 // Use for reading. |buffer_size| is in bytes and must be larger than the
32 // header size (see above).
33 PartialCircularBuffer(void* buffer, uint32 buffer_size);
34
35 // Use for writing. |buffer_size| is in bytes and must be larger than the
36 // header size (see above).
37 PartialCircularBuffer(void* buffer,
38 uint32 buffer_size,
39 uint32 wrap_position);
40
41 uint32 Read(void* buffer, uint32 buffer_size);
42 void Write(const void* buffer, uint32 buffer_size);
43
44 private:
45 friend class PartialCircularBufferTest;
46
47 #pragma pack(push)
48 #pragma pack(4)
49 struct BufferData {
50 uint32 total_written;
51 uint32 wrap_position;
52 uint32 end_position;
53 uint8 data[1];
54 };
55 #pragma pack(pop)
56
57 void DoWrite(void* dest, const void* src, uint32 num);
58
59 uint32 Min3(uint32 a, uint32 b, uint32 c) {
tommi (sloooow) - chröme 2013/04/18 16:38:52 I don't think this needs to be a member function.
Henrik Grunell 2013/04/19 06:32:31 Done.
60 return std::min(a, std::min(b, c));
61 }
62
63 // Used for reading and writing.
64 BufferData* buffer_data_;
65 uint32 memory_buffer_size_;
66 uint32 data_size_;
67 uint32 position_;
68
69 // Used for reading.
70 uint32 total_read_;
71 };
72
73 } // namespace content
74
75 #endif // CONTENT_COMMON_PARTIAL_CIRCULAR_BUFFER_H_
OLDNEW
« no previous file with comments | « no previous file | content/common/partial_circular_buffer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698