OSDN Git Service

Simpleperf: don't use designation in struct member initialization.
authorYabin Cui <yabinc@google.com>
Thu, 25 Jun 2015 20:23:55 +0000 (13:23 -0700)
committerYabin Cui <yabinc@google.com>
Thu, 25 Jun 2015 21:34:42 +0000 (14:34 -0700)
designation is not supported in c++ standard, and partially initialized
struct can be unsafe.
And use constexpr when desired.

Bug: 19483574
Change-Id: I4f445845a7d97aeb685f02176485c70f8b0ca995

simpleperf/environment.h
simpleperf/record_file_format.h
simpleperf/sample_tree.cpp
simpleperf/sample_tree.h

index cfa324c..aa6f5eb 100644 (file)
@@ -26,7 +26,7 @@
 
 std::vector<int> GetOnlineCpus();
 
-static const char* DEFAULT_KERNEL_MMAP_NAME = "[kernel.kallsyms]_text";
+constexpr char DEFAULT_KERNEL_MMAP_NAME[] = "[kernel.kallsyms]_text";
 
 struct KernelMmap {
   std::string name;
@@ -51,7 +51,7 @@ struct ThreadComm {
 
 bool GetThreadComms(std::vector<ThreadComm>* thread_comms);
 
-static const char* DEFAULT_EXECNAME_FOR_THREAD_MMAP = "//anon";
+constexpr char DEFAULT_EXECNAME_FOR_THREAD_MMAP[] = "//anon";
 
 struct ThreadMmap {
   uint64_t start_addr;
@@ -63,7 +63,7 @@ struct ThreadMmap {
 
 bool GetThreadMmapsInProcess(pid_t pid, std::vector<ThreadMmap>* thread_mmaps);
 
-static const char* DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID = "[kernel.kallsyms]";
+constexpr char DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID[] = "[kernel.kallsyms]";
 
 bool GetKernelBuildId(BuildId* build_id);
 bool GetModuleBuildId(const std::string& module_name, BuildId* build_id);
index 9758f11..da6434b 100644 (file)
@@ -63,7 +63,7 @@ struct SectionDesc {
   uint64_t size;
 };
 
-static const char* PERF_MAGIC = "PERFILE2";
+constexpr char PERF_MAGIC[] = "PERFILE2";
 
 struct FileHeader {
   char magic[8];
index 8030c48..89d632c 100644 (file)
@@ -37,7 +37,9 @@ void SampleTree::AddThread(int pid, int tid, const std::string& comm) {
   auto it = thread_tree_.find(tid);
   if (it == thread_tree_.end()) {
     ThreadEntry* thread = new ThreadEntry{
-        .pid = pid, .tid = tid,
+        pid, tid,
+        "unknown",                             // comm
+        std::set<MapEntry*, MapComparator>(),  // maps
     };
     auto pair = thread_tree_.insert(std::make_pair(tid, std::unique_ptr<ThreadEntry>(thread)));
     CHECK(pair.second);
@@ -75,7 +77,7 @@ void SampleTree::AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff,
   }
   DsoEntry* dso = FindKernelDsoOrNew(filename);
   MapEntry* map = new MapEntry{
-      .start_addr = start_addr, .len = len, .pgoff = pgoff, .time = time, .dso = dso,
+      start_addr, len, pgoff, time, dso,
   };
   map_storage_.push_back(std::unique_ptr<MapEntry>(map));
   RemoveOverlappedMap(&kernel_map_tree_, map);
@@ -103,7 +105,7 @@ void SampleTree::AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t le
   ThreadEntry* thread = FindThreadOrNew(pid, tid);
   DsoEntry* dso = FindUserDsoOrNew(filename);
   MapEntry* map = new MapEntry{
-      .start_addr = start_addr, .len = len, .pgoff = pgoff, .time = time, .dso = dso,
+      start_addr, len, pgoff, time, dso,
   };
   map_storage_.push_back(std::unique_ptr<MapEntry>(map));
   RemoveOverlappedMap(&thread->maps, map);
@@ -138,7 +140,11 @@ static bool IsIpInMap(uint64_t ip, const MapEntry* map) {
 const MapEntry* SampleTree::FindMap(const ThreadEntry* thread, uint64_t ip, bool in_kernel) {
   // Construct a map_entry which is strictly after the searched map_entry, based on MapComparator.
   MapEntry find_map = {
-      .start_addr = ip, .len = static_cast<uint64_t>(-1), .time = static_cast<uint64_t>(-1),
+      ip,          // start_addr
+      ULLONG_MAX,  // len
+      0,           // pgoff
+      ULLONG_MAX,  // time
+      nullptr,     // dso
   };
   if (!in_kernel) {
     auto it = thread->maps.upper_bound(&find_map);
@@ -161,14 +167,17 @@ void SampleTree::AddSample(int pid, int tid, uint64_t ip, uint64_t time, uint64_
   const SymbolEntry* symbol = FindSymbol(map, ip);
 
   SampleEntry sample = {
-      .ip = ip,
-      .time = time,
-      .period = period,
-      .sample_count = 1,
-      .thread = thread,
-      .thread_comm = thread->comm,
-      .map = map,
-      .symbol = symbol,
+      ip, time, period,
+      1,  // sample_count
+      thread,
+      thread->comm,  // thead_comm
+      map, symbol,
+      BranchFromEntry{
+          0,        // ip
+          nullptr,  // map
+          nullptr,  // symbol
+          0,        // flags
+      },
   };
   InsertSample(sample);
 }
@@ -187,20 +196,19 @@ void SampleTree::AddBranchSample(int pid, int tid, uint64_t from_ip, uint64_t to
   }
   const SymbolEntry* to_symbol = FindSymbol(to_map, to_ip);
 
-  BranchFromEntry branch_from = {
-      .ip = from_ip, .map = from_map, .symbol = from_symbol, .flags = branch_flags,
-  };
-  SampleEntry sample = {
-      .ip = to_ip,
-      .time = time,
-      .period = period,
-      .sample_count = 1,
-      .thread = thread,
-      .thread_comm = thread->comm,
-      .map = to_map,
-      .symbol = to_symbol,
-      .branch_from = branch_from,
-  };
+  SampleEntry sample = {to_ip,  // ip
+                        time, period,
+                        1,  // sample_count
+                        thread,
+                        thread->comm,  // thread_comm
+                        to_map,        // map
+                        to_symbol,     // symbol
+                        BranchFromEntry{
+                            from_ip,       // ip
+                            from_map,      // map
+                            from_symbol,   // symbol
+                            branch_flags,  // flags
+                        }};
   InsertSample(sample);
 }
 
index cbb0646..8ed2e99 100644 (file)
@@ -75,14 +75,18 @@ class SampleTree {
         sorted_sample_tree_(sorted_sample_comparator_),
         total_samples_(0),
         total_period_(0) {
-    unknown_map_ = {
-        .start_addr = 0, .len = ULLONG_MAX, .pgoff = 0, .time = 0, .dso = &unknown_dso_,
+    unknown_map_ = MapEntry{
+        0,              // start_addr
+        ULLONG_MAX,     // len
+        0,              // pgoff
+        0,              // time
+        &unknown_dso_,  // dso
     };
-    unknown_dso_ = {
-        .path = "unknown",
-    };
-    unknown_symbol_ = {
-        .name = "unknown", .addr = 0, .len = ULLONG_MAX,
+    unknown_dso_.path = "unknown";
+    unknown_symbol_ = SymbolEntry{
+        "unknown",   // name
+        0,           // addr
+        ULLONG_MAX,  // len
     };
   }