eliminate cs_vector/cs_map, use our allocator for all state

master
Daniel Kolesa 2021-03-19 23:05:19 +01:00
parent ac1af69d96
commit 70d7e15ed5
2 changed files with 25 additions and 15 deletions

View File

@ -9,12 +9,6 @@
namespace cscript {
template<typename K, typename V>
using cs_map = std::unordered_map<K, V>;
template<typename T>
using cs_vector = std::vector<T>;
cs_int cs_parse_int(
ostd::string_range input, ostd::string_range *end = nullptr
);
@ -131,12 +125,32 @@ struct cs_charbuf: cs_valbuf<char> {
};
struct cs_shared_state {
cs_map<ostd::string_range, cs_ident *> idents;
cs_vector<cs_ident *> identmap;
using allocator_type = cs_allocator<
std::pair<ostd::string_range const, cs_ident *>
>;
cs_alloc_cb allocf;
void *aptr;
std::unordered_map<
ostd::string_range, cs_ident *,
std::hash<ostd::string_range>,
std::equal_to<ostd::string_range>,
allocator_type
> idents;
std::vector<cs_ident *, cs_allocator<cs_ident *>> 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<cs_strman>(this)}
{}
void *alloc(void *ptr, size_t os, size_t ns) {
return allocf(aptr, ptr, os, ns);

View File

@ -256,13 +256,9 @@ cs_state::cs_state(cs_alloc_cb func, void *data):
p_state = static_cast<cs_shared_state *>(
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<cs_strman>(p_state);
p_state->varprintf = [](auto &, auto &) {};
for (int i = 0; i < MaxArguments; ++i) {
char buf[32];