folly: replace old-style header guards with "pragma once"
[folly.git] / folly / experimental / symbolizer / ElfCache.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <cstring>
20 #include <limits.h>  // for PATH_MAX
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include <vector>
25 #include <unordered_map>
26
27 #include <boost/operators.hpp>
28 #include <boost/container/flat_map.hpp>
29 #include <boost/intrusive/list.hpp>
30 #include <glog/logging.h>
31
32 #include <folly/experimental/symbolizer/Elf.h>
33
34 namespace folly { namespace symbolizer {
35
36 class ElfCacheBase {
37  public:
38   virtual std::shared_ptr<ElfFile> getFile(StringPiece path) = 0;
39   virtual ~ElfCacheBase() { }
40 };
41
42 /**
43  * Cache ELF files. Async-signal-safe: does memory allocation upfront.
44  *
45  * Will not grow; once the capacity is reached, lookups for files that
46  * aren't already in the cache will fail (return nullptr).
47  *
48  * Not MT-safe. May not be used concurrently from multiple threads.
49  *
50  * NOTE that async-signal-safety is preserved only as long as the
51  * SignalSafeElfCache object exists; after the SignalSafeElfCache object
52  * is destroyed, destroying returned shared_ptr<ElfFile> objects may
53  * cause ElfFile objects to be destroyed, and that's not async-signal-safe.
54  */
55 class SignalSafeElfCache : public ElfCacheBase {
56  public:
57   explicit SignalSafeElfCache(size_t capacity);
58
59   std::shared_ptr<ElfFile> getFile(StringPiece path) override;
60
61  private:
62   // We can't use std::string (allocating memory is bad!) so we roll our
63   // own wrapper around a fixed-size, null-terminated string.
64   class Path : private boost::totally_ordered<Path> {
65    public:
66     explicit Path(StringPiece s) {
67       DCHECK_LE(s.size(), kMaxSize);
68       memcpy(data_, s.data(), s.size());
69       data_[s.size()] = '\0';
70     }
71
72     bool operator<(const Path& other) const {
73       return strcmp(data_, other.data_) < 0;
74     }
75
76     bool operator==(const Path& other) const {
77       return strcmp(data_, other.data_) == 0;
78     }
79
80     const char* data() const {
81       return data_;
82     }
83
84     static constexpr size_t kMaxSize = PATH_MAX - 1;
85
86    private:
87     char data_[kMaxSize + 1];
88   };
89
90   boost::container::flat_map<Path, int> map_;
91   std::vector<std::shared_ptr<ElfFile>> slots_;
92 };
93
94 /**
95  * General-purpose ELF file cache.
96  *
97  * LRU of given capacity. MT-safe (uses locking). Not async-signal-safe.
98  */
99 class ElfCache : public ElfCacheBase {
100  public:
101   explicit ElfCache(size_t capacity);
102
103   std::shared_ptr<ElfFile> getFile(StringPiece path) override;
104
105  private:
106   std::mutex mutex_;
107
108   typedef boost::intrusive::list_member_hook<> LruLink;
109
110   struct Entry {
111     std::string path;
112     ElfFile file;
113     LruLink lruLink;
114   };
115
116   static std::shared_ptr<ElfFile> filePtr(const std::shared_ptr<Entry>& e);
117
118   size_t capacity_;
119   std::unordered_map<
120     StringPiece,
121     std::shared_ptr<Entry>,
122     StringPieceHash> files_;
123
124   typedef boost::intrusive::list<
125       Entry,
126       boost::intrusive::member_hook<Entry, LruLink, &Entry::lruLink>,
127       boost::intrusive::constant_time_size<false>> LruList;
128   LruList lruList_;
129 };
130
131 }}  // namespaces