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

Unified 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 side-by-side diff with in-line comments
Download patch
« cc/frame_rate_counter.cc ('K') | « cc/layer_tree_host_impl.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cc/ring_buffer.h
diff --git a/cc/ring_buffer.h b/cc/ring_buffer.h
new file mode 100644
index 0000000000000000000000000000000000000000..b62f2f555b53a3102ca059b3ad49516015be7c3d
--- /dev/null
+++ b/cc/ring_buffer.h
@@ -0,0 +1,56 @@
+// Copyright 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 CC_RING_BUFFER_H_
+#define CC_RING_BUFFER_H_
+
+#include "base/logging.h"
+
+namespace cc {
+
+template<typename T, int size>
+class RingBuffer {
+ public:
+ explicit RingBuffer()
+ : current_index_(0) {
+ }
+
+ int BufferSize() const {
+ return size;
+ }
+
+ int CurrentIndex() const {
+ return current_index_;
+ }
+
+ // tests if a value was saved to this index
+ bool IsFilledIndex(int n) const {
+ DCHECK(n >= 0);
egraether 2013/01/10 23:38:29 Prevents using negative indices.
+ return BufferIndex(n) < current_index_;
+ }
+
+ // n = 0 returns the oldest value and
+ // n = bufferSize() - 1 returns the most recent value.
+ T ReadBuffer(int n) const {
+ DCHECK(IsFilledIndex(n));
egraether 2013/01/10 23:38:29 Forbids access to uninitialized indices.
+ return buffer_[BufferIndex(n)];
+ }
+
+ void SaveToBuffer(T value) {
+ buffer_[BufferIndex(0)] = value;
+ current_index_++;
+ }
+
+ private:
+ inline int BufferIndex(int n) const {
+ return (current_index_ + n) % size;
+ }
+
+ T buffer_[size];
+ int current_index_;
+};
+
+} // namespace cc
+
+#endif // CC_RING_BUFFER_H_
« 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