#include "simgrid/forward.h"
#include "src/kernel/activity/ActivityImpl.hpp"
+#include <array>
#include <csignal>
#include <functional>
XBT_PRIVATE void SIMIX_context_mod_exit();
#ifndef WIN32
-XBT_PUBLIC_DATA unsigned char sigsegv_stack[SIGSTKSZ];
+XBT_PUBLIC_DATA std::array<unsigned char, SIGSTKSZ> sigsegv_stack;
#endif
#endif
#ifndef WIN32
/* Install alternate signal stack, for SIGSEGV handler. */
stack_t stack;
- stack.ss_sp = sigsegv_stack;
- stack.ss_size = sizeof sigsegv_stack;
+ stack.ss_sp = sigsegv_stack.data();
+ stack.ss_size = sigsegv_stack.size();
stack.ss_flags = 0;
sigaltstack(&stack, nullptr);
#endif
* @return the equivalent as a readable string
*/
const char *__get_state_name(e_SD_task_state_t state){
- static std::string state_names[7] =
- { "not scheduled", "schedulable", "scheduled", "runnable","running", "done", "failed" };
- return state_names[static_cast<int>(log2(static_cast<double>(state)))].data();
+ static std::array<const char*, 7> state_names{
+ {"not scheduled", "schedulable", "scheduled", "runnable", "running", "done", "failed"}};
+ return state_names.at(static_cast<int>(log2(static_cast<double>(state))));
}
/**
XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_popping);
/** @brief Simcalls' names (generated from src/simix/simcalls.in) */
-const char* simcall_names[] = {
+const std::array<const char*, NUM_SIMCALLS> simcall_names{{
"SIMCALL_NONE",
"SIMCALL_EXECUTION_WAITANY_FOR",
"SIMCALL_COMM_SEND",
"SIMCALL_MC_RANDOM",
"SIMCALL_RUN_KERNEL",
"SIMCALL_RUN_BLOCKING",
-};
+}};
/** @private
* @brief (in kernel mode) unpack the simcall and activate the handler
#include "simgrid/forward.h"
#include "src/kernel/activity/ActivityImpl.hpp"
+#include <array>
#include <boost/intrusive_ptr.hpp>
/********************************* Simcalls *********************************/
#include "popping_enum.hpp" /* Definition of e_smx_simcall_t, with one value per simcall */
-XBT_PUBLIC_DATA const char* simcall_names[]; /* Name of each simcall */
+XBT_PUBLIC_DATA const std::array<const char*, NUM_SIMCALLS> simcall_names; /* Name of each simcall */
typedef bool (*simix_match_func_t)(void*, void*, simgrid::kernel::activity::CommImpl*);
typedef void (*simix_copy_data_func_t)(simgrid::kernel::activity::CommImpl*, void*, size_t);
fd.write(
'/** @brief Simcalls\' names (generated from src/simix/simcalls.in) */\n')
- fd.write('const char* simcall_names[] = {\n')
+ fd.write('const std::array<const char*, NUM_SIMCALLS> simcall_names{{\n')
fd.write(' "SIMCALL_NONE",\n')
handle(fd, Simcall.string, simcalls, simcalls_dict)
- fd.write('};\n\n')
+ fd.write('}};\n\n')
fd.write('/** @private\n')
fd.write(
#include "smpi/smpi.h"
#include "xbt/config.hpp"
+#include <initializer_list>
#include <thread>
XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_context, simix, "Context switching mechanism");
-static std::pair<const char*, simgrid::kernel::context::ContextFactoryInitializer> context_factories[] = {
+constexpr std::initializer_list<std::pair<const char*, simgrid::kernel::context::ContextFactoryInitializer>>
+ context_factories = {
#if HAVE_RAW_CONTEXTS
- { "raw", &simgrid::kernel::context::raw_factory },
+ {"raw", &simgrid::kernel::context::raw_factory},
#endif
#if HAVE_UCONTEXT_CONTEXTS
- { "ucontext", &simgrid::kernel::context::sysv_factory },
+ {"ucontext", &simgrid::kernel::context::sysv_factory},
#endif
#if HAVE_BOOST_CONTEXTS
- { "boost", &simgrid::kernel::context::boost_factory },
+ {"boost", &simgrid::kernel::context::boost_factory},
#endif
- { "thread", &simgrid::kernel::context::thread_factory },
+ {"thread", &simgrid::kernel::context::thread_factory},
};
-static_assert(sizeof(context_factories) != 0, "No context factories are enabled for this build");
+static_assert(context_factories.size() > 0, "No context factories are enabled for this build");
// Create the list of possible contexts:
static inline
std::string contexts_list()
{
std::string res;
- const std::size_t n = sizeof(context_factories) / sizeof(context_factories[0]);
- for (std::size_t i = 1; i != n; ++i) {
- res += ", ";
- res += context_factories[i].first;
+ std::string sep = "";
+ for (auto const& factory : context_factories) {
+ res += sep + factory.first;
+ sep = ", ";
}
return res;
}
-static simgrid::config::Flag<std::string> context_factory_name(
- "contexts/factory",
- (std::string("Possible values: ")+contexts_list()).c_str(),
- context_factories[0].first);
+static simgrid::config::Flag<std::string>
+ context_factory_name("contexts/factory", (std::string("Possible values: ") + contexts_list()).c_str(),
+ context_factories.begin()->first);
unsigned smx_context_stack_size;
unsigned smx_context_guard_size;
std::raise(signum);
}
-unsigned char sigsegv_stack[SIGSTKSZ]; /* alternate stack for SIGSEGV handler */
+std::array<unsigned char, SIGSTKSZ> sigsegv_stack; /* alternate stack for SIGSEGV handler */
/**
* Install signal handler for SIGSEGV. Check that nobody has already installed
{
stack_t stack;
stack_t old_stack;
- stack.ss_sp = sigsegv_stack;
- stack.ss_size = sizeof sigsegv_stack;
+ stack.ss_sp = sigsegv_stack.data();
+ stack.ss_size = sigsegv_stack.size();
stack.ss_flags = 0;
if (sigaltstack(&stack, &old_stack) == -1) {
class BacktraceImpl {
#if HAVE_BOOST_STACKTRACE_BACKTRACE || HAVE_BOOST_STACKTRACE_ADDR2LINE
const boost::stacktrace::stacktrace st = boost::stacktrace::stacktrace();
-#else
- const char st[1] = ""; // fallback value
-#endif
public:
std::string resolve() const
{
ss << st;
return ss.str();
}
+#else
+public:
+ std::string resolve() const { return ""; } // fallback value
+#endif
};
Backtrace::Backtrace() : impl_(std::make_shared<BacktraceImpl>()) {}
namespace {
-const char* true_values[] = {
- "yes", "on", "true", "1"
-};
-const char* false_values[] = {
- "no", "off", "false", "0"
-};
-
static bool parse_bool(const char* value)
{
- for (const char* const& true_value : true_values)
+ for (const char* true_value : {"yes", "on", "true", "1"})
if (std::strcmp(true_value, value) == 0)
return true;
- for (const char* const& false_value : false_values)
+ for (const char* false_value : {"no", "off", "false", "0"})
if (std::strcmp(false_value, value) == 0)
return false;
throw std::range_error("not a boolean");
#include "xbt/string.hpp"
#include <algorithm>
+#include <array>
#include <mutex>
#include <string>
#include <vector>
static std::vector<xbt_log_setting_t> xbt_log_settings;
-const char *xbt_log_priority_names[8] = {
- "NONE",
- "TRACE",
- "DEBUG",
- "VERBOSE",
- "INFO",
- "WARNING",
- "ERROR",
- "CRITICAL"
-};
+const std::array<const char*, 8> xbt_log_priority_names{
+ {"NONE", "TRACE", "DEBUG", "VERBOSE", "INFO", "WARNING", "ERROR", "CRITICAL"}};
s_xbt_log_category_t _XBT_LOGV(XBT_LOG_ROOT_CAT) = {
nullptr /*parent */,
const xbt_log_category_s* cat = ev->cat;
xbt_assert(ev->priority >= 0, "Negative logging priority naturally forbidden");
- xbt_assert(static_cast<size_t>(ev->priority) < sizeof(xbt_log_priority_names)/sizeof(xbt_log_priority_names[0]),
+ xbt_assert(static_cast<size_t>(ev->priority) < xbt_log_priority_names.size(),
"Priority %d is greater than the biggest allowed value", ev->priority);
while (true) {
/* First, try with a static buffer */
int done = 0;
- char buff[XBT_LOG_STATIC_BUFFER_SIZE];
- ev->buffer = buff;
- ev->buffer_size = sizeof buff;
+ std::array<char, XBT_LOG_STATIC_BUFFER_SIZE> buff;
+ ev->buffer = buff.data();
+ ev->buffer_size = buff.size();
va_start(ev->ap, fmt);
done = cat->layout->do_layout(cat->layout, ev, fmt);
va_end(ev->ap);
ev->buffer = nullptr; // Calm down, static analyzers, this pointer to local array won't leak out of the scope.
if (done) {
- appender->do_append(appender, buff);
+ appender->do_append(appender, buff.data());
} else {
/* The static buffer was too small, use a dynamically expanded one */
ev->buffer_size = XBT_LOG_DYNAMIC_BUFFER_SIZE;
#define LOG_PRIVATE_H
#include "xbt/log.h"
+#include <array>
+
struct xbt_log_appender_s {
void (*do_append)(const s_xbt_log_appender_t* this_appender, const char* event);
void (*free_)(const s_xbt_log_appender_t* this_);
void *data;
};
+extern const std::array<const char*, 8> xbt_log_priority_names;
+
/**
* @ingroup XBT_log_implem
* @param cat the category (not only its name, but the variable)
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
+#include <array>
#include <cstdio>
#include <cstdlib>
#include <cstring>
/* Tokenize the line using spaces as delimiters and store each token in lfields array. We expect 5 tokens for 6 fields */
char* saveptr = nullptr; // for strtok_r()
- char* lfields[6];
+ std::array<char*, 6> lfields;
lfields[0] = strtok_r(line, " ", &saveptr);
int i;
//Split
if(data->file)
fclose(data->file);
- char newname[512];
char* pre=xbt_strdup(data->filename);
char* sep=strchr(pre,'%');
if(!sep)
sep=pre+strlen(pre);
char* post=sep+1;
*sep='\0';
- snprintf(newname,511,"%s%i%s",pre,data->count,post);
+ std::string newname = pre + std::to_string(data->count) + post;
data->count++;
- data->file= fopen(newname, "w");
+ data->file = fopen(newname.c_str(), "w");
if (data->file == nullptr)
- xbt_die("Cannot open file: %s: %s", newname, strerror(errno));
+ xbt_die("Cannot open file: %s: %s", newname.c_str(), strerror(errno));
xbt_free(pre);
}
}
#include <algorithm>
#include <cstdio>
-extern const char *xbt_log_priority_names[8];
-
static constexpr const char* ERRMSG =
"Unknown %%%c sequence in layout format (%s).\n"
"Known sequences:\n"
#include "simgrid/host.h" /* sg_host_self_get_name */
#include <cstdio>
-extern const char *xbt_log_priority_names[8];
extern int xbt_log_no_loc;
#define check_overflow(len) \
#endif
#if HAVE_UNISTD_H
+#include <array>
+#include <cerrno>
#include <unistd.h>
#endif
-#include <cerrno>
#include <cstring>
#include <libgen.h> /* POSIX dirname */
simgrid::xbt::Path::Path()
{
#if HAVE_UNISTD_H
- char buffer[2048];
- const char* ret = getcwd(buffer, 2048);
- xbt_assert(ret == buffer, "Error during getcwd: %s", strerror(errno));
- path_ = std::string(buffer);
+ std::array<char, 2048> buffer;
+ const char* cwd = getcwd(buffer.data(), 2048);
+ xbt_assert(cwd != nullptr, "Error during getcwd: %s", strerror(errno));
+ path_ = std::string(cwd);
#else
path_ = std::string(".");
#endif
#include "xbt/misc.h"
#include "xbt/str.h" /* headers of these functions */
#include "xbt/string.hpp"
+#include <array>
/** @brief Splits a string into a dynar of strings
*
{
xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
const char *sep_dflt = " \t\n\r\x0B";
- char is_sep[256] = { 1, 0 };
+ std::array<bool, 256> is_sep;
/* check what are the separators */
- memset(is_sep, 0, sizeof(is_sep));
+ is_sep.fill(false);
if (not sep) {
while (*sep_dflt)
- is_sep[(unsigned char) *sep_dflt++] = 1;
+ is_sep[(unsigned char)*sep_dflt++] = true;
} else {
while (*sep)
- is_sep[(unsigned char) *sep++] = 1;
+ is_sep[(unsigned char)*sep++] = true;
}
- is_sep[0] = 1; /* End of string is also separator */
+ is_sep[0] = true; /* End of string is also separator */
/* Do the job */
const char* p = s;
int main(int argc, char** argv)
{
- char user_data[] = "some user_data";
+ std::string user_data = "some user_data";
/* initialization of SD */
simgrid::s4u::Engine e(&argc, argv);
for (const auto& l : links) {
XBT_INFO("%s: latency = %.5f, bandwidth = %f", l->get_cname(), l->get_latency(), l->get_bandwidth());
- l->set_data(user_data);
- xbt_assert(!strcmp(user_data, static_cast<const char*>(l->get_data())), "User data was corrupted.");
+ l->set_data(&user_data);
+ xbt_assert(user_data == *static_cast<const std::string*>(l->get_data()), "User data was corrupted.");
}
return 0;