proxygen
folly::symbolizer::ElfCache Class Reference

#include <ElfCache.h>

Inheritance diagram for folly::symbolizer::ElfCache:
folly::symbolizer::ElfCacheBase

Classes

struct  Entry
 

Public Member Functions

 ElfCache (size_t capacity)
 
std::shared_ptr< ElfFilegetFile (StringPiece path) override
 
- Public Member Functions inherited from folly::symbolizer::ElfCacheBase
virtual ~ElfCacheBase ()
 

Private Types

typedef boost::intrusive::list_member_hook LruLink
 
typedef boost::intrusive::list< Entry, boost::intrusive::member_hook< Entry, LruLink,&Entry::lruLink >, boost::intrusive::constant_time_size< false > > LruList
 

Static Private Member Functions

static std::shared_ptr< ElfFilefilePtr (const std::shared_ptr< Entry > &e)
 

Private Attributes

std::mutex mutex_
 
size_t capacity_
 
std::unordered_map< StringPiece, std::shared_ptr< Entry >, Hashfiles_
 
LruList lruList_
 

Detailed Description

General-purpose ELF file cache.

LRU of given capacity. MT-safe (uses locking). Not async-signal-safe.

Definition at line 118 of file ElfCache.h.

Member Typedef Documentation

typedef boost::intrusive::list_member_hook folly::symbolizer::ElfCache::LruLink
private

Definition at line 127 of file ElfCache.h.

typedef boost::intrusive::list< Entry, boost::intrusive::member_hook<Entry, LruLink, &Entry::lruLink>, boost::intrusive::constant_time_size<false> > folly::symbolizer::ElfCache::LruList
private

Definition at line 144 of file ElfCache.h.

Constructor & Destructor Documentation

folly::symbolizer::ElfCache::ElfCache ( size_t  capacity)
explicit

Definition at line 92 of file ElfCache.cpp.

92 : capacity_(capacity) {}

Member Function Documentation

std::shared_ptr< ElfFile > folly::symbolizer::ElfCache::filePtr ( const std::shared_ptr< Entry > &  e)
staticprivate

Definition at line 129 of file ElfCache.cpp.

Referenced by getFile().

129  {
130  // share ownership
131  return std::shared_ptr<ElfFile>(e, &e->file);
132 }
std::shared_ptr< ElfFile > folly::symbolizer::ElfCache::getFile ( StringPiece  path)
overridevirtual

Implements folly::symbolizer::ElfCacheBase.

Definition at line 94 of file ElfCache.cpp.

References capacity_, filePtr(), files_, folly::symbolizer::ElfFile::kSuccess, folly::lock(), lruList_, mutex_, and folly::Range< Iter >::str().

94  {
95  std::lock_guard<std::mutex> lock(mutex_);
96 
97  auto pos = files_.find(p);
98  if (pos != files_.end()) {
99  // Found, move to back (MRU)
100  auto& entry = pos->second;
101  lruList_.erase(lruList_.iterator_to(*entry));
102  lruList_.push_back(*entry);
103  return filePtr(entry);
104  }
105 
106  auto entry = std::make_shared<Entry>();
107  entry->path = p.str();
108  auto& path = entry->path;
109 
110  // No negative caching
111  const char* msg = "";
112  int r = entry->file.openAndFollow(path.c_str(), true, &msg);
113  if (r != ElfFile::kSuccess) {
114  return nullptr;
115  }
116 
117  if (files_.size() == capacity_) {
118  auto& e = lruList_.front();
119  lruList_.pop_front();
120  files_.erase(e.path);
121  }
122 
123  files_.emplace(entry->path, entry);
124  lruList_.push_back(*entry);
125 
126  return filePtr(entry);
127 }
std::unordered_map< StringPiece, std::shared_ptr< Entry >, Hash > files_
Definition: ElfCache.h:138
static std::shared_ptr< ElfFile > filePtr(const std::shared_ptr< Entry > &e)
Definition: ElfCache.cpp:129
auto lock(Synchronized< D, M > &synchronized, Args &&...args)

Member Data Documentation

size_t folly::symbolizer::ElfCache::capacity_
private

Definition at line 137 of file ElfCache.h.

Referenced by getFile().

std::unordered_map<StringPiece, std::shared_ptr<Entry>, Hash> folly::symbolizer::ElfCache::files_
private

Definition at line 138 of file ElfCache.h.

Referenced by getFile().

LruList folly::symbolizer::ElfCache::lruList_
private

Definition at line 145 of file ElfCache.h.

Referenced by getFile().

std::mutex folly::symbolizer::ElfCache::mutex_
private

Definition at line 125 of file ElfCache.h.

Referenced by getFile().


The documentation for this class was generated from the following files: