From b27f8dee91f4e1eb87e5634c5d493ab2fee07e80 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 19 Mar 2021 03:10:02 +0100 Subject: [PATCH] use state's allocator to manage cs_strman resources --- src/cs_util.hh | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/cs_util.hh b/src/cs_util.hh index e444bc8..70de1dd 100644 --- a/src/cs_util.hh +++ b/src/cs_util.hh @@ -87,6 +87,11 @@ struct cs_shared_state { struct allocator { using value_type = T; + allocator(cs_shared_state *s): state{s} {} + + template + allocator(allocator const &a): state{a.state} {}; + T *allocate(std::size_t n) { return static_cast(state->alloc(nullptr, 0, n * sizeof(T))); } @@ -129,8 +134,11 @@ struct cs_strref_state { }; struct cs_strman { + using allocator_type = cs_shared_state::allocator< + std::pair + >; cs_strman() = delete; - cs_strman(cs_shared_state *cs): cstate{cs} {} + cs_strman(cs_shared_state *cs): cstate{cs}, counts{allocator_type{cs}} {} ~cs_strman() {} cs_strman(cs_strman const &) = delete; @@ -175,8 +183,12 @@ struct cs_strman { char *alloc_buf(std::size_t len) const; cs_shared_state *cstate; - /* FIXME: use main allocator */ - std::unordered_map counts{}; + std::unordered_map< + ostd::string_range, cs_strref_state *, + std::hash, + std::equal_to, + allocator_type + > counts; }; struct cs_charbuf {