Modernize memory management

Update internals of various classes
This commit is contained in:
Marc Chevrier
2020-02-26 15:52:47 +01:00
parent ab2d170c74
commit 557cecdc3d
17 changed files with 126 additions and 138 deletions

View File

@@ -488,24 +488,8 @@ struct cmCPluginAPISourceFile
// Keep a map from real cmSourceFile instances stored in a makefile to // Keep a map from real cmSourceFile instances stored in a makefile to
// the CPluginAPI proxy source file. // the CPluginAPI proxy source file.
class cmCPluginAPISourceFileMap using cmCPluginAPISourceFileMap =
: public std::map<cmSourceFile*, cmCPluginAPISourceFile*> std::map<cmSourceFile*, std::unique_ptr<cmCPluginAPISourceFile>>;
{
public:
using derived = std::map<cmSourceFile*, cmCPluginAPISourceFile*>;
using iterator = derived::iterator;
using value_type = derived::value_type;
cmCPluginAPISourceFileMap() = default;
~cmCPluginAPISourceFileMap()
{
for (auto const& i : *this) {
delete i.second;
}
}
cmCPluginAPISourceFileMap(const cmCPluginAPISourceFileMap&) = delete;
cmCPluginAPISourceFileMap& operator=(const cmCPluginAPISourceFileMap&) =
delete;
};
cmCPluginAPISourceFileMap cmCPluginAPISourceFiles; cmCPluginAPISourceFileMap cmCPluginAPISourceFiles;
void* CCONV cmCreateSourceFile(void) void* CCONV cmCreateSourceFile(void)
@@ -536,7 +520,7 @@ void CCONV* cmGetSource(void* arg, const char* name)
auto i = cmCPluginAPISourceFiles.find(rsf); auto i = cmCPluginAPISourceFiles.find(rsf);
if (i == cmCPluginAPISourceFiles.end()) { if (i == cmCPluginAPISourceFiles.end()) {
// Create a proxy source file object for this source. // Create a proxy source file object for this source.
cmCPluginAPISourceFile* sf = new cmCPluginAPISourceFile; auto sf = cm::make_unique<cmCPluginAPISourceFile>();
sf->RealSourceFile = rsf; sf->RealSourceFile = rsf;
sf->FullPath = rsf->ResolveFullPath(); sf->FullPath = rsf->ResolveFullPath();
sf->SourceName = sf->SourceName =
@@ -545,10 +529,9 @@ void CCONV* cmGetSource(void* arg, const char* name)
cmSystemTools::GetFilenameLastExtension(sf->FullPath); cmSystemTools::GetFilenameLastExtension(sf->FullPath);
// Store the proxy in the map so it can be re-used and deleted later. // Store the proxy in the map so it can be re-used and deleted later.
cmCPluginAPISourceFileMap::value_type entry(rsf, sf); i = cmCPluginAPISourceFiles.emplace(rsf, std::move(sf)).first;
i = cmCPluginAPISourceFiles.insert(entry).first;
} }
return i->second; return i->second.get();
} }
return nullptr; return nullptr;
} }
@@ -569,15 +552,16 @@ void* CCONV cmAddSource(void* arg, void* arg2)
} }
// Create the proxy for the real source file. // Create the proxy for the real source file.
cmCPluginAPISourceFile* sf = new cmCPluginAPISourceFile; auto sf = cm::make_unique<cmCPluginAPISourceFile>();
sf->RealSourceFile = rsf; sf->RealSourceFile = rsf;
sf->FullPath = osf->FullPath; sf->FullPath = osf->FullPath;
sf->SourceName = osf->SourceName; sf->SourceName = osf->SourceName;
sf->SourceExtension = osf->SourceExtension; sf->SourceExtension = osf->SourceExtension;
// Store the proxy in the map so it can be re-used and deleted later. // Store the proxy in the map so it can be re-used and deleted later.
cmCPluginAPISourceFiles[rsf] = sf; auto value = sf.get();
return sf; cmCPluginAPISourceFiles[rsf] = std::move(sf);
return value;
} }
const char* CCONV cmSourceFileGetSourceName(void* arg) const char* CCONV cmSourceFileGetSourceName(void* arg)

View File

@@ -16,6 +16,9 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <cm/memory>
#include <cmext/algorithm>
#include "cmsys/Base64.h" #include "cmsys/Base64.h"
#include "cmsys/Directory.hxx" #include "cmsys/Directory.hxx"
#include "cmsys/FStream.hxx" #include "cmsys/FStream.hxx"
@@ -32,9 +35,6 @@
# include <unistd.h> // IWYU pragma: keep # include <unistd.h> // IWYU pragma: keep
#endif #endif
#include <cm/memory>
#include <cmext/algorithm>
#include "cmCTestBuildAndTestHandler.h" #include "cmCTestBuildAndTestHandler.h"
#include "cmCTestBuildHandler.h" #include "cmCTestBuildHandler.h"
#include "cmCTestConfigureHandler.h" #include "cmCTestConfigureHandler.h"
@@ -201,7 +201,7 @@ struct cmCTest::Private
int SubmitIndex = 0; int SubmitIndex = 0;
cmGeneratedFileStream* OutputLogFile = nullptr; std::unique_ptr<cmGeneratedFileStream> OutputLogFile;
int OutputLogFileLastTag = -1; int OutputLogFileLastTag = -1;
bool OutputTestOutputOnTestFailure = false; bool OutputTestOutputOnTestFailure = false;
@@ -362,10 +362,7 @@ cmCTest::cmCTest()
cmSystemTools::EnableVSConsoleOutput(); cmSystemTools::EnableVSConsoleOutput();
} }
cmCTest::~cmCTest() cmCTest::~cmCTest() = default;
{
delete this->Impl->OutputLogFile;
}
int cmCTest::GetParallelLevel() const int cmCTest::GetParallelLevel() const
{ {
@@ -3086,12 +3083,10 @@ bool cmCTest::RunCommand(std::vector<std::string> const& args,
void cmCTest::SetOutputLogFileName(const char* name) void cmCTest::SetOutputLogFileName(const char* name)
{ {
if (this->Impl->OutputLogFile) {
delete this->Impl->OutputLogFile;
this->Impl->OutputLogFile = nullptr;
}
if (name) { if (name) {
this->Impl->OutputLogFile = new cmGeneratedFileStream(name); this->Impl->OutputLogFile = cm::make_unique<cmGeneratedFileStream>(name);
} else {
this->Impl->OutputLogFile.reset();
} }
} }

View File

@@ -5,6 +5,9 @@
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <utility>
#include <cm/memory>
#include "cmCommandArgumentLexer.h" #include "cmCommandArgumentLexer.h"
#include "cmMakefile.h" #include "cmMakefile.h"
@@ -40,10 +43,10 @@ const char* cmCommandArgumentParserHelper::AddString(const std::string& str)
if (str.empty()) { if (str.empty()) {
return ""; return "";
} }
char* stVal = new char[str.size() + 1]; auto stVal = cm::make_unique<char[]>(str.size() + 1);
strcpy(stVal, str.c_str()); strcpy(stVal.get(), str.c_str());
this->Variables.push_back(stVal); this->Variables.push_back(std::move(stVal));
return stVal; return this->Variables.back().get();
} }
const char* cmCommandArgumentParserHelper::ExpandSpecialVariable( const char* cmCommandArgumentParserHelper::ExpandSpecialVariable(
@@ -136,11 +139,11 @@ const char* cmCommandArgumentParserHelper::CombineUnions(const char* in1,
return in1; return in1;
} }
size_t len = strlen(in1) + strlen(in2) + 1; size_t len = strlen(in1) + strlen(in2) + 1;
char* out = new char[len]; auto out = cm::make_unique<char[]>(len);
strcpy(out, in1); strcpy(out.get(), in1);
strcat(out, in2); strcat(out.get(), in2);
this->Variables.push_back(out); this->Variables.push_back(std::move(out));
return out; return this->Variables.back().get();
} }
void cmCommandArgumentParserHelper::AllocateParserType( void cmCommandArgumentParserHelper::AllocateParserType(
@@ -153,11 +156,11 @@ void cmCommandArgumentParserHelper::AllocateParserType(
if (len == 0) { if (len == 0) {
return; return;
} }
char* out = new char[len + 1]; auto out = cm::make_unique<char[]>(len + 1);
memcpy(out, str, len); memcpy(out.get(), str, len);
out[len] = 0; out.get()[len] = 0;
pt->str = out; pt->str = out.get();
this->Variables.push_back(out); this->Variables.push_back(std::move(out));
} }
bool cmCommandArgumentParserHelper::HandleEscapeSymbol( bool cmCommandArgumentParserHelper::HandleEscapeSymbol(
@@ -235,10 +238,7 @@ int cmCommandArgumentParserHelper::ParseString(const char* str, int verb)
void cmCommandArgumentParserHelper::CleanupParser() void cmCommandArgumentParserHelper::CleanupParser()
{ {
for (char* var : this->Variables) { this->Variables.clear();
delete[] var;
}
this->Variables.erase(this->Variables.begin(), this->Variables.end());
} }
int cmCommandArgumentParserHelper::LexInput(char* buf, int maxlen) int cmCommandArgumentParserHelper::LexInput(char* buf, int maxlen)

View File

@@ -5,6 +5,7 @@
#include "cmConfigure.h" // IWYU pragma: keep #include "cmConfigure.h" // IWYU pragma: keep
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -69,7 +70,7 @@ private:
void CleanupParser(); void CleanupParser();
void SetError(std::string const& msg); void SetError(std::string const& msg);
std::vector<char*> Variables; std::vector<std::unique_ptr<char[]>> Variables;
const cmMakefile* Makefile; const cmMakefile* Makefile;
std::string Result; std::string Result;
std::string ErrorString; std::string ErrorString;

View File

@@ -8,6 +8,7 @@
#include <iostream> #include <iostream>
#include <utility> #include <utility>
#include <cm/memory>
#include <cm/string_view> #include <cm/string_view>
#include "cmsys/FStream.hxx" #include "cmsys/FStream.hxx"
@@ -169,10 +170,11 @@ void cmDependsJavaParserHelper::AllocateParserType(
return; return;
} }
this->UnionsAvailable++; this->UnionsAvailable++;
pt->str = new char[len + 1]; auto up = cm::make_unique<char[]>(len + 1);
pt->str = up.get();
strncpy(pt->str, str, len); strncpy(pt->str, str, len);
pt->str[len] = 0; pt->str[len] = 0;
this->Allocates.push_back(pt->str); this->Allocates.push_back(std::move(up));
} }
void cmDependsJavaParserHelper::StartClass(const char* cls) void cmDependsJavaParserHelper::StartClass(const char* cls)
@@ -275,10 +277,7 @@ int cmDependsJavaParserHelper::ParseString(const char* str, int verb)
void cmDependsJavaParserHelper::CleanupParser() void cmDependsJavaParserHelper::CleanupParser()
{ {
for (char* allocate : this->Allocates) { this->Allocates.clear();
delete[] allocate;
}
this->Allocates.erase(this->Allocates.begin(), this->Allocates.end());
} }
int cmDependsJavaParserHelper::LexInput(char* buf, int maxlen) int cmDependsJavaParserHelper::LexInput(char* buf, int maxlen)

View File

@@ -5,6 +5,7 @@
#include "cmConfigure.h" // IWYU pragma: keep #include "cmConfigure.h" // IWYU pragma: keep
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -81,7 +82,7 @@ private:
int CurrentDepth; int CurrentDepth;
int Verbose; int Verbose;
std::vector<char*> Allocates; std::vector<std::unique_ptr<char[]>> Allocates;
void PrintClasses(); void PrintClasses();

View File

@@ -6,6 +6,7 @@
#include <string> #include <string>
#include <utility> #include <utility>
namespace {
class cmDynamicLoaderCache class cmDynamicLoaderCache
{ {
public: public:
@@ -15,14 +16,15 @@ public:
cmsys::DynamicLoader::LibraryHandle& /*p*/); cmsys::DynamicLoader::LibraryHandle& /*p*/);
bool FlushCache(const char* path); bool FlushCache(const char* path);
void FlushCache(); void FlushCache();
static cmDynamicLoaderCache* GetInstance(); static cmDynamicLoaderCache& GetInstance();
private: private:
std::map<std::string, cmsys::DynamicLoader::LibraryHandle> CacheMap; std::map<std::string, cmsys::DynamicLoader::LibraryHandle> CacheMap;
static cmDynamicLoaderCache* Instance; static cmDynamicLoaderCache Instance;
}; };
cmDynamicLoaderCache* cmDynamicLoaderCache::Instance = nullptr; cmDynamicLoaderCache cmDynamicLoaderCache::Instance;
}
cmDynamicLoaderCache::~cmDynamicLoaderCache() = default; cmDynamicLoaderCache::~cmDynamicLoaderCache() = default;
@@ -64,15 +66,11 @@ void cmDynamicLoaderCache::FlushCache()
for (auto const& it : this->CacheMap) { for (auto const& it : this->CacheMap) {
cmsys::DynamicLoader::CloseLibrary(it.second); cmsys::DynamicLoader::CloseLibrary(it.second);
} }
delete cmDynamicLoaderCache::Instance; this->CacheMap.clear();
cmDynamicLoaderCache::Instance = nullptr;
} }
cmDynamicLoaderCache* cmDynamicLoaderCache::GetInstance() cmDynamicLoaderCache& cmDynamicLoaderCache::GetInstance()
{ {
if (!cmDynamicLoaderCache::Instance) {
cmDynamicLoaderCache::Instance = new cmDynamicLoaderCache;
}
return cmDynamicLoaderCache::Instance; return cmDynamicLoaderCache::Instance;
} }
@@ -80,15 +78,15 @@ cmsys::DynamicLoader::LibraryHandle cmDynamicLoader::OpenLibrary(
const char* libname) const char* libname)
{ {
cmsys::DynamicLoader::LibraryHandle lh; cmsys::DynamicLoader::LibraryHandle lh;
if (cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh)) { if (cmDynamicLoaderCache::GetInstance().GetCacheFile(libname, lh)) {
return lh; return lh;
} }
lh = cmsys::DynamicLoader::OpenLibrary(libname); lh = cmsys::DynamicLoader::OpenLibrary(libname);
cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh); cmDynamicLoaderCache::GetInstance().CacheFile(libname, lh);
return lh; return lh;
} }
void cmDynamicLoader::FlushCache() void cmDynamicLoader::FlushCache()
{ {
cmDynamicLoaderCache::GetInstance()->FlushCache(); cmDynamicLoaderCache::GetInstance().FlushCache();
} }

View File

@@ -41,7 +41,6 @@ cmGlobalUnixMakefileGenerator3::cmGlobalUnixMakefileGenerator3(cmake* cm)
#else #else
this->UseLinkScript = true; this->UseLinkScript = true;
#endif #endif
this->CommandDatabase = nullptr;
this->IncludeDirective = "include"; this->IncludeDirective = "include";
this->DefineWindowsNULL = false; this->DefineWindowsNULL = false;
@@ -49,6 +48,8 @@ cmGlobalUnixMakefileGenerator3::cmGlobalUnixMakefileGenerator3(cmake* cm)
this->UnixCD = true; this->UnixCD = true;
} }
cmGlobalUnixMakefileGenerator3::~cmGlobalUnixMakefileGenerator3() = default;
void cmGlobalUnixMakefileGenerator3::EnableLanguage( void cmGlobalUnixMakefileGenerator3::EnableLanguage(
std::vector<std::string> const& languages, cmMakefile* mf, bool optional) std::vector<std::string> const& languages, cmMakefile* mf, bool optional)
{ {
@@ -157,10 +158,9 @@ void cmGlobalUnixMakefileGenerator3::Generate()
this->WriteMainMakefile2(); this->WriteMainMakefile2();
this->WriteMainCMakefile(); this->WriteMainCMakefile();
if (this->CommandDatabase != nullptr) { if (this->CommandDatabase) {
*this->CommandDatabase << std::endl << "]"; *this->CommandDatabase << std::endl << "]";
delete this->CommandDatabase; this->CommandDatabase.reset();
this->CommandDatabase = nullptr;
} }
} }
@@ -168,11 +168,12 @@ void cmGlobalUnixMakefileGenerator3::AddCXXCompileCommand(
const std::string& sourceFile, const std::string& workingDirectory, const std::string& sourceFile, const std::string& workingDirectory,
const std::string& compileCommand) const std::string& compileCommand)
{ {
if (this->CommandDatabase == nullptr) { if (!this->CommandDatabase) {
std::string commandDatabaseName = std::string commandDatabaseName =
this->GetCMakeInstance()->GetHomeOutputDirectory() + this->GetCMakeInstance()->GetHomeOutputDirectory() +
"/compile_commands.json"; "/compile_commands.json";
this->CommandDatabase = new cmGeneratedFileStream(commandDatabaseName); this->CommandDatabase =
cm::make_unique<cmGeneratedFileStream>(commandDatabaseName);
*this->CommandDatabase << "[" << std::endl; *this->CommandDatabase << "[" << std::endl;
} else { } else {
*this->CommandDatabase << "," << std::endl; *this->CommandDatabase << "," << std::endl;

View File

@@ -68,6 +68,13 @@ public:
new cmGlobalGeneratorSimpleFactory<cmGlobalUnixMakefileGenerator3>()); new cmGlobalGeneratorSimpleFactory<cmGlobalUnixMakefileGenerator3>());
} }
~cmGlobalUnixMakefileGenerator3() override;
cmGlobalUnixMakefileGenerator3(const cmGlobalUnixMakefileGenerator3&) =
delete;
cmGlobalUnixMakefileGenerator3& operator=(
const cmGlobalUnixMakefileGenerator3&) = delete;
//! Get the name for the generator. //! Get the name for the generator.
std::string GetName() const override std::string GetName() const override
{ {
@@ -232,7 +239,7 @@ protected:
std::set<cmGeneratorTarget const*>& emitted); std::set<cmGeneratorTarget const*>& emitted);
size_t CountProgressMarksInAll(const cmLocalGenerator& lg); size_t CountProgressMarksInAll(const cmLocalGenerator& lg);
cmGeneratedFileStream* CommandDatabase; std::unique_ptr<cmGeneratedFileStream> CommandDatabase;
private: private:
const char* GetBuildIgnoreErrorsFlag() const override { return "-i"; } const char* GetBuildIgnoreErrorsFlag() const override { return "-i"; }

View File

@@ -7,6 +7,8 @@
#include <sstream> #include <sstream>
#include <utility> #include <utility>
#include <cm/memory>
#ifndef CMAKE_BOOTSTRAP #ifndef CMAKE_BOOTSTRAP
# include "cmExportInstallAndroidMKGenerator.h" # include "cmExportInstallAndroidMKGenerator.h"
#endif #endif
@@ -33,18 +35,15 @@ cmInstallExportGenerator::cmInstallExportGenerator(
{ {
if (android) { if (android) {
#ifndef CMAKE_BOOTSTRAP #ifndef CMAKE_BOOTSTRAP
this->EFGen = new cmExportInstallAndroidMKGenerator(this); this->EFGen = cm::make_unique<cmExportInstallAndroidMKGenerator>(this);
#endif #endif
} else { } else {
this->EFGen = new cmExportInstallFileGenerator(this); this->EFGen = cm::make_unique<cmExportInstallFileGenerator>(this);
} }
exportSet->AddInstallation(this); exportSet->AddInstallation(this);
} }
cmInstallExportGenerator::~cmInstallExportGenerator() cmInstallExportGenerator::~cmInstallExportGenerator() = default;
{
delete this->EFGen;
}
bool cmInstallExportGenerator::Compute(cmLocalGenerator* lg) bool cmInstallExportGenerator::Compute(cmLocalGenerator* lg)
{ {

View File

@@ -7,6 +7,7 @@
#include <cstddef> #include <cstddef>
#include <iosfwd> #include <iosfwd>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -30,8 +31,12 @@ public:
bool exclude_from_all, std::string filename, bool exclude_from_all, std::string filename,
std::string name_space, bool exportOld, std::string name_space, bool exportOld,
bool android); bool android);
cmInstallExportGenerator(const cmInstallExportGenerator&) = delete;
~cmInstallExportGenerator() override; ~cmInstallExportGenerator() override;
cmInstallExportGenerator& operator=(const cmInstallExportGenerator&) =
delete;
cmExportSet* GetExportSet() { return this->ExportSet; } cmExportSet* GetExportSet() { return this->ExportSet; }
bool Compute(cmLocalGenerator* lg) override; bool Compute(cmLocalGenerator* lg) override;
@@ -61,7 +66,7 @@ protected:
std::string TempDir; std::string TempDir;
std::string MainImportFile; std::string MainImportFile;
cmExportInstallFileGenerator* EFGen; std::unique_ptr<cmExportInstallFileGenerator> EFGen;
}; };
#endif #endif

View File

@@ -79,14 +79,14 @@ public:
// A load_command and its associated data // A load_command and its associated data
struct RawLoadCommand struct RawLoadCommand
{ {
uint32_t type(const cmMachOHeaderAndLoadCommands* m) const uint32_t type(const cmMachOHeaderAndLoadCommands& m) const
{ {
if (this->LoadCommand.size() < sizeof(load_command)) { if (this->LoadCommand.size() < sizeof(load_command)) {
return 0; return 0;
} }
const load_command* cmd = const load_command* cmd =
reinterpret_cast<const load_command*>(&this->LoadCommand[0]); reinterpret_cast<const load_command*>(&this->LoadCommand[0]);
return m->swap(cmd->cmd); return m.swap(cmd->cmd);
} }
std::vector<char> LoadCommand; std::vector<char> LoadCommand;
}; };
@@ -186,8 +186,11 @@ class cmMachOInternal
{ {
public: public:
cmMachOInternal(const char* fname); cmMachOInternal(const char* fname);
cmMachOInternal(const cmMachOInternal&) = delete;
~cmMachOInternal(); ~cmMachOInternal();
cmMachOInternal& operator=(const cmMachOInternal&) = delete;
// read a Mach-O file // read a Mach-O file
bool read_mach_o(uint32_t file_offset); bool read_mach_o(uint32_t file_offset);
@@ -202,7 +205,7 @@ public:
std::string ErrorMessage; std::string ErrorMessage;
// the list of Mach-O's // the list of Mach-O's
std::vector<cmMachOHeaderAndLoadCommands*> MachOList; std::vector<std::unique_ptr<cmMachOHeaderAndLoadCommands>> MachOList;
}; };
cmMachOInternal::cmMachOInternal(const char* fname) cmMachOInternal::cmMachOInternal(const char* fname)
@@ -260,12 +263,7 @@ cmMachOInternal::cmMachOInternal(const char* fname)
} }
} }
cmMachOInternal::~cmMachOInternal() cmMachOInternal::~cmMachOInternal() = default;
{
for (auto& i : this->MachOList) {
delete i;
}
}
bool cmMachOInternal::read_mach_o(uint32_t file_offset) bool cmMachOInternal::read_mach_o(uint32_t file_offset)
{ {
@@ -280,25 +278,25 @@ bool cmMachOInternal::read_mach_o(uint32_t file_offset)
return false; return false;
} }
cmMachOHeaderAndLoadCommands* f = nullptr; std::unique_ptr<cmMachOHeaderAndLoadCommands> f;
if (magic == MH_CIGAM || magic == MH_MAGIC) { if (magic == MH_CIGAM || magic == MH_MAGIC) {
bool swap = false; bool swap = false;
if (magic == MH_CIGAM) { if (magic == MH_CIGAM) {
swap = true; swap = true;
} }
f = new cmMachOHeaderAndLoadCommandsImpl<mach_header>(swap); f = cm::make_unique<cmMachOHeaderAndLoadCommandsImpl<mach_header>>(swap);
} else if (magic == MH_CIGAM_64 || magic == MH_MAGIC_64) { } else if (magic == MH_CIGAM_64 || magic == MH_MAGIC_64) {
bool swap = false; bool swap = false;
if (magic == MH_CIGAM_64) { if (magic == MH_CIGAM_64) {
swap = true; swap = true;
} }
f = new cmMachOHeaderAndLoadCommandsImpl<mach_header_64>(swap); f =
cm::make_unique<cmMachOHeaderAndLoadCommandsImpl<mach_header_64>>(swap);
} }
if (f && f->read_mach_o(this->Fin)) { if (f && f->read_mach_o(this->Fin)) {
this->MachOList.push_back(f); this->MachOList.push_back(std::move(f));
} else { } else {
delete f;
this->ErrorMessage = "Failed to read Mach-O header."; this->ErrorMessage = "Failed to read Mach-O header.";
return false; return false;
} }
@@ -333,11 +331,12 @@ bool cmMachO::GetInstallName(std::string& install_name)
} }
// grab the first Mach-O and get the install name from that one // grab the first Mach-O and get the install name from that one
cmMachOHeaderAndLoadCommands* macho = this->Internal->MachOList[0]; std::unique_ptr<cmMachOHeaderAndLoadCommands>& macho =
this->Internal->MachOList[0];
for (size_t i = 0; i < macho->load_commands().size(); i++) { for (size_t i = 0; i < macho->load_commands().size(); i++) {
const cmMachOHeaderAndLoadCommands::RawLoadCommand& cmd = const cmMachOHeaderAndLoadCommands::RawLoadCommand& cmd =
macho->load_commands()[i]; macho->load_commands()[i];
uint32_t lc_cmd = cmd.type(macho); uint32_t lc_cmd = cmd.type(*macho);
if (lc_cmd == LC_ID_DYLIB || lc_cmd == LC_LOAD_WEAK_DYLIB || if (lc_cmd == LC_ID_DYLIB || lc_cmd == LC_LOAD_WEAK_DYLIB ||
lc_cmd == LC_LOAD_DYLIB) { lc_cmd == LC_LOAD_DYLIB) {
if (sizeof(dylib_command) < cmd.LoadCommand.size()) { if (sizeof(dylib_command) < cmd.LoadCommand.size()) {

View File

@@ -59,16 +59,12 @@ cmServer::cmServer(cmConnection* conn, bool supportExperimental)
, SupportExperimental(supportExperimental) , SupportExperimental(supportExperimental)
{ {
// Register supported protocols: // Register supported protocols:
this->RegisterProtocol(new cmServerProtocol1); this->RegisterProtocol(cm::make_unique<cmServerProtocol1>());
} }
cmServer::~cmServer() cmServer::~cmServer()
{ {
Close(); Close();
for (cmServerProtocol* p : this->SupportedProtocols) {
delete p;
}
} }
void cmServer::ProcessRequest(cmConnection* connection, void cmServer::ProcessRequest(cmConnection* connection,
@@ -117,22 +113,22 @@ void cmServer::ProcessRequest(cmConnection* connection,
} }
} }
void cmServer::RegisterProtocol(cmServerProtocol* protocol) void cmServer::RegisterProtocol(std::unique_ptr<cmServerProtocol> protocol)
{ {
if (protocol->IsExperimental() && !this->SupportExperimental) { if (protocol->IsExperimental() && !this->SupportExperimental) {
delete protocol; protocol.reset();
return; return;
} }
auto version = protocol->ProtocolVersion(); auto version = protocol->ProtocolVersion();
assert(version.first >= 0); assert(version.first >= 0);
assert(version.second >= 0); assert(version.second >= 0);
auto it = std::find_if(this->SupportedProtocols.begin(), auto it = std::find_if(
this->SupportedProtocols.end(), this->SupportedProtocols.begin(), this->SupportedProtocols.end(),
[version](cmServerProtocol* p) { [version](const std::unique_ptr<cmServerProtocol>& p) {
return p->ProtocolVersion() == version; return p->ProtocolVersion() == version;
}); });
if (it == this->SupportedProtocols.end()) { if (it == this->SupportedProtocols.end()) {
this->SupportedProtocols.push_back(protocol); this->SupportedProtocols.push_back(std::move(protocol));
} }
} }
@@ -297,19 +293,20 @@ void cmServer::WriteJsonObject(cmConnection* connection,
} }
cmServerProtocol* cmServer::FindMatchingProtocol( cmServerProtocol* cmServer::FindMatchingProtocol(
const std::vector<cmServerProtocol*>& protocols, int major, int minor) const std::vector<std::unique_ptr<cmServerProtocol>>& protocols, int major,
int minor)
{ {
cmServerProtocol* bestMatch = nullptr; cmServerProtocol* bestMatch = nullptr;
for (auto protocol : protocols) { for (const auto& protocol : protocols) {
auto version = protocol->ProtocolVersion(); auto version = protocol->ProtocolVersion();
if (major != version.first) { if (major != version.first) {
continue; continue;
} }
if (minor == version.second) { if (minor == version.second) {
return protocol; return protocol.get();
} }
if (!bestMatch || bestMatch->ProtocolVersion().second < version.second) { if (!bestMatch || bestMatch->ProtocolVersion().second < version.second) {
bestMatch = protocol; bestMatch = protocol.get();
} }
} }
return minor < 0 ? bestMatch : nullptr; return minor < 0 ? bestMatch : nullptr;

View File

@@ -103,7 +103,7 @@ public:
cmFileMonitor* FileMonitor() const; cmFileMonitor* FileMonitor() const;
private: private:
void RegisterProtocol(cmServerProtocol* protocol); void RegisterProtocol(std::unique_ptr<cmServerProtocol> protocol);
// Callbacks from cmServerConnection: // Callbacks from cmServerConnection:
@@ -149,12 +149,13 @@ private:
const DebugInfo* debug) const; const DebugInfo* debug) const;
static cmServerProtocol* FindMatchingProtocol( static cmServerProtocol* FindMatchingProtocol(
const std::vector<cmServerProtocol*>& protocols, int major, int minor); const std::vector<std::unique_ptr<cmServerProtocol>>& protocols, int major,
int minor);
const bool SupportExperimental; const bool SupportExperimental;
cmServerProtocol* Protocol = nullptr; cmServerProtocol* Protocol = nullptr;
std::vector<cmServerProtocol*> SupportedProtocols; std::vector<std::unique_ptr<cmServerProtocol>> SupportedProtocols;
friend class cmServerProtocol; friend class cmServerProtocol;
friend class cmServerRequest; friend class cmServerRequest;

View File

@@ -4,6 +4,8 @@
#include <utility> #include <utility>
#include <cm/memory>
#include "cmStringAlgorithms.h" #include "cmStringAlgorithms.h"
class cmSourceGroupInternals class cmSourceGroupInternals
@@ -16,7 +18,7 @@ cmSourceGroup::cmSourceGroup(std::string name, const char* regex,
const char* parentName) const char* parentName)
: Name(std::move(name)) : Name(std::move(name))
{ {
this->Internal = new cmSourceGroupInternals; this->Internal = cm::make_unique<cmSourceGroupInternals>();
this->SetGroupRegex(regex); this->SetGroupRegex(regex);
if (parentName) { if (parentName) {
this->FullName = cmStrCat(parentName, '\\'); this->FullName = cmStrCat(parentName, '\\');
@@ -24,10 +26,7 @@ cmSourceGroup::cmSourceGroup(std::string name, const char* regex,
this->FullName += this->Name; this->FullName += this->Name;
} }
cmSourceGroup::~cmSourceGroup() cmSourceGroup::~cmSourceGroup() = default;
{
delete this->Internal;
}
cmSourceGroup::cmSourceGroup(cmSourceGroup const& r) cmSourceGroup::cmSourceGroup(cmSourceGroup const& r)
{ {
@@ -36,7 +35,7 @@ cmSourceGroup::cmSourceGroup(cmSourceGroup const& r)
this->GroupRegex = r.GroupRegex; this->GroupRegex = r.GroupRegex;
this->GroupFiles = r.GroupFiles; this->GroupFiles = r.GroupFiles;
this->SourceFiles = r.SourceFiles; this->SourceFiles = r.SourceFiles;
this->Internal = new cmSourceGroupInternals(*r.Internal); this->Internal = cm::make_unique<cmSourceGroupInternals>(*r.Internal);
} }
cmSourceGroup& cmSourceGroup::operator=(cmSourceGroup const& r) cmSourceGroup& cmSourceGroup::operator=(cmSourceGroup const& r)

View File

@@ -5,6 +5,7 @@
#include "cmConfigure.h" // IWYU pragma: keep #include "cmConfigure.h" // IWYU pragma: keep
#include <memory>
#include <set> #include <set>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -122,7 +123,7 @@ private:
*/ */
std::vector<const cmSourceFile*> SourceFiles; std::vector<const cmSourceFile*> SourceFiles;
cmSourceGroupInternals* Internal; std::unique_ptr<cmSourceGroupInternals> Internal;
}; };
#endif #endif

View File

@@ -25,6 +25,9 @@
#endif #endif
#if !defined(CMAKE_BOOTSTRAP) #if !defined(CMAKE_BOOTSTRAP)
# if defined(_WIN32)
# include <cm/memory>
# endif
# include "cmCryptoHash.h" # include "cmCryptoHash.h"
#endif #endif
@@ -908,7 +911,6 @@ std::string cmSystemTools::ComputeCertificateThumbprint(
std::string thumbprint; std::string thumbprint;
#if !defined(CMAKE_BOOTSTRAP) && defined(_WIN32) #if !defined(CMAKE_BOOTSTRAP) && defined(_WIN32)
BYTE* certData = NULL;
CRYPT_INTEGER_BLOB cryptBlob; CRYPT_INTEGER_BLOB cryptBlob;
HCERTSTORE certStore = NULL; HCERTSTORE certStore = NULL;
PCCERT_CONTEXT certContext = NULL; PCCERT_CONTEXT certContext = NULL;
@@ -920,12 +922,12 @@ std::string cmSystemTools::ComputeCertificateThumbprint(
if (certFile != INVALID_HANDLE_VALUE && certFile != NULL) { if (certFile != INVALID_HANDLE_VALUE && certFile != NULL) {
DWORD fileSize = GetFileSize(certFile, NULL); DWORD fileSize = GetFileSize(certFile, NULL);
if (fileSize != INVALID_FILE_SIZE) { if (fileSize != INVALID_FILE_SIZE) {
certData = new BYTE[fileSize]; auto certData = cm::make_unique<BYTE[]>(fileSize);
if (certData != NULL) { if (certData != NULL) {
DWORD dwRead = 0; DWORD dwRead = 0;
if (ReadFile(certFile, certData, fileSize, &dwRead, NULL)) { if (ReadFile(certFile, certData.get(), fileSize, &dwRead, NULL)) {
cryptBlob.cbData = fileSize; cryptBlob.cbData = fileSize;
cryptBlob.pbData = certData; cryptBlob.pbData = certData.get();
// Verify that this is a valid cert // Verify that this is a valid cert
if (PFXIsPFXBlob(&cryptBlob)) { if (PFXIsPFXBlob(&cryptBlob)) {
@@ -961,7 +963,6 @@ std::string cmSystemTools::ComputeCertificateThumbprint(
} }
} }
} }
delete[] certData;
} }
} }
CloseHandle(certFile); CloseHandle(certFile);