Use Abseil hash maps instead of std::map<T>

PiperOrigin-RevId: 340807499
Change-Id: I2689bd1d32be45e3085dcc7a0ba4b8fedd7d53b0
This commit is contained in:
Christian Blichmann 2020-11-05 01:48:46 -08:00 committed by Copybara-Service
parent 2955d20c9f
commit 7c30aebe2d
4 changed files with 9 additions and 11 deletions

View File

@ -343,6 +343,7 @@ cc_library(
"//sandboxed_api/sandbox2/util:strerror", "//sandboxed_api/sandbox2/util:strerror",
"//sandboxed_api/util:raw_logging", "//sandboxed_api/util:raw_logging",
"@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/memory", "@com_google_absl//absl/memory",
"@com_google_absl//absl/strings", "@com_google_absl//absl/strings",
], ],

View File

@ -328,6 +328,7 @@ add_library(sandbox2_client STATIC
add_library(sandbox2::client ALIAS sandbox2_client) add_library(sandbox2::client ALIAS sandbox2_client)
target_link_libraries(sandbox2_client target_link_libraries(sandbox2_client
PRIVATE absl::core_headers PRIVATE absl::core_headers
absl::flat_hash_map
absl::memory absl::memory
absl::strings absl::strings
sandbox2::logsink sandbox2::logsink

View File

@ -46,22 +46,18 @@
namespace sandbox2 { namespace sandbox2 {
constexpr uint32_t Client::kClient2SandboxReady;
constexpr uint32_t Client::kSandbox2ClientDone;
constexpr const char* Client::kFDMapEnvVar;
Client::Client(Comms* comms) : comms_(comms) { Client::Client(Comms* comms) : comms_(comms) {
char* fdmap_envvar = getenv(kFDMapEnvVar); char* fdmap_envvar = getenv(kFDMapEnvVar);
if (!fdmap_envvar) { if (!fdmap_envvar) {
return; return;
} }
std::map<absl::string_view, absl::string_view> vars = absl::flat_hash_map<absl::string_view, absl::string_view> vars =
absl::StrSplit(fdmap_envvar, ',', absl::SkipEmpty()); absl::StrSplit(fdmap_envvar, ',', absl::SkipEmpty());
for (const auto& var : vars) { for (const auto& [name, mapped_fd] : vars) {
int fd; int fd;
SAPI_RAW_CHECK(absl::SimpleAtoi(var.second, &fd), "failed to parse fd map"); SAPI_RAW_CHECK(absl::SimpleAtoi(mapped_fd, &fd), "failed to parse fd map");
SAPI_RAW_CHECK(fd_map_.emplace(std::string{var.first}, fd).second, SAPI_RAW_CHECK(fd_map_.emplace(std::string(name), fd).second,
"could not insert mapping into fd map (duplicate)"); "could not insert mapping into fd map (duplicate)");
} }
unsetenv(kFDMapEnvVar); unsetenv(kFDMapEnvVar);
} }

View File

@ -19,10 +19,10 @@
#define SANDBOXED_API_SANDBOX2_CLIENT_H_ #define SANDBOXED_API_SANDBOX2_CLIENT_H_
#include <cstdint> #include <cstdint>
#include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include "absl/container/flat_hash_map.h"
#include "sandboxed_api/sandbox2/comms.h" #include "sandboxed_api/sandbox2/comms.h"
#include "sandboxed_api/sandbox2/logsink.h" #include "sandboxed_api/sandbox2/logsink.h"
#include "sandboxed_api/sandbox2/network_proxy/client.h" #include "sandboxed_api/sandbox2/network_proxy/client.h"
@ -87,7 +87,7 @@ class Client {
// In the pre-execve case, the sandboxee has to pass the information about // In the pre-execve case, the sandboxee has to pass the information about
// file descriptors to the new process. We set an environment variable for // file descriptors to the new process. We set an environment variable for
// this case that is parsed in the Client constructor if present. // this case that is parsed in the Client constructor if present.
std::map<std::string, int> fd_map_; absl::flat_hash_map<std::string, int> fd_map_;
std::string GetFdMapEnvVar() const; std::string GetFdMapEnvVar() const;