Skip to content

Commit

Permalink
Support constness changes made in PCP 3.11.5+
Browse files Browse the repository at this point in the history
In commit [384625f], some of PCP's PMDA interface struct members and
function parameters were changed from `char *` to `const char *`.

Some PMDA++ code was using those struct members as intermediaries in
some chained assignment operations, making PMDA++ unecessarily
sensitive to that change. This commit breaks those assignments into
separate steps, so the `const` conversion does not affect the separate-
but-related cleanup stack assignment (which necessarily required non-
const). Arguably, this would have been "best practice" to begin with.

Additionally, this allowed PMDA++ to avoid some undesirable
(un)constnesss casts when invoking `pmdaDaemon` on newer PCP versions.

[384625f]: performancecopilot/pcp@384625f
  • Loading branch information
pcolby committed Oct 14, 2017
1 parent 1dc4c61 commit 41dd3e2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
25 changes: 18 additions & 7 deletions include/pcp-cpp/pmda.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,21 @@ class pmda {

// Initialize the PMDA to run as a daemon.
pmdaInterface interface;
// PCP 3.11.5 updated the pmdaDaemon signature to use const char pointers.
// So for earlier versions only, we need to cast away some constness.
// Note, the PM_VERSION* macros themselves weren't added until PCP 3.10.5.
#if defined PM_VERSION_CURRENT && PM_VERSION_CURRENT < 0x30B05 // 3.11.5
#define PCP_CPP_PMDA_CONST_CHAR(str) const_cast<char *>(str)
#else
#define PCP_CPP_PMDA_CONST_CHAR(str) str // Do nothing.
#endif
pmdaDaemon(&interface, PCP_CPP_PMDA_INTERFACE_VERSION,
const_cast<char *>(program_name.c_str()),
PCP_CPP_PMDA_CONST_CHAR(program_name.c_str()),
get_default_pmda_domain_number(),
const_cast<char *>(log_file_pathname.c_str()),
(help_text_pathname.empty()) ? NULL : const_cast<char *>(help_text_pathname.c_str())
PCP_CPP_PMDA_CONST_CHAR(log_file_pathname.c_str()),
(help_text_pathname.empty()) ? NULL : PCP_CPP_PMDA_CONST_CHAR(help_text_pathname.c_str())
);
#undef PCP_CPP_PMDA_CONST_CHAR

// Parse the command line options.
if (!parse_command_line(argc, argv, interface)) {
Expand Down Expand Up @@ -560,16 +569,18 @@ class pmda {
interface.domain = options.at("domain").as<int>();
}
if (options.count("help-file") > 0) {
free_on_destruction.push(interface.version.two.ext->e_helptext =
strdup(options.at("help-file").as<std::string>().c_str()));
char * const help_file = strdup(options.at("help-file").as<std::string>().c_str());
interface.version.two.ext->e_helptext = help_file;
free_on_destruction.push(help_file);
}
if (options.count("inet") > 0) {
interface.version.two.ext->e_io = pmdaInet;
interface.version.two.ext->e_port = options.at("inet").as<int>();
}
if (options.count("log-file") > 0) {
free_on_destruction.push(interface.version.two.ext->e_logfile =
strdup(options.at("log-file").as<std::string>().c_str()));
char * const log_file = strdup(options.at("log-file").as<std::string>().c_str());
interface.version.two.ext->e_logfile = log_file;
free_on_destruction.push(log_file);
}
if (options.count("pipe") > 0) {
interface.version.two.ext->e_io = pmdaPipe;
Expand Down
7 changes: 7 additions & 0 deletions test/unit/src/fake_libpcp-pmda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,15 @@ void pmdaConnect(pmdaInterface */*dispatch*/)

}

// PCP 3.11.5 updated the pmdaDaemon signature to use const char pointers.
// Note, the PM_VERSION* macros themselves weren't added until PCP 3.10.5.
#if defined PM_VERSION_CURRENT && PM_VERSION_CURRENT < 0x30B05 // 3.11.5
void pmdaDaemon(pmdaInterface */*dispatch*/, int /*interface*/, char */*name*/,
int /*domain*/, char */*logfile*/, char */*helptext*/)
#else
void pmdaDaemon(pmdaInterface */*dispatch*/, int /*interface*/, const char */*name*/,
int /*domain*/, const char */*logfile*/, const char */*helptext*/)
#endif
{

}
Expand Down

0 comments on commit 41dd3e2

Please sign in to comment.