Add copying utility methods to Arena
This commit is contained in:
@@ -123,16 +123,6 @@ static void validate_or_abort(bool condition, const char *message,
|
||||
}
|
||||
}
|
||||
|
||||
// Helper to copy a string into arena memory
|
||||
static std::string_view arena_copy_string(std::string_view str, Arena &arena) {
|
||||
if (str.empty()) {
|
||||
return std::string_view{};
|
||||
}
|
||||
char *copied = arena.allocate<char>(str.size());
|
||||
std::memcpy(copied, str.data(), str.size());
|
||||
return std::string_view(copied, str.size());
|
||||
}
|
||||
|
||||
// Arena-based labels key for second level of map
|
||||
// Uses string_view containing labels in Prometheus text format
|
||||
struct LabelsKey {
|
||||
@@ -149,8 +139,8 @@ struct LabelsKey {
|
||||
validate_or_abort(is_valid_label_value(value), "invalid label value",
|
||||
value);
|
||||
|
||||
auto key_view = arena_copy_string(key, arena);
|
||||
auto value_view = arena_copy_string(value, arena);
|
||||
auto key_view = arena.copy_string(key);
|
||||
auto value_view = arena.copy_string(value);
|
||||
labels.push_back({key_view, value_view});
|
||||
}
|
||||
|
||||
@@ -581,7 +571,7 @@ struct Metric {
|
||||
}
|
||||
|
||||
// Not found - copy to global arena and intern
|
||||
auto interned_text = arena_copy_string(text, get_global_arena());
|
||||
auto interned_text = get_global_arena().copy_string(text);
|
||||
auto result = interned_set.emplace(interned_text);
|
||||
return *result.first;
|
||||
}
|
||||
@@ -1461,12 +1451,12 @@ Family<Counter> create_counter(std::string_view name, std::string_view help) {
|
||||
std::unique_lock _{Metric::mutex};
|
||||
++Metric::registration_version;
|
||||
auto &global_arena = Metric::get_global_arena();
|
||||
auto name_view = arena_copy_string(name, global_arena);
|
||||
auto name_view = global_arena.copy_string(name);
|
||||
auto &familyPtr = Metric::get_counter_families()[name_view];
|
||||
if (!familyPtr) {
|
||||
familyPtr = global_arena.construct<Family<Counter>::State>(global_arena);
|
||||
familyPtr->name = name_view;
|
||||
familyPtr->help = arena_copy_string(help, global_arena);
|
||||
familyPtr->help = global_arena.copy_string(help);
|
||||
} else {
|
||||
validate_or_abort(
|
||||
familyPtr->help == help,
|
||||
@@ -1483,13 +1473,13 @@ Family<Gauge> create_gauge(std::string_view name, std::string_view help) {
|
||||
std::unique_lock _{Metric::mutex};
|
||||
++Metric::registration_version;
|
||||
auto &global_arena = Metric::get_global_arena();
|
||||
auto name_view = arena_copy_string(name, global_arena);
|
||||
auto name_view = global_arena.copy_string(name);
|
||||
auto &familyPtr = Metric::get_gauge_families()[name_view];
|
||||
if (!familyPtr) {
|
||||
// Family<T>::State instances use Arena::Ptr for automatic cleanup
|
||||
familyPtr = global_arena.construct<Family<Gauge>::State>(global_arena);
|
||||
familyPtr->name = name_view;
|
||||
familyPtr->help = arena_copy_string(help, global_arena);
|
||||
familyPtr->help = global_arena.copy_string(help);
|
||||
} else {
|
||||
validate_or_abort(
|
||||
familyPtr->help == help,
|
||||
@@ -1507,13 +1497,13 @@ Family<Histogram> create_histogram(std::string_view name, std::string_view help,
|
||||
std::unique_lock _{Metric::mutex};
|
||||
++Metric::registration_version;
|
||||
auto &global_arena = Metric::get_global_arena();
|
||||
auto name_view = arena_copy_string(name, global_arena);
|
||||
auto name_view = global_arena.copy_string(name);
|
||||
auto &family_ptr = Metric::get_histogram_families()[name_view];
|
||||
if (!family_ptr) {
|
||||
// Family<T>::State instances use Arena::Ptr for automatic cleanup
|
||||
family_ptr = global_arena.construct<Family<Histogram>::State>(global_arena);
|
||||
family_ptr->name = name_view;
|
||||
family_ptr->help = arena_copy_string(help, global_arena);
|
||||
family_ptr->help = global_arena.copy_string(help);
|
||||
|
||||
// DESIGN: Prometheus-compatible histogram buckets
|
||||
// Convert to vector for sorting
|
||||
|
||||
Reference in New Issue
Block a user