diff --git a/adb/adb_listeners.cpp b/adb/adb_listeners.cpp index a142384be..fecf452c1 100644 --- a/adb/adb_listeners.cpp +++ b/adb/adb_listeners.cpp @@ -29,6 +29,7 @@ #include "socket_spec.h" #include "sysdeps.h" +#include "sysdeps/memory.h" #include "transport.h" // A listener is an entity which binds to a local port and, upon receiving a connection on that @@ -203,7 +204,7 @@ InstallStatus install_listener(const std::string& local_name, const char* connec } } - std::unique_ptr listener(new alistener(local_name, connect_to)); + auto listener = std::make_unique(local_name, connect_to); int resolved = 0; listener->fd = socket_spec_listen(listener->local_name, error, &resolved); diff --git a/adb/client/commandline.cpp b/adb/client/commandline.cpp index d126f52a5..34930c6aa 100644 --- a/adb/client/commandline.cpp +++ b/adb/client/commandline.cpp @@ -61,6 +61,7 @@ #include "services.h" #include "shell_service.h" #include "sysdeps/chrono.h" +#include "sysdeps/memory.h" static int install_app(int argc, const char** argv); static int install_multiple_app(int argc, const char** argv); @@ -263,7 +264,7 @@ int read_and_dump(int fd, bool use_shell_protocol = false, char raw_buffer[BUFSIZ]; char* buffer_ptr = raw_buffer; if (use_shell_protocol) { - protocol.reset(new ShellProtocol(fd)); + protocol = std::make_unique(fd); if (!protocol) { LOG(ERROR) << "failed to allocate memory for ShellProtocol object"; return 1; @@ -630,7 +631,7 @@ static int RemoteShell(bool use_shell_protocol, const std::string& type_arg, args->raw_stdin = raw_stdin; args->escape_char = escape_char; if (use_shell_protocol) { - args->protocol.reset(new ShellProtocol(args->write_fd)); + args->protocol = std::make_unique(args->write_fd); } if (raw_stdin) stdin_raw_init(); diff --git a/adb/daemon/shell_service.cpp b/adb/daemon/shell_service.cpp index f9f80c03d..c04ceafa7 100644 --- a/adb/daemon/shell_service.cpp +++ b/adb/daemon/shell_service.cpp @@ -372,8 +372,8 @@ bool Subprocess::ForkAndExec(std::string* error) { } D("protocol FD = %d", protocol_sfd_.get()); - input_.reset(new ShellProtocol(protocol_sfd_)); - output_.reset(new ShellProtocol(protocol_sfd_)); + input_ = std::make_unique(protocol_sfd_); + output_ = std::make_unique(protocol_sfd_); if (!input_ || !output_) { *error = "failed to allocate shell protocol objects"; kill(pid_, SIGKILL); diff --git a/adb/fdevent.cpp b/adb/fdevent.cpp index d2855619b..ba82e7c5f 100644 --- a/adb/fdevent.cpp +++ b/adb/fdevent.cpp @@ -21,6 +21,7 @@ #include "fdevent.h" #include +#include #include #include #include @@ -36,6 +37,7 @@ #include #include #include +#include #include "adb_io.h" #include "adb_trace.h" @@ -78,7 +80,7 @@ static auto& g_poll_node_map = *new std::unordered_map(); static auto& g_pending_list = *new std::list(); static std::atomic terminate_loop(false); static bool main_thread_valid; -static unsigned long main_thread_id; +static uint64_t main_thread_id; static auto& run_queue_notify_fd = *new unique_fd(); static auto& run_queue_mutex = *new std::mutex(); @@ -86,13 +88,13 @@ static auto& run_queue GUARDED_BY(run_queue_mutex) = *new std::deque> fd_handlers; for (size_t i = 0; i < read_fds.size(); ++i) { - fd_handlers.push_back(std::unique_ptr(new FdHandler(read_fds[i], write_fds[i]))); + fd_handlers.push_back(std::make_unique(read_fds[i], write_fds[i])); } fdevent_loop(); diff --git a/adb/sysdeps.h b/adb/sysdeps.h index 307be6d58..fd08ad8b2 100644 --- a/adb/sysdeps.h +++ b/adb/sysdeps.h @@ -92,11 +92,6 @@ static __inline__ int adb_thread_setname(const std::string& name) { return 0; } -static __inline__ unsigned long adb_thread_id() -{ - return GetCurrentThreadId(); -} - static __inline__ void close_on_exec(int fd) { /* nothing really */ @@ -625,11 +620,6 @@ static __inline__ int adb_is_absolute_host_path(const char* path) { return path[0] == '/'; } -static __inline__ unsigned long adb_thread_id() -{ - return (unsigned long)gettid(); -} - #endif /* !_WIN32 */ static inline void disable_tcp_nagle(int fd) { diff --git a/adb/sysdeps/memory.h b/adb/sysdeps/memory.h new file mode 100644 index 000000000..0e4c509f6 --- /dev/null +++ b/adb/sysdeps/memory.h @@ -0,0 +1,36 @@ +#pragma once + +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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. + */ + +#include +#include + +#if defined(_WIN32) +// We don't have C++14 on Windows yet. +// Reimplement std::make_unique ourselves until we do. + +namespace std { + +template +typename std::enable_if::value, std::unique_ptr>::type make_unique( + Args&&... args) { + return std::unique_ptr(new T(std::forward(args)...)); +} + +} // namespace std + +#endif diff --git a/adb/transport.cpp b/adb/transport.cpp index 3329f0f19..37b56e28f 100644 --- a/adb/transport.cpp +++ b/adb/transport.cpp @@ -17,6 +17,8 @@ #define TRACE_TAG TRANSPORT #include "sysdeps.h" +#include "sysdeps/memory.h" + #include "transport.h" #include @@ -79,7 +81,7 @@ void BlockingConnectionAdapter::Start() { read_thread_ = std::thread([this]() { LOG(INFO) << this->transport_name_ << ": read thread spawning"; while (true) { - std::unique_ptr packet(new apacket()); + auto packet = std::make_unique(); if (!underlying_->Read(packet.get())) { PLOG(INFO) << this->transport_name_ << ": read failed"; break; diff --git a/adb/transport_local.cpp b/adb/transport_local.cpp index ff395dc7e..c09fcb76d 100644 --- a/adb/transport_local.cpp +++ b/adb/transport_local.cpp @@ -45,6 +45,7 @@ #include "adb_unique_fd.h" #include "adb_utils.h" #include "sysdeps/chrono.h" +#include "sysdeps/memory.h" #if ADB_HOST @@ -450,9 +451,8 @@ int init_socket_transport(atransport* t, int s, int adb_port, int local) { #if ADB_HOST // Emulator connection. if (local) { - std::unique_ptr emulator_connection( - new EmulatorConnection(std::move(fd), adb_port)); - t->connection.reset(new BlockingConnectionAdapter(std::move(emulator_connection))); + auto emulator_connection = std::make_unique(std::move(fd), adb_port); + t->connection = std::make_unique(std::move(emulator_connection)); std::lock_guard lock(local_transports_lock); atransport* existing_transport = find_emulator_transport_by_adb_port_locked(adb_port); if (existing_transport != NULL) { @@ -471,7 +471,7 @@ int init_socket_transport(atransport* t, int s, int adb_port, int local) { #endif // Regular tcp connection. - std::unique_ptr fd_connection(new FdConnection(std::move(fd))); - t->connection.reset(new BlockingConnectionAdapter(std::move(fd_connection))); + auto fd_connection = std::make_unique(std::move(fd)); + t->connection = std::make_unique(std::move(fd_connection)); return fail; } diff --git a/adb/transport_usb.cpp b/adb/transport_usb.cpp index 33e00a1f4..e9a75cd71 100644 --- a/adb/transport_usb.cpp +++ b/adb/transport_usb.cpp @@ -17,6 +17,7 @@ #define TRACE_TAG TRANSPORT #include "sysdeps.h" +#include "sysdeps/memory.h" #include "transport.h" #include @@ -174,8 +175,8 @@ void UsbConnection::Close() { void init_usb_transport(atransport* t, usb_handle* h) { D("transport: usb"); - std::unique_ptr connection(new UsbConnection(h)); - t->connection.reset(new BlockingConnectionAdapter(std::move(connection))); + auto connection = std::make_unique(h); + t->connection = std::make_unique(std::move(connection)); t->type = kTransportUsb; } diff --git a/base/Android.bp b/base/Android.bp index 5d70d47bd..7b0ba11d0 100644 --- a/base/Android.bp +++ b/base/Android.bp @@ -50,6 +50,7 @@ cc_defaults { "quick_exit.cpp", "stringprintf.cpp", "strings.cpp", + "threads.cpp", "test_utils.cpp", ], diff --git a/base/include/android-base/threads.h b/base/include/android-base/threads.h new file mode 100644 index 000000000..85e65ba65 --- /dev/null +++ b/base/include/android-base/threads.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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. + */ + +#ifndef ANDROID_BASE_THREADS_H +#define ANDROID_BASE_THREADS_H + +#include + +namespace android { +namespace base { +uint64_t GetThreadId(); +} +} // namespace android + +#endif diff --git a/base/logging.cpp b/base/logging.cpp index a31feefab..30d7f8d57 100644 --- a/base/logging.cpp +++ b/base/logging.cpp @@ -21,6 +21,7 @@ #include "android-base/logging.h" #include +#include #include #include @@ -54,41 +55,7 @@ #include #include - -// For gettid. -#if defined(__APPLE__) -#include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED -#include -#include -#include -#include -#include -#elif defined(__linux__) && !defined(__ANDROID__) -#include -#include -#elif defined(_WIN32) -#include -#endif - -#if defined(_WIN32) -typedef uint32_t thread_id; -#else -typedef pid_t thread_id; -#endif - -static thread_id GetThreadId() { -#if defined(__BIONIC__) - return gettid(); -#elif defined(__APPLE__) - uint64_t tid; - pthread_threadid_np(NULL, &tid); - return tid; -#elif defined(__linux__) - return syscall(__NR_gettid); -#elif defined(_WIN32) - return GetCurrentThreadId(); -#endif -} +#include namespace { #if defined(__GLIBC__) @@ -223,8 +190,8 @@ void StderrLogger(LogId, LogSeverity severity, const char* tag, const char* file static_assert(arraysize(log_characters) - 1 == FATAL + 1, "Mismatch in size of log_characters and values in LogSeverity"); char severity_char = log_characters[severity]; - fprintf(stderr, "%s %c %s %5d %5d %s:%u] %s\n", tag ? tag : "nullptr", severity_char, timestamp, - getpid(), GetThreadId(), file, line, message); + fprintf(stderr, "%s %c %s %5d %5" PRIu64 " %s:%u] %s\n", tag ? tag : "nullptr", severity_char, + timestamp, getpid(), GetThreadId(), file, line, message); } void DefaultAborter(const char* abort_message) { diff --git a/base/threads.cpp b/base/threads.cpp new file mode 100644 index 000000000..a71382bc5 --- /dev/null +++ b/base/threads.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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. + */ + +#include + +#include +#include + +#if defined(__APPLE__) +#include +#elif defined(__linux__) && !defined(__ANDROID__) +#include +#elif defined(_WIN32) +#include +#endif + +namespace android { +namespace base { + +uint64_t GetThreadId() { +#if defined(__BIONIC__) + return gettid(); +#elif defined(__APPLE__) + uint64_t tid; + pthread_threadid_np(NULL, &tid); + return tid; +#elif defined(__linux__) + return syscall(__NR_gettid); +#elif defined(_WIN32) + return GetCurrentThreadId(); +#endif +} + +} // namespace base +} // namespace android