u/var: Put in a namespace and add some documentation

This commit is contained in:
Jakob Bornecrantz 2022-05-17 14:54:40 +01:00
parent 812c6e46b0
commit cf9c5b9dc7

View file

@ -1,4 +1,4 @@
// Copyright 2019, Collabora, Ltd. // Copyright 2019-2022, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0 // SPDX-License-Identifier: BSL-1.0
/*! /*!
* @file * @file
@ -19,19 +19,27 @@
#include <unordered_map> #include <unordered_map>
namespace xrt::auxiliary::util {
/* /*
* *
* Enums, Classes and Defines. * Enums, Classes and Defines.
* *
*/ */
/*!
* Simple container for the variable information.
*/
class Var class Var
{ {
public: public:
struct u_var_info info = {}; struct u_var_info info = {};
}; };
/*!
* Object that has a series of tracked variables.
*/
class Obj class Obj
{ {
public: public:
@ -39,6 +47,9 @@ public:
std::vector<Var> vars = {}; std::vector<Var> vars = {};
}; };
/*!
* Object that has a series of tracked variables.
*/
class Tracker class Tracker
{ {
public: public:
@ -59,7 +70,10 @@ public:
} }
}; };
static class Tracker tracker; /*!
* Global variable tracking state.
*/
static class Tracker gTracker;
/* /*
@ -71,20 +85,20 @@ static class Tracker tracker;
static bool static bool
get_on() get_on()
{ {
if (tracker.tested) { if (gTracker.tested) {
return tracker.on; return gTracker.on;
} }
tracker.on = debug_get_bool_option("XRT_TRACK_VARIABLES", false); gTracker.on = debug_get_bool_option("XRT_TRACK_VARIABLES", false);
tracker.tested = true; gTracker.tested = true;
return tracker.on; return gTracker.on;
} }
static void static void
add_var(void *root, void *ptr, u_var_kind kind, const char *c_name) add_var(void *root, void *ptr, u_var_kind kind, const char *c_name)
{ {
auto s = tracker.map.find((ptrdiff_t)root); auto s = gTracker.map.find((ptrdiff_t)root);
if (s == tracker.map.end()) { if (s == gTracker.map.end()) {
return; return;
} }
@ -106,8 +120,8 @@ add_var(void *root, void *ptr, u_var_kind kind, const char *c_name)
extern "C" void extern "C" void
u_var_force_on(void) u_var_force_on(void)
{ {
tracker.on = true; gTracker.on = true;
tracker.tested = true; gTracker.tested = true;
} }
extern "C" void extern "C" void
@ -120,14 +134,14 @@ u_var_add_root(void *root, const char *c_name, bool number)
auto name = std::string(c_name); auto name = std::string(c_name);
if (number) { if (number) {
int count = tracker.getNumber(name); int count = gTracker.getNumber(name);
std::stringstream ss; std::stringstream ss;
ss << name << " #" << count; ss << name << " #" << count;
name = ss.str(); name = ss.str();
} }
auto &obj = tracker.map[(ptrdiff_t)root] = Obj(); auto &obj = gTracker.map[(ptrdiff_t)root] = Obj();
obj.name = name; obj.name = name;
} }
@ -138,12 +152,12 @@ u_var_remove_root(void *root)
return; return;
} }
auto s = tracker.map.find((ptrdiff_t)root); auto s = gTracker.map.find((ptrdiff_t)root);
if (s == tracker.map.end()) { if (s == gTracker.map.end()) {
return; return;
} }
tracker.map.erase(s); gTracker.map.erase(s);
} }
extern "C" void extern "C" void
@ -154,9 +168,9 @@ u_var_visit(u_var_root_cb enter_cb, u_var_root_cb exit_cb, u_var_elm_cb elem_cb,
} }
std::vector<Obj *> tmp; std::vector<Obj *> tmp;
tmp.reserve(tracker.map.size()); tmp.reserve(gTracker.map.size());
for (auto &n : tracker.map) { for (auto &n : gTracker.map) {
tmp.push_back(&n.second); tmp.push_back(&n.second);
} }
@ -183,3 +197,5 @@ u_var_visit(u_var_root_cb enter_cb, u_var_root_cb exit_cb, u_var_elm_cb elem_cb,
U_VAR_ADD_FUNCS() U_VAR_ADD_FUNCS()
#undef ADD_FUNC #undef ADD_FUNC
} // namespace xrt::auxiliary::util