Refactor to share map_info amongst threads.

Allow the use of the same map info to be shared when getting information on
multiple threads from the same pid.

Change-Id: I2e460e20154a10f4894ae563331fb32179e4551f
diff --git a/include/backtrace/Backtrace.h b/include/backtrace/Backtrace.h
index b15678c..bf4efd3 100644
--- a/include/backtrace/Backtrace.h
+++ b/include/backtrace/Backtrace.h
@@ -33,7 +33,9 @@
   // If pid >= 0 and tid < 0, then the Backtrace object corresponds to a
   // different process.
   // Tracing a thread in a different process is not supported.
-  static Backtrace* Create(pid_t pid, pid_t tid);
+  // If map_info is NULL, then create the map and manage it internally.
+  // If map_info is not NULL, the map is still owned by the caller.
+  static Backtrace* Create(pid_t pid, pid_t tid, backtrace_map_info_t* map_info = NULL);
 
   virtual ~Backtrace();
 
@@ -70,7 +72,7 @@
   }
 
 protected:
-  Backtrace(BacktraceImpl* impl);
+  Backtrace(BacktraceImpl* impl, pid_t pid, backtrace_map_info_t* map_info);
 
   virtual bool VerifyReadWordArgs(uintptr_t ptr, uint32_t* out_value);
 
@@ -78,6 +80,8 @@
 
   backtrace_map_info_t* map_info_;
 
+  bool map_info_requires_delete_;
+
   backtrace_t backtrace_;
 
   friend class BacktraceImpl;
diff --git a/include/backtrace/backtrace.h b/include/backtrace/backtrace.h
index 8a45690..186d327 100644
--- a/include/backtrace/backtrace.h
+++ b/include/backtrace/backtrace.h
@@ -73,6 +73,14 @@
 bool backtrace_create_context(
     backtrace_context_t* context, pid_t pid, pid_t tid, size_t num_ignore_frames);
 
+/* The same as backtrace_create_context, except that it is assumed that
+ * the pid map has already been acquired and the caller will handle freeing
+ * the map data.
+ */
+bool backtrace_create_context_with_map(
+    backtrace_context_t* context, pid_t pid, pid_t tid, size_t num_ignore_frames,
+    backtrace_map_info_t* map_info);
+
 /* Gather the backtrace data for a pthread instead of a process. */
 bool backtrace_create_thread_context(
     backtrace_context_t* context, pid_t tid, size_t num_ignore_frames);