libprocinfo: provide MapInfo structure for reading maps.

Bug: none
Test: run libprocinfo_test.
Change-Id: I4d3c0f3012e91571aef7ebf4b154df1c9c5addc2
diff --git a/libprocinfo/include/procinfo/process_map.h b/libprocinfo/include/procinfo/process_map.h
index 3771f9f..0fc4201 100644
--- a/libprocinfo/include/procinfo/process_map.h
+++ b/libprocinfo/include/procinfo/process_map.h
@@ -22,6 +22,7 @@
 
 #include <functional>
 #include <string>
+#include <vector>
 
 #include <android-base/file.h>
 
@@ -147,5 +148,23 @@
   return ReadMapFile("/proc/" + std::to_string(pid) + "/maps", callback);
 }
 
+struct MapInfo {
+  uint64_t start;
+  uint64_t end;
+  uint16_t flags;
+  uint64_t pgoff;
+  std::string name;
+
+  MapInfo(uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name)
+      : start(start), end(end), flags(flags), pgoff(pgoff), name(name) {}
+};
+
+inline bool ReadProcessMaps(pid_t pid, std::vector<MapInfo>* maps) {
+  return ReadProcessMaps(
+      pid, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name) {
+        maps->emplace_back(start, end, flags, pgoff, name);
+      });
+}
+
 } /* namespace procinfo */
 } /* namespace android */
diff --git a/libprocinfo/process_map_benchmark.cpp b/libprocinfo/process_map_benchmark.cpp
index d9e8a4d..04995d4 100644
--- a/libprocinfo/process_map_benchmark.cpp
+++ b/libprocinfo/process_map_benchmark.cpp
@@ -27,21 +27,10 @@
 
 #include <benchmark/benchmark.h>
 
-struct MapInfo {
-  uint64_t start;
-  uint64_t end;
-  uint16_t flags;
-  uint64_t pgoff;
-  const std::string name;
-
-  MapInfo(uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name)
-      : start(start), end(end), flags(flags), pgoff(pgoff), name(name) {}
-};
-
 static void BM_ReadMapFile(benchmark::State& state) {
   std::string map_file = android::base::GetExecutableDirectory() + "/testdata/maps";
   for (auto _ : state) {
-    std::vector<MapInfo> maps;
+    std::vector<android::procinfo::MapInfo> maps;
     android::procinfo::ReadMapFile(
         map_file, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff,
                       const char* name) { maps.emplace_back(start, end, flags, pgoff, name); });
diff --git a/libprocinfo/process_map_test.cpp b/libprocinfo/process_map_test.cpp
index 4b93c5b..170a806 100644
--- a/libprocinfo/process_map_test.cpp
+++ b/libprocinfo/process_map_test.cpp
@@ -22,20 +22,9 @@
 
 #include <gtest/gtest.h>
 
-struct MapInfo {
-  uint64_t start;
-  uint64_t end;
-  uint16_t flags;
-  uint64_t pgoff;
-  const std::string name;
-
-  MapInfo(uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name)
-      : start(start), end(end), flags(flags), pgoff(pgoff), name(name) {}
-};
-
-TEST(process_map, smoke) {
+TEST(process_map, ReadMapFile) {
   std::string map_file = android::base::GetExecutableDirectory() + "/testdata/maps";
-  std::vector<MapInfo> maps;
+  std::vector<android::procinfo::MapInfo> maps;
   ASSERT_TRUE(android::procinfo::ReadMapFile(
       map_file, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff,
                     const char* name) { maps.emplace_back(start, end, flags, pgoff, name); }));
@@ -58,3 +47,14 @@
             "[anon:dalvik-classes.dex extracted in memory from "
             "/data/app/com.google.sample.tunnel-HGGRU03Gu1Mwkf_-RnFmvw==/base.apk]");
 }
+
+TEST(process_map, ReadProcessMaps) {
+  std::vector<android::procinfo::MapInfo> maps;
+  ASSERT_TRUE(android::procinfo::ReadProcessMaps(
+      getpid(), [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff,
+                    const char* name) { maps.emplace_back(start, end, flags, pgoff, name); }));
+  ASSERT_GT(maps.size(), 0u);
+  maps.clear();
+  ASSERT_TRUE(android::procinfo::ReadProcessMaps(getpid(), &maps));
+  ASSERT_GT(maps.size(), 0u);
+}