Path: blob/main/contrib/llvm-project/lldb/source/Utility/Environment.cpp
39587 views
//===-- Environment.cpp ---------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Utility/Environment.h"910using namespace lldb_private;1112char *Environment::Envp::make_entry(llvm::StringRef Key,13llvm::StringRef Value) {14const size_t size = Key.size() + 1 /*=*/ + Value.size() + 1 /*\0*/;15char *Result = static_cast<char *>(16Allocator.Allocate(sizeof(char) * size, alignof(char)));17char *Next = Result;1819Next = std::copy(Key.begin(), Key.end(), Next);20*Next++ = '=';21Next = std::copy(Value.begin(), Value.end(), Next);22*Next++ = '\0';2324return Result;25}2627Environment::Envp::Envp(const Environment &Env) {28Data = static_cast<char **>(29Allocator.Allocate(sizeof(char *) * (Env.size() + 1), alignof(char *)));30char **Next = Data;31for (const auto &KV : Env)32*Next++ = make_entry(KV.first(), KV.second);33*Next++ = nullptr;34}3536Environment::Environment(const char *const *Env) {37if (!Env)38return;39while (*Env)40insert(*Env++);41}4243void Environment::insert(iterator first, iterator last) {44while (first != last) {45try_emplace(first->first(), first->second);46++first;47}48}495051