move allocator into shared state
parent
21c5a38e17
commit
4353bb7904
|
@ -363,6 +363,14 @@ enum class CsLoopState {
|
||||||
Normal = 0, Break, Continue
|
Normal = 0, Break, Continue
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static inline void *cs_default_alloc(void *, void *p, ostd::Size, ostd::Size ns) {
|
||||||
|
if (!ns) {
|
||||||
|
delete[] static_cast<unsigned char *>(p);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return new unsigned char[ns];
|
||||||
|
}
|
||||||
|
|
||||||
struct OSTD_EXPORT CsState {
|
struct OSTD_EXPORT CsState {
|
||||||
friend struct CsErrorException;
|
friend struct CsErrorException;
|
||||||
friend struct GenState;
|
friend struct GenState;
|
||||||
|
@ -372,7 +380,7 @@ struct OSTD_EXPORT CsState {
|
||||||
|
|
||||||
int identflags = 0;
|
int identflags = 0;
|
||||||
|
|
||||||
CsState(CsAllocCb func = nullptr, void *data = nullptr);
|
CsState(CsAllocCb func = cs_default_alloc, void *data = nullptr);
|
||||||
virtual ~CsState();
|
virtual ~CsState();
|
||||||
|
|
||||||
CsHookCb set_call_hook(CsHookCb func);
|
CsHookCb set_call_hook(CsHookCb func);
|
||||||
|
@ -563,9 +571,6 @@ private:
|
||||||
GenState *p_pstate = nullptr;
|
GenState *p_pstate = nullptr;
|
||||||
int p_inloop = 0;
|
int p_inloop = 0;
|
||||||
|
|
||||||
CsAllocCb p_allocf;
|
|
||||||
void *p_aptr;
|
|
||||||
|
|
||||||
char p_errbuf[512];
|
char p_errbuf[512];
|
||||||
|
|
||||||
CsHookCb p_callhook;
|
CsHookCb p_callhook;
|
||||||
|
|
|
@ -97,6 +97,8 @@ enum {
|
||||||
struct CsSharedState {
|
struct CsSharedState {
|
||||||
CsMap<ostd::ConstCharRange, CsIdent *> idents;
|
CsMap<ostd::ConstCharRange, CsIdent *> idents;
|
||||||
CsVector<CsIdent *> identmap;
|
CsVector<CsIdent *> identmap;
|
||||||
|
CsAllocCb allocf;
|
||||||
|
void *aptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct CsBreakException {
|
struct CsBreakException {
|
||||||
|
|
|
@ -287,10 +287,20 @@ int CsCommand::get_num_args() const {
|
||||||
void cs_init_lib_base(CsState &cs);
|
void cs_init_lib_base(CsState &cs);
|
||||||
|
|
||||||
CsState::CsState(CsAllocCb func, void *data):
|
CsState::CsState(CsAllocCb func, void *data):
|
||||||
p_state(nullptr),
|
p_state(nullptr), p_callhook()
|
||||||
p_allocf(func), p_aptr(data), p_callhook()
|
|
||||||
{
|
{
|
||||||
p_state = create<CsSharedState>();
|
if (!func) {
|
||||||
|
func = cs_default_alloc;
|
||||||
|
}
|
||||||
|
/* allocator is not set up yet, use func directly */
|
||||||
|
p_state = static_cast<CsSharedState *>(
|
||||||
|
func(data, nullptr, 0, sizeof(CsSharedState))
|
||||||
|
);
|
||||||
|
new (p_state) CsSharedState();
|
||||||
|
/* set up allocator, from now we can call into alloc() */
|
||||||
|
p_state->allocf = func;
|
||||||
|
p_state->aptr = data;
|
||||||
|
|
||||||
for (int i = 0; i < MaxArguments; ++i) {
|
for (int i = 0; i < MaxArguments; ++i) {
|
||||||
char buf[32];
|
char buf[32];
|
||||||
snprintf(buf, sizeof(buf), "arg%d", i + 1);
|
snprintf(buf, sizeof(buf), "arg%d", i + 1);
|
||||||
|
@ -415,14 +425,7 @@ CsHookCb &CsState::get_call_hook() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void *CsState::alloc(void *ptr, ostd::Size os, ostd::Size ns) {
|
void *CsState::alloc(void *ptr, ostd::Size os, ostd::Size ns) {
|
||||||
if (p_allocf) {
|
return p_state->allocf(p_state->aptr, ptr, os, ns);
|
||||||
return p_allocf(p_aptr, ptr, os, ns);
|
|
||||||
}
|
|
||||||
if (!ns) {
|
|
||||||
delete[] static_cast<unsigned char *>(ptr);
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
return new unsigned char[ns];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CsState::clear_override(CsIdent &id) {
|
void CsState::clear_override(CsIdent &id) {
|
||||||
|
|
Loading…
Reference in New Issue