[Lldb-commits] [lldb] r235011 - Add Modulecache::GetAndPut method which wraps sequence of Get and Put (if module wasn't found in cache) calls.

Oleksiy Vyalov ovyalov at google.com
Wed Apr 15 07:35:11 PDT 2015


Author: ovyalov
Date: Wed Apr 15 09:35:10 2015
New Revision: 235011

URL: http://llvm.org/viewvc/llvm-project?rev=235011&view=rev
Log:
Add Modulecache::GetAndPut method which wraps sequence of Get and Put (if module wasn't found in cache) calls.

http://reviews.llvm.org/D9013

Modified:
    lldb/trunk/source/Target/Platform.cpp
    lldb/trunk/source/Utility/ModuleCache.cpp
    lldb/trunk/source/Utility/ModuleCache.h

Modified: lldb/trunk/source/Target/Platform.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Platform.cpp?rev=235011&r1=235010&r2=235011&view=diff
==============================================================================
--- lldb/trunk/source/Target/Platform.cpp (original)
+++ lldb/trunk/source/Target/Platform.cpp Wed Apr 15 09:35:10 2015
@@ -37,7 +37,6 @@
 #include "lldb/Target/Target.h"
 #include "lldb/Utility/Utils.h"
 #include "llvm/Support/FileSystem.h"
-#include "llvm/Support/FileUtilities.h"
 
 #include "Utility/ModuleCache.h"
 
@@ -1837,67 +1836,35 @@ Platform::GetCachedSharedModule (const M
     Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PLATFORM);
 
     // Check local cache for a module.
-    auto error = m_module_cache->Get (GetModuleCacheRoot (),
-                                      GetCacheHostname (),
-                                      module_spec,
-                                      module_sp,
-                                      did_create_ptr);
+    auto error = m_module_cache->GetAndPut (
+        GetModuleCacheRoot (),
+        GetCacheHostname (),
+        module_spec,
+        [=](const ModuleSpec &module_spec, FileSpec &tmp_download_file_spec)
+        {
+            // Get temporary file name for a downloaded module.
+            llvm::SmallString<PATH_MAX> tmp_download_file_path;
+            const auto err_code = llvm::sys::fs::createTemporaryFile (
+                "lldb", module_spec.GetUUID ().GetAsString ().c_str (), tmp_download_file_path);
+            if (err_code)
+                return Error ("Failed to create temp file: %s", err_code.message ().c_str ());
+
+            tmp_download_file_spec.SetFile (tmp_download_file_path.c_str (), true);
+            return DownloadModuleSlice (module_spec.GetFileSpec (),
+                                        module_spec.GetObjectOffset (),
+                                        module_spec.GetObjectSize (),
+                                        tmp_download_file_spec);
+
+        },
+        module_sp,
+        did_create_ptr);
     if (error.Success ())
         return true;
 
     if (log)
         log->Printf("Platform::%s - module %s not found in local cache: %s",
                     __FUNCTION__, module_spec.GetUUID ().GetAsString ().c_str (), error.AsCString ());
-
-    // Get temporary file name for a downloaded module.
-    llvm::SmallString<PATH_MAX> tmp_download_file_path;
-    const auto err_code = llvm::sys::fs::createTemporaryFile (
-        "lldb", module_spec.GetUUID ().GetAsString ().c_str (), tmp_download_file_path);
-    if (err_code)
-    {
-        if (log)
-            log->Printf ("Platform::%s - failed to create unique file: %s",
-                         __FUNCTION__, err_code.message ().c_str ());
-        return false;
-    }
-
-    llvm::FileRemover tmp_file_remover (tmp_download_file_path.c_str ());
-
-    const FileSpec tmp_download_file_spec (tmp_download_file_path.c_str (), true);
-    // Download a module file.
-    error = DownloadModuleSlice (module_spec.GetFileSpec (),
-                                 module_spec.GetObjectOffset (),
-                                 module_spec.GetObjectSize (),
-                                 tmp_download_file_spec);
-    if (error.Fail ())
-    {
-        if (log)
-            log->Printf("Platform::%s - failed to download %s to %s: %s",
-                        __FUNCTION__, module_spec.GetFileSpec ().GetPath ().c_str (),
-                        tmp_download_file_path.c_str (), error.AsCString ());
-        return false;
-    }
-
-    // Put downloaded file into local module cache.
-    error = m_module_cache->Put (GetModuleCacheRoot (),
-                                 GetCacheHostname (),
-                                 module_spec,
-                                 tmp_download_file_spec);
-    if (error.Fail ())
-    {
-        if (log)
-            log->Printf("Platform::%s - failed to put module %s into cache: %s",
-                        __FUNCTION__, module_spec.GetUUID ().GetAsString ().c_str (),
-                        error.AsCString ());
-        return false;
-    }
-
-    error = m_module_cache->Get (GetModuleCacheRoot (),
-                                 GetCacheHostname (),
-                                 module_spec,
-                                 module_sp,
-                                 did_create_ptr);
-    return error.Success ();
+    return false;
 }
 
 Error

Modified: lldb/trunk/source/Utility/ModuleCache.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/ModuleCache.cpp?rev=235011&r1=235010&r2=235011&view=diff
==============================================================================
--- lldb/trunk/source/Utility/ModuleCache.cpp (original)
+++ lldb/trunk/source/Utility/ModuleCache.cpp Wed Apr 15 09:35:10 2015
@@ -13,6 +13,7 @@
 #include "lldb/Core/ModuleSpec.h"
 #include "lldb/Host/FileSystem.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/FileUtilities.h"
 
 #include <assert.h>
 
@@ -118,6 +119,44 @@ ModuleCache::Get (const FileSpec &root_d
     return Error ();
 }
 
+Error
+ModuleCache::GetAndPut (const FileSpec &root_dir_spec,
+                        const char *hostname,
+                        const ModuleSpec &module_spec,
+                        const Downloader &downloader,
+                        lldb::ModuleSP &cached_module_sp,
+                        bool *did_create_ptr)
+{
+    // Check local cache for a module.
+    auto error = Get (root_dir_spec,
+                      hostname,
+                      module_spec,
+                      cached_module_sp,
+                      did_create_ptr);
+    if (error.Success ())
+        return error;
+
+    FileSpec tmp_download_file_spec;
+    error = downloader (module_spec, tmp_download_file_spec);
+    llvm::FileRemover tmp_file_remover (tmp_download_file_spec.GetPath ().c_str ());
+    if (error.Fail ())
+        return Error("Failed to download module: %s", error.AsCString ());
+
+    // Put downloaded file into local module cache.
+    error = Put (root_dir_spec,
+                 hostname,
+                 module_spec,
+                 tmp_download_file_spec);
+    if (error.Fail ())
+        return Error ("Failed to put module into cache: %s", error.AsCString ());
+
+    return Get (root_dir_spec,
+                hostname,
+                module_spec,
+                cached_module_sp,
+                did_create_ptr);
+}
+
 FileSpec
 ModuleCache::GetModuleDirectory (const FileSpec &root_dir_spec, const UUID &uuid)
 {

Modified: lldb/trunk/source/Utility/ModuleCache.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/ModuleCache.h?rev=235011&r1=235010&r2=235011&view=diff
==============================================================================
--- lldb/trunk/source/Utility/ModuleCache.h (original)
+++ lldb/trunk/source/Utility/ModuleCache.h Wed Apr 15 09:35:10 2015
@@ -16,6 +16,7 @@
 #include "lldb/Core/Error.h"
 #include "lldb/Host/FileSpec.h"
 
+#include <functional>
 #include <string>
 #include <unordered_map>
 
@@ -44,6 +45,8 @@ class UUID;
 class ModuleCache
 {
 public:
+    using Downloader = std::function<Error (const ModuleSpec&, FileSpec&)>;
+
     Error
     Put (const FileSpec &root_dir_spec,
          const char *hostname,
@@ -57,6 +60,14 @@ public:
          lldb::ModuleSP &cached_module_sp,
          bool *did_create_ptr);
 
+    Error
+    GetAndPut(const FileSpec &root_dir_spec,
+              const char *hostname,
+              const ModuleSpec &module_spec,
+              const Downloader &downloader,
+              lldb::ModuleSP &cached_module_sp,
+              bool *did_create_ptr);
+
 private:
     static FileSpec
     GetModuleDirectory (const FileSpec &root_dir_spec, const UUID &uuid);





More information about the lldb-commits mailing list