2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
29 #include <android-base/logging.h>
31 void OneTimeFreeAllocator::Clear() {
40 const char* OneTimeFreeAllocator::AllocateString(const std::string& s) {
41 size_t size = s.size() + 1;
42 if (cur_ + size > end_) {
43 size_t alloc_size = std::max(size, unit_size_);
44 char* p = new char[alloc_size];
47 end_ = p + alloc_size;
49 strcpy(cur_, s.c_str());
50 const char* result = cur_;
55 void PrintIndented(size_t indent, const char* fmt, ...) {
58 printf("%*s", static_cast<int>(indent * 2), "");
63 bool IsPowerOfTwo(uint64_t value) {
64 return (value != 0 && ((value & (value - 1)) == 0));
67 void GetEntriesInDir(const std::string& dirpath, std::vector<std::string>* files,
68 std::vector<std::string>* subdirs) {
69 if (files != nullptr) {
72 if (subdirs != nullptr) {
75 DIR* dir = opendir(dirpath.c_str());
77 PLOG(DEBUG) << "can't open dir " << dirpath;
81 while ((entry = readdir(dir)) != nullptr) {
82 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
85 if (entry->d_type == DT_DIR) {
86 if (subdirs != nullptr) {
87 subdirs->push_back(entry->d_name);
90 if (files != nullptr) {
91 files->push_back(entry->d_name);
98 bool IsDir(const std::string& dirpath) {
100 if (stat(dirpath.c_str(), &st) == 0) {
101 if (S_ISDIR(st.st_mode)) {
108 bool RemovePossibleFile(const std::string& filename) {
110 if (stat(filename.c_str(), &st) == 0) {
111 if (!S_ISREG(st.st_mode)) {
112 LOG(ERROR) << filename << " is not a file.";
115 if (unlink(filename.c_str()) == -1) {
116 PLOG(ERROR) << "unlink(" << filename << ") failed";
123 bool StringToPid(const std::string& s, int* pid) {
125 *pid = static_cast<int>(strtol(s.c_str(), &endptr, 10));
126 return *endptr == '\0';