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

Unified Diff: counter.cc

Issue 2729018: Readability review. (Closed) Base URL: ssh://git@chromiumos-git/metrics.git
Patch Set: Address comments -- empty line, const method. Created 10 years, 6 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 | « counter.h ('k') | counter_mock.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: counter.cc
diff --git a/counter.cc b/counter.cc
index 34fcf711628ac0520add8e35178391ea2858cf7e..88073df035e072ed53e3c94bd72d76750964c2da 100644
--- a/counter.cc
+++ b/counter.cc
@@ -4,7 +4,7 @@
#include "counter.h"
-#include <sys/file.h>
+#include <fcntl.h>
#include <base/eintr_wrapper.h>
#include <base/logging.h>
@@ -12,26 +12,28 @@
namespace chromeos_metrics {
// TaggedCounter::Record implementation.
-void TaggedCounter::Record::Init(int tag, int count) {
+void TaggedCounter::Record::Init(int32 tag, int32 count) {
tag_ = tag;
count_ = (count > 0) ? count : 0;
}
-void TaggedCounter::Record::Add(int count) {
+void TaggedCounter::Record::Add(int32 count) {
if (count <= 0)
return;
- count_ += count;
-
- // Saturates on postive overflow.
- if (count_ < 0) {
- count_ = INT_MAX;
- }
+ // Saturates on positive overflow.
+ int64 new_count = static_cast<int64>(count_) + static_cast<int64>(count);
+ if (new_count > kint32max)
+ count_ = kint32max;
+ else
+ count_ = static_cast<int32>(new_count);
}
// TaggedCounter implementation.
TaggedCounter::TaggedCounter()
- : filename_(NULL), reporter_(NULL), reporter_handle_(NULL),
+ : filename_(NULL),
+ reporter_(NULL),
+ reporter_handle_(NULL),
record_state_(kRecordInvalid) {}
TaggedCounter::~TaggedCounter() {}
@@ -45,7 +47,7 @@ void TaggedCounter::Init(const char* filename,
record_state_ = kRecordInvalid;
}
-void TaggedCounter::Update(int tag, int count) {
+void TaggedCounter::Update(int32 tag, int32 count) {
UpdateInternal(tag,
count,
false); // No flush.
@@ -57,7 +59,7 @@ void TaggedCounter::Flush() {
true); // Do flush.
}
-void TaggedCounter::UpdateInternal(int tag, int count, bool flush) {
+void TaggedCounter::UpdateInternal(int32 tag, int32 count, bool flush) {
// If there's no new data and the last record in the aggregation
// file is with the same tag, there's nothing to do.
if (!flush && count <= 0 &&
@@ -99,11 +101,10 @@ void TaggedCounter::ReadRecord(int fd) {
record_state_ = kRecordNullDirty;
return;
}
-
record_state_ = kRecordNull;
}
-void TaggedCounter::ReportRecord(int tag, bool flush) {
+void TaggedCounter::ReportRecord(int32 tag, bool flush) {
// If no valid record, there's nothing to report.
if (record_state_ != kRecordValid) {
DCHECK(record_state_ == kRecordNull);
@@ -121,7 +122,7 @@ void TaggedCounter::ReportRecord(int tag, bool flush) {
record_state_ = kRecordNullDirty;
}
-void TaggedCounter::UpdateRecord(int tag, int count) {
+void TaggedCounter::UpdateRecord(int32 tag, int32 count) {
if (count <= 0)
return;
« no previous file with comments | « counter.h ('k') | counter_mock.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698