Move FileDesc to separated header file
authorTeng Qin <qinteng@fb.com>
Wed, 10 May 2017 23:01:05 +0000 (16:01 -0700)
committerTeng Qin <qinteng@fb.com>
Wed, 10 May 2017 23:49:43 +0000 (16:49 -0700)
src/cc/CMakeLists.txt
src/cc/common.cc
src/cc/common.h
src/cc/file_desc.h [new file with mode: 0644]
src/cc/frontends/b/codegen_llvm.cc
src/cc/syms.h
src/cc/table_desc.h

index 15d246dde5047d2aef6f0ac9c9773fdb097c87e4..267e5c399f1a4947ad4e4017eea032948ff0cb94 100644 (file)
@@ -67,7 +67,9 @@ target_link_libraries(bcc-static b_frontend clang_frontend bcc-loader-static ${c
 
 install(TARGETS bcc-shared LIBRARY COMPONENT libbcc
   DESTINATION ${CMAKE_INSTALL_LIBDIR})
-install(FILES bpf_common.h bpf_module.h bcc_syms.h bcc_exception.h libbpf.h perf_reader.h BPF.h BPFTable.h shared_table.h table_desc.h table_storage.h COMPONENT libbcc
+install(FILES bpf_common.h bpf_module.h bcc_syms.h bcc_exception.h file_desc.h
+              libbpf.h perf_reader.h BPF.h BPFTable.h shared_table.h
+              table_desc.h table_storage.h COMPONENT libbcc
   DESTINATION include/bcc)
 install(DIRECTORY compat/linux/ COMPONENT libbcc
   DESTINATION include/bcc/compat/linux
index 68501d8f0c71d1364447339842db15e77fff0f69..193ef64c749f87aa8758456771542ac6d23efd7a 100644 (file)
@@ -15,7 +15,6 @@
  */
 #include <fstream>
 #include <sstream>
-#include <unistd.h>
 
 #include "common.h"
 
@@ -49,37 +48,4 @@ std::vector<int> get_possible_cpus() {
   return read_cpu_range("/sys/devices/system/cpu/possible");
 }
 
-FileDesc::FileDesc(int fd) : fd_(fd) {}
-
-FileDesc::FileDesc(FileDesc &&that) : fd_(-1) { *this = std::move(that); }
-
-FileDesc::~FileDesc() {
-  if (fd_ >= 0)
-    ::close(fd_);
-}
-
-FileDesc &FileDesc::operator=(int fd) {
-  if (fd_ >= 0)
-    ::close(fd_);
-  fd_ = fd;
-  return *this;
-}
-
-FileDesc &FileDesc::operator=(FileDesc &&that) {
-  if (fd_ >= 0)
-    ::close(fd_);
-  fd_ = that.fd_;
-  that.fd_ = -1;
-  return *this;
-}
-
-FileDesc FileDesc::dup() const {
-  int dup_fd = ::dup(fd_);
-  return FileDesc(dup_fd);
-}
-
-FileDesc::operator int() { return fd_; }
-
-FileDesc::operator int() const { return fd_; }
-
 } // namespace ebpf
index 4e6b335581b8454f3bfb95cb43d4cbbc2ec205e0..6c8f9c9e4d805317b7814f51ce3ccbdc753121f7 100644 (file)
@@ -17,8 +17,6 @@
 #pragma once
 
 #include <memory>
-#include <string>
-#include <tuple>
 #include <vector>
 
 namespace ebpf {
@@ -33,27 +31,4 @@ std::vector<int> get_online_cpus();
 
 std::vector<int> get_possible_cpus();
 
-/// FileDesc is a helper class for managing open file descriptors. Copy is
-/// disallowed (call dup instead), and cleanup happens automatically.
-class FileDesc {
- public:
-  explicit FileDesc(int fd = -1);
-  FileDesc(FileDesc &&that);
-  FileDesc(const FileDesc &that) = delete;
-
-  ~FileDesc();
-
-  FileDesc &operator=(int fd);
-  FileDesc &operator=(FileDesc &&that);
-  FileDesc &operator=(const FileDesc &that) = delete;
-
-  operator int();
-  operator int() const;
-
-  FileDesc dup() const;
-
- private:
-  int fd_;
-};
-
 }  // namespace ebpf
diff --git a/src/cc/file_desc.h b/src/cc/file_desc.h
new file mode 100644 (file)
index 0000000..a55ba0b
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <unistd.h>
+
+namespace ebpf {
+
+/// FileDesc is a helper class for managing open file descriptors. Copy is
+/// disallowed (call dup instead), and cleanup happens automatically.
+class FileDesc {
+ public:
+  explicit FileDesc(int fd = -1) : fd_(fd) {}
+  FileDesc(FileDesc &&that) : fd_(-1) { *this = std::move(that); }
+  FileDesc(const FileDesc &that) = delete;
+
+  ~FileDesc() {
+    if (fd_ >= 0)
+      ::close(fd_);
+  }
+
+  FileDesc &operator=(int fd) {
+    if (fd_ >= 0)
+      ::close(fd_);
+    fd_ = fd;
+    return *this;
+  }
+  FileDesc &operator=(FileDesc &&that) {
+    if (fd_ >= 0)
+      ::close(fd_);
+    fd_ = that.fd_;
+    that.fd_ = -1;
+    return *this;
+  }
+  FileDesc &operator=(const FileDesc &that) = delete;
+
+  FileDesc dup() const {
+    int dup_fd = ::dup(fd_);
+    return FileDesc(dup_fd);
+  }
+
+  operator int() { return fd_; }
+  operator int() const { return fd_; }
+
+ private:
+  int fd_;
+};
+
+}  // namespace ebpf
index ab27e3674de5835e157d40b9d9d5da2308ad084c..063b486d8cd38184e512fd60d1a8363255286751 100644 (file)
@@ -33,8 +33,8 @@
 #include <llvm/IR/Module.h>
 
 #include "bcc_exception.h"
-#include "common.h"
 #include "codegen_llvm.h"
+#include "file_desc.h"
 #include "lexer.h"
 #include "libbpf.h"
 #include "linux/bpf.h"
index ccf7af0a7d2a542a8a18c69fbe6614e347efad6c..cb0d950c1f6a8bd0e8a30f1386e09f4f05c3e45f 100644 (file)
@@ -23,7 +23,7 @@
 #include <unordered_set>
 #include <vector>
 
-#include "common.h"
+#include "file_desc.h"
 
 class ProcStat {
   std::string procfs_;
index d1338202032ac2db53d5fef42720315683527d0d..da0927f9441a1b6aa6fcbc5829243592872b77fd 100644 (file)
@@ -23,7 +23,7 @@
 #include <string>
 
 #include "bcc_exception.h"
-#include "common.h"
+#include "file_desc.h"
 
 namespace clang {
 class ASTContext;