Changeset 4964 for icGREP


Ignore:
Timestamp:
Mar 8, 2016, 3:15:25 PM (3 years ago)
Author:
hongpum
Message:

Refine path representation and default CacheDir?

Location:
icGREP/icgrep-devel/icgrep
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/object_cache.cpp

    r4962 r4964  
    1818// directory specified by CacheDir, using a filename provided in the module
    1919// descriptor. The cache tries to load a saved object using that path if the
    20 // file exists. CacheDir defaults to "", in which case objects are cached
    21 // alongside their originating bitcodes.
     20// file exists.
    2221//
    23 ICGrepObjectCache::ICGrepObjectCache(const std::string& CacheDir) : CacheDir(CacheDir) {
    24     // Add trailing '/' to cache dir if necessary.
    25     if (!this->CacheDir.empty() &&
    26             this->CacheDir[this->CacheDir.size() - 1] != '/')
    27         this->CacheDir += '/';
     22ICGrepObjectCache::ICGrepObjectCache(const std::string &dir): CacheDir(dir) {}
     23
     24ICGrepObjectCache::ICGrepObjectCache() {
     25    // $HOME/.cache/icgrep
     26    // TODO use path::user_cache_directory once we have llvm >= 3.7.
     27    sys::path::home_directory(CacheDir);
     28    sys::path::append(CacheDir, ".cache", "icgrep");
    2829}
    2930
     
    3132
    3233void ICGrepObjectCache::notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) {
    33     const std::string ModuleID = M->getModuleIdentifier();
    34     std::string CacheName;
     34    const std::string &ModuleID = M->getModuleIdentifier();
     35    Path CacheName(CacheDir);
    3536    if (!getCacheFilename(ModuleID, CacheName))
    3637        return;
    37     // TODO use a cleaner, more universal fs API
    38     if (!CacheDir.empty()) { // Create user-defined cache dir.
    39         SmallString<128> dir(CacheName);
    40         sys::path::remove_filename(dir);
    41         sys::fs::create_directories(Twine(dir));
    42     }
     38    if (!CacheDir.empty())      // Re-creating an existing directory is fine.
     39        sys::fs::create_directories(Twine(CacheDir));
    4340    std::error_code EC;
    4441    raw_fd_ostream outfile(CacheName, EC, sys::fs::F_None);
     
    4643    outfile.close();
    4744#ifdef OBJECT_CACHE_DEBUG
    48     std::cerr << "Cache created: " << CacheName << std::endl;
     45    std::cerr << "Cache created: " << CacheName.c_str() << std::endl;
    4946#endif
    5047}
    5148
    5249std::unique_ptr<MemoryBuffer> ICGrepObjectCache::getObject(const Module* M) {
    53     const std::string ModuleID = M->getModuleIdentifier();
    54     std::string CacheName;
     50    const std::string &ModuleID = M->getModuleIdentifier();
     51    Path CacheName(CacheDir);
    5552    if (!getCacheFilename(ModuleID, CacheName))
    5653        return nullptr;
     
    7168}
    7269
    73 bool ICGrepObjectCache::getCacheFilename(const std::string &ModID, std::string &CacheName) {
     70bool ICGrepObjectCache::getCacheFilename(const std::string &ModID, Path &CacheName) {
    7471#ifdef OBJECT_CACHE_DEBUG
    7572    std::cerr << "ModuleID: " << ModID << std::endl;
    7673#endif
    77     const static std::string Prefix("grepcode:");
     74    const std::string Prefix("grepcode:");
    7875    size_t PrefixLength = Prefix.length();
    7976    if (ModID.substr(0, PrefixLength) != Prefix)
    8077        return false;
    81 /*
    82 //TODO
    83 #if defined(_WIN32)
    84     // Transform "X:\foo" => "/X\foo" for convenience.
    85     if (isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') {
    86         CacheSubdir[1] = CacheSubdir[0];
    87         CacheSubdir[0] = '/';
    88     }
    89 #endif
    90 */
    91     CacheName = CacheDir + ModID.substr(PrefixLength) + ".o";
     78
     79    CacheName = CacheDir;
     80    sys::path::append(CacheName, ModID.substr(PrefixLength) + ".o");
    9281#ifdef OBJECT_CACHE_DEBUG
    93     std::cerr << "CacheName: " << CacheName << std::endl;
     82    std::cerr << "CacheName: " << CacheName.c_str() << std::endl;
    9483#endif
    9584    return true;
  • icGREP/icgrep-devel/icgrep/object_cache.h

    r4962 r4964  
    77#include <llvm/Support/MemoryBuffer.h>
    88#include <llvm/ExecutionEngine/ObjectCache.h>
     9#include <llvm/ADT/SmallString.h>
    910
    1011class ICGrepObjectCache : public llvm::ObjectCache {
    1112    public:
    12         ICGrepObjectCache(const std::string& CacheDir);
     13        ICGrepObjectCache(const std::string &dir);
     14        ICGrepObjectCache();
    1315        virtual ~ICGrepObjectCache();
    1416
     
    1719
    1820    private:
    19         std::string CacheDir;
     21        const static size_t mPathInitLength = 256;
     22        typedef llvm::SmallString<mPathInitLength> Path;
     23        Path CacheDir;
    2024
    21         bool getCacheFilename(const std::string &ModID, std::string &CacheName);
     25        bool getCacheFilename(const std::string &ModID, Path &CacheName);
    2226};
    2327
  • icGREP/icgrep-devel/icgrep/toolchain.cpp

    r4962 r4964  
    460460
    461461    if (EnableObjectCache) {
    462         // TODO a better default cache diretory (HOME expansion?)
    463462        if (ObjectCacheDir.empty())
    464             cache = new ICGrepObjectCache(".icgrep-cache/");
     463            // Default is $HOME/.cache/icgrep
     464            cache = new ICGrepObjectCache();
    465465        else
    466466            cache = new ICGrepObjectCache(ObjectCacheDir);
Note: See TracChangeset for help on using the changeset viewer.