Revert D6050464: [Folly] Move folly/Hash.h to folly/hash/
[folly.git] / folly / experimental / symbolizer / ElfCache.h
index a2d8993a44767ff31f1851eebaa60aaaf22c5bd8..b9d7d966b6a034703ed535de1e85b5661ed86122 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * limitations under the License.
  */
 
-#ifndef FOLLY_SYMBOLIZER_ELFCACHE_H_
-#define FOLLY_SYMBOLIZER_ELFCACHE_H_
+#pragma once
 
+#include <climits> // for PATH_MAX
 #include <cstring>
-#include <limits.h>  // for PATH_MAX
 #include <memory>
 #include <mutex>
 #include <string>
-#include <vector>
 #include <unordered_map>
+#include <vector>
 
-#include <boost/operators.hpp>
 #include <boost/container/flat_map.hpp>
 #include <boost/intrusive/list.hpp>
+#include <boost/operators.hpp>
 #include <glog/logging.h>
 
-#include "folly/experimental/symbolizer/Elf.h"
+#include <folly/Hash.h>
+#include <folly/Range.h>
+#include <folly/experimental/symbolizer/Elf.h>
 
-namespace folly { namespace symbolizer {
+namespace folly {
+namespace symbolizer {
+
+/**
+ * Number of ELF files loaded by the dynamic loader.
+ */
+size_t countLoadedElfFiles();
 
 class ElfCacheBase {
  public:
   virtual std::shared_ptr<ElfFile> getFile(StringPiece path) = 0;
-  virtual ~ElfCacheBase() { }
+  virtual ~ElfCacheBase() {}
 };
 
 /**
@@ -64,9 +71,19 @@ class SignalSafeElfCache : public ElfCacheBase {
   // own wrapper around a fixed-size, null-terminated string.
   class Path : private boost::totally_ordered<Path> {
    public:
+    Path() {
+      assign(folly::StringPiece());
+    }
+
     explicit Path(StringPiece s) {
+      assign(s);
+    }
+
+    void assign(StringPiece s) {
       DCHECK_LE(s.size(), kMaxSize);
-      memcpy(data_, s.data(), s.size());
+      if (!s.empty()) {
+        memcpy(data_, s.data(), s.size());
+      }
       data_[s.size()] = '\0';
     }
 
@@ -88,6 +105,7 @@ class SignalSafeElfCache : public ElfCacheBase {
     char data_[kMaxSize + 1];
   };
 
+  Path scratchpad_; // Preallocated key for map_ lookups.
   boost::container::flat_map<Path, int> map_;
   std::vector<std::shared_ptr<ElfFile>> slots_;
 };
@@ -117,19 +135,14 @@ class ElfCache : public ElfCacheBase {
   static std::shared_ptr<ElfFile> filePtr(const std::shared_ptr<Entry>& e);
 
   size_t capacity_;
-  std::unordered_map<
-    StringPiece,
-    std::shared_ptr<Entry>,
-    StringPieceHash> files_;
+  std::unordered_map<StringPiece, std::shared_ptr<Entry>, Hash> files_;
 
   typedef boost::intrusive::list<
       Entry,
       boost::intrusive::member_hook<Entry, LruLink, &Entry::lruLink>,
-      boost::intrusive::constant_time_size<false>> LruList;
+      boost::intrusive::constant_time_size<false>>
+      LruList;
   LruList lruList_;
 };
-
-}}  // namespaces
-
-#endif /* FOLLY_SYMBOLIZER_ELFCACHE_H_ */
-
+} // namespace symbolizer
+} // namespace folly