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/logging.h>
26#include <android-base/macros.h>
27#include <ziparchive/zip_archive.h>
28
29#define ALIGN(value, alignment) (((value) + (alignment)-1) & ~((alignment)-1))
30
31#ifdef _WIN32
32#define CLOSE_ON_EXEC_MODE ""
33#else
34#define CLOSE_ON_EXEC_MODE "e"
35#endif
36
37// OneTimeAllocator is used to allocate memory many times and free only once at the end.
38// It reduces the cost to free each allocated memory.
39class OneTimeFreeAllocator {
40 public:
41  OneTimeFreeAllocator(size_t unit_size = 8192u)
42      : unit_size_(unit_size), cur_(nullptr), end_(nullptr) {
43  }
44
45  ~OneTimeFreeAllocator() {
46    Clear();
47  }
48
49  void Clear();
50  const char* AllocateString(const std::string& s);
51
52 private:
53  const size_t unit_size_;
54  std::vector<char*> v_;
55  char* cur_;
56  char* end_;
57};
58
59class FileHelper {
60 public:
61  static FileHelper OpenReadOnly(const std::string& filename);
62  static FileHelper OpenWriteOnly(const std::string& filename);
63
64  FileHelper(FileHelper&& other) {
65    fd_ = other.fd_;
66    other.fd_ = -1;
67  }
68
69  ~FileHelper();
70
71  explicit operator bool() const {
72    return fd_ != -1;
73  }
74
75  int fd() const {
76    return fd_;
77  }
78
79 private:
80  FileHelper(int fd) : fd_(fd) {}
81  int fd_;
82
83  DISALLOW_COPY_AND_ASSIGN(FileHelper);
84};
85
86class 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
105template <class T>
106void MoveFromBinaryFormat(T& data, const char*& p) {
107  data = *reinterpret_cast<const T*>(p);
108  p += sizeof(T);
109}
110
111void PrintIndented(size_t indent, const char* fmt, ...);
112
113bool IsPowerOfTwo(uint64_t value);
114
115void GetEntriesInDir(const std::string& dirpath, std::vector<std::string>* files,
116                     std::vector<std::string>* subdirs);
117bool IsDir(const std::string& dirpath);
118bool IsRegularFile(const std::string& filename);
119uint64_t GetFileSize(const std::string& filename);
120bool MkdirWithParents(const std::string& path);
121
122bool GetLogSeverity(const std::string& name, android::base::LogSeverity* severity);
123
124bool IsRoot();
125#endif  // SIMPLE_PERF_UTILS_H_
126