Fix memory leaks
This commit is contained in:
@@ -329,19 +329,23 @@ struct Metric {
|
|||||||
|
|
||||||
// Global arena allocator for metric families and persistent global state
|
// Global arena allocator for metric families and persistent global state
|
||||||
static ArenaAllocator &get_global_arena() {
|
static ArenaAllocator &get_global_arena() {
|
||||||
static ArenaAllocator global_arena(64 * 1024); // 64KB initial size
|
static auto *global_arena =
|
||||||
return global_arena;
|
new ArenaAllocator(64 * 1024); // 64KB initial size
|
||||||
|
return *global_arena;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function-local statics to avoid static initialization order fiasco
|
// Function-local statics to avoid static initialization order fiasco
|
||||||
static auto &get_counter_families() {
|
static auto &get_counter_families() {
|
||||||
using FamilyMap = std::map<
|
using FamilyMap =
|
||||||
std::string_view, Family<Counter>::State *, std::less<std::string_view>,
|
std::map<std::string_view, ArenaAllocator::Ptr<Family<Counter>::State>,
|
||||||
|
std::less<std::string_view>,
|
||||||
ArenaStlAllocator<
|
ArenaStlAllocator<
|
||||||
std::pair<const std::string_view, Family<Counter>::State *>>>;
|
std::pair<const std::string_view,
|
||||||
static FamilyMap *counterFamilies = new FamilyMap(
|
ArenaAllocator::Ptr<Family<Counter>::State>>>>;
|
||||||
ArenaStlAllocator<
|
static FamilyMap *counterFamilies =
|
||||||
std::pair<const std::string_view, Family<Counter>::State *>>(
|
new FamilyMap(ArenaStlAllocator<
|
||||||
|
std::pair<const std::string_view,
|
||||||
|
ArenaAllocator::Ptr<Family<Counter>::State>>>(
|
||||||
&get_global_arena()));
|
&get_global_arena()));
|
||||||
return *counterFamilies;
|
return *counterFamilies;
|
||||||
}
|
}
|
||||||
@@ -360,13 +364,16 @@ struct Metric {
|
|||||||
|
|
||||||
static auto &get_histogram_families() {
|
static auto &get_histogram_families() {
|
||||||
using FamilyMap =
|
using FamilyMap =
|
||||||
std::map<std::string_view, Family<Histogram>::State *,
|
std::map<std::string_view,
|
||||||
|
ArenaAllocator::Ptr<Family<Histogram>::State>,
|
||||||
std::less<std::string_view>,
|
std::less<std::string_view>,
|
||||||
ArenaStlAllocator<std::pair<const std::string_view,
|
|
||||||
Family<Histogram>::State *>>>;
|
|
||||||
static FamilyMap *histogramFamilies = new FamilyMap(
|
|
||||||
ArenaStlAllocator<
|
ArenaStlAllocator<
|
||||||
std::pair<const std::string_view, Family<Histogram>::State *>>(
|
std::pair<const std::string_view,
|
||||||
|
ArenaAllocator::Ptr<Family<Histogram>::State>>>>;
|
||||||
|
static FamilyMap *histogramFamilies =
|
||||||
|
new FamilyMap(ArenaStlAllocator<
|
||||||
|
std::pair<const std::string_view,
|
||||||
|
ArenaAllocator::Ptr<Family<Histogram>::State>>>(
|
||||||
&get_global_arena()));
|
&get_global_arena()));
|
||||||
return *histogramFamilies;
|
return *histogramFamilies;
|
||||||
}
|
}
|
||||||
@@ -872,11 +879,7 @@ Family<Counter> create_counter(std::string_view name, std::string_view help) {
|
|||||||
auto name_view = arena_copy_string(name, global_arena);
|
auto name_view = arena_copy_string(name, global_arena);
|
||||||
auto &familyPtr = Metric::get_counter_families()[name_view];
|
auto &familyPtr = Metric::get_counter_families()[name_view];
|
||||||
if (!familyPtr) {
|
if (!familyPtr) {
|
||||||
// NOTE: Family<T>::State instances are never destroyed - this is fine
|
familyPtr = global_arena.construct<Family<Counter>::State>(global_arena);
|
||||||
// because the number of metric families is bounded by application design
|
|
||||||
familyPtr = new (global_arena.allocate_raw(sizeof(Family<Counter>::State),
|
|
||||||
alignof(Family<Counter>::State)))
|
|
||||||
Family<Counter>::State(global_arena);
|
|
||||||
familyPtr->name = name_view;
|
familyPtr->name = name_view;
|
||||||
familyPtr->help = arena_copy_string(help, global_arena);
|
familyPtr->help = arena_copy_string(help, global_arena);
|
||||||
} else {
|
} else {
|
||||||
@@ -885,7 +888,7 @@ Family<Counter> create_counter(std::string_view name, std::string_view help) {
|
|||||||
"metric family already registered with different help text", name);
|
"metric family already registered with different help text", name);
|
||||||
}
|
}
|
||||||
Family<Counter> family;
|
Family<Counter> family;
|
||||||
family.p = familyPtr;
|
family.p = familyPtr.get();
|
||||||
return family;
|
return family;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -925,9 +928,7 @@ Family<Histogram> create_histogram(std::string_view name, std::string_view help,
|
|||||||
if (!family_ptr) {
|
if (!family_ptr) {
|
||||||
// NOTE: Family<T>::State instances are never destroyed - this is fine
|
// NOTE: Family<T>::State instances are never destroyed - this is fine
|
||||||
// because the number of metric families is bounded by application design
|
// because the number of metric families is bounded by application design
|
||||||
family_ptr = new (global_arena.allocate_raw(
|
family_ptr = global_arena.construct<Family<Histogram>::State>(global_arena);
|
||||||
sizeof(Family<Histogram>::State), alignof(Family<Histogram>::State)))
|
|
||||||
Family<Histogram>::State(global_arena);
|
|
||||||
family_ptr->name = name_view;
|
family_ptr->name = name_view;
|
||||||
family_ptr->help = arena_copy_string(help, global_arena);
|
family_ptr->help = arena_copy_string(help, global_arena);
|
||||||
|
|
||||||
@@ -971,7 +972,7 @@ Family<Histogram> create_histogram(std::string_view name, std::string_view help,
|
|||||||
name);
|
name);
|
||||||
}
|
}
|
||||||
Family<Histogram> family;
|
Family<Histogram> family;
|
||||||
family.p = family_ptr;
|
family.p = family_ptr.get();
|
||||||
return family;
|
return family;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user