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

Side by Side Diff: cc/ring_buffer.h

Issue 11817011: cc: add RingBuffer class for timestamp storing in FrameRateCounter (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: added DCHECKS for wrong buffer reads and divide by 0 Created 7 years, 11 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
« cc/frame_rate_counter.cc ('K') | « cc/layer_tree_host_impl.cc ('k') | no next file » | 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 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 CC_RING_BUFFER_H_
6 #define CC_RING_BUFFER_H_
7
8 #include "base/logging.h"
9
10 namespace cc {
11
12 template<typename T, int size>
13 class RingBuffer {
14 public:
15 explicit RingBuffer()
16 : current_index_(0) {
17 }
18
19 int BufferSize() const {
20 return size;
21 }
22
23 int CurrentIndex() const {
24 return current_index_;
25 }
26
27 // tests if a value was saved to this index
28 bool IsFilledIndex(int n) const {
29 DCHECK(n >= 0);
egraether 2013/01/10 23:38:29 Prevents using negative indices.
30 return BufferIndex(n) < current_index_;
31 }
32
33 // n = 0 returns the oldest value and
34 // n = bufferSize() - 1 returns the most recent value.
35 T ReadBuffer(int n) const {
36 DCHECK(IsFilledIndex(n));
egraether 2013/01/10 23:38:29 Forbids access to uninitialized indices.
37 return buffer_[BufferIndex(n)];
38 }
39
40 void SaveToBuffer(T value) {
41 buffer_[BufferIndex(0)] = value;
42 current_index_++;
43 }
44
45 private:
46 inline int BufferIndex(int n) const {
47 return (current_index_ + n) % size;
48 }
49
50 T buffer_[size];
51 int current_index_;
52 };
53
54 } // namespace cc
55
56 #endif // CC_RING_BUFFER_H_
OLDNEW
« cc/frame_rate_counter.cc ('K') | « cc/layer_tree_host_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698