OSDN Git Service

Update simpleperf for LLVM rebase to r256229.
[android-x86/system-extras.git] / simpleperf / utils.h
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
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 #ifndef SIMPLE_PERF_UTILS_H_
18 #define SIMPLE_PERF_UTILS_H_
19
20 #include <stddef.h>
21
22 #include <string>
23 #include <vector>
24
25 #include <android-base/macros.h>
26 #include <ziparchive/zip_archive.h>
27
28 #define ALIGN(value, alignment) (((value) + (alignment)-1) & ~((alignment)-1))
29
30 #ifdef _WIN32
31 #define CLOSE_ON_EXEC_MODE ""
32 #else
33 #define CLOSE_ON_EXEC_MODE "e"
34 #endif
35
36 // OneTimeAllocator is used to allocate memory many times and free only once at the end.
37 // It reduces the cost to free each allocated memory.
38 class OneTimeFreeAllocator {
39  public:
40   OneTimeFreeAllocator(size_t unit_size = 8192u)
41       : unit_size_(unit_size), cur_(nullptr), end_(nullptr) {
42   }
43
44   ~OneTimeFreeAllocator() {
45     Clear();
46   }
47
48   void Clear();
49   const char* AllocateString(const std::string& s);
50
51  private:
52   const size_t unit_size_;
53   std::vector<char*> v_;
54   char* cur_;
55   char* end_;
56 };
57
58 class FileHelper {
59  public:
60   static FileHelper OpenReadOnly(const std::string& filename);
61   static FileHelper OpenWriteOnly(const std::string& filename);
62
63   FileHelper(FileHelper&& other) {
64     fd_ = other.fd_;
65     other.fd_ = -1;
66   }
67
68   ~FileHelper();
69
70   explicit operator bool() const {
71     return fd_ != -1;
72   }
73
74   int fd() const {
75     return fd_;
76   }
77
78  private:
79   FileHelper(int fd) : fd_(fd) {}
80   int fd_;
81
82   DISALLOW_COPY_AND_ASSIGN(FileHelper);
83 };
84
85
86 class ArchiveHelper {
87  public:
88   ArchiveHelper(int fd, const std::string& debug_filename);
89   ~ArchiveHelper();
90
91   explicit operator bool() const {
92     return valid_;
93   }
94   ZipArchiveHandle &archive_handle() {
95     return handle_;
96   }
97
98  private:
99   ZipArchiveHandle handle_;
100   bool valid_;
101
102   DISALLOW_COPY_AND_ASSIGN(ArchiveHelper);
103 };
104
105 template <class T>
106 void MoveFromBinaryFormat(T& data, const char*& p) {
107   data = *reinterpret_cast<const T*>(p);
108   p += sizeof(T);
109 }
110
111 void PrintIndented(size_t indent, const char* fmt, ...);
112
113 bool IsPowerOfTwo(uint64_t value);
114
115 void GetEntriesInDir(const std::string& dirpath, std::vector<std::string>* files,
116                      std::vector<std::string>* subdirs);
117 bool IsDir(const std::string& dirpath);
118 bool IsRegularFile(const std::string& filename);
119 uint64_t GetFileSize(const std::string& filename);
120 bool MkdirWithParents(const std::string& path);
121
122 #endif  // SIMPLE_PERF_UTILS_H_