From 70d7e15ed5cd4d247526b01124607b03dc6e1883 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 19 Mar 2021 23:05:19 +0100 Subject: [PATCH] eliminate cs_vector/cs_map, use our allocator for all state --- src/cs_util.hh | 32 +++++++++++++++++++++++--------- src/cubescript.cc | 8 ++------ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/cs_util.hh b/src/cs_util.hh index 2a63a6d..a21b2aa 100644 --- a/src/cs_util.hh +++ b/src/cs_util.hh @@ -9,12 +9,6 @@ namespace cscript { -template -using cs_map = std::unordered_map; - -template -using cs_vector = std::vector; - cs_int cs_parse_int( ostd::string_range input, ostd::string_range *end = nullptr ); @@ -131,12 +125,32 @@ struct cs_charbuf: cs_valbuf { }; struct cs_shared_state { - cs_map idents; - cs_vector identmap; + using allocator_type = cs_allocator< + std::pair + >; cs_alloc_cb allocf; + void *aptr; + + std::unordered_map< + ostd::string_range, cs_ident *, + std::hash, + std::equal_to, + allocator_type + > idents; + std::vector> identmap; + cs_vprint_cb varprintf; cs_strman *strman; - void *aptr; + + cs_shared_state() = delete; + + cs_shared_state(cs_alloc_cb af, void *data): + allocf{af}, aptr{data}, + idents{allocator_type{this}}, + identmap{allocator_type{this}}, + varprintf{[](auto &, auto &) {}}, + strman{create(this)} + {} void *alloc(void *ptr, size_t os, size_t ns) { return allocf(aptr, ptr, os, ns); diff --git a/src/cubescript.cc b/src/cubescript.cc index a6c6372..431845d 100644 --- a/src/cubescript.cc +++ b/src/cubescript.cc @@ -256,13 +256,9 @@ cs_state::cs_state(cs_alloc_cb func, void *data): p_state = static_cast( func(data, nullptr, 0, sizeof(cs_shared_state)) ); - new (p_state) cs_shared_state(); + /* allocator will be set up in the constructor */ + new (p_state) cs_shared_state{func, data}; p_owner = true; - /* set up allocator, from now we can call into alloc() */ - p_state->allocf = func; - p_state->aptr = data; - p_state->strman = p_state->create(p_state); - p_state->varprintf = [](auto &, auto &) {}; for (int i = 0; i < MaxArguments; ++i) { char buf[32];