custom allocf support

master
Daniel Kolesa 2016-09-14 21:33:32 +02:00
parent e41c3c03a1
commit e7f3213588
2 changed files with 13 additions and 5 deletions

View File

@ -320,6 +320,7 @@ struct CsStackStateNode {
};
using CsHookCb = ostd::Function<void(CsState &)>;
using CsAllocCb = void *(*)(void *, void *, ostd::Size, ostd::Size);
template<typename T>
struct CsAllocator {
@ -364,7 +365,7 @@ struct OSTD_EXPORT CsState {
int identflags = 0;
CsState();
CsState(CsAllocCb func = nullptr, void *data = nullptr);
virtual ~CsState();
CsStream const &get_out() const;
@ -550,6 +551,8 @@ struct OSTD_EXPORT CsState {
private:
CsIdent *add_ident(CsIdent *id);
CsAllocCb p_allocf;
void *p_aptr;
char p_errbuf[512];
CsHookCb p_callhook;
CsStream *p_out, *p_err;

View File

@ -252,10 +252,12 @@ int CsCommand::get_num_args() const {
void cs_init_lib_base(CsState &cs);
CsState::CsState():
p_state(create<CsSharedState>()), p_callhook(),
CsState::CsState(CsAllocCb func, void *data):
p_state(nullptr),
p_allocf(func), p_aptr(data), p_callhook(),
p_out(&ostd::out), p_err(&ostd::err)
{
p_state = create<CsSharedState>();
for (int i = 0; i < MaxArguments; ++i) {
char buf[32];
snprintf(buf, sizeof(buf), "arg%d", i + 1);
@ -389,9 +391,12 @@ CsHookCb &CsState::get_call_hook() {
return p_callhook;
}
void *CsState::alloc(void *ptr, ostd::Size, ostd::Size ns) {
void *CsState::alloc(void *ptr, ostd::Size os, ostd::Size ns) {
if (p_allocf) {
return p_allocf(p_aptr, ptr, os, ns);
}
if (!ns) {
delete static_cast<unsigned char *>(ptr);
delete[] static_cast<unsigned char *>(ptr);
}
return new unsigned char[ns];
}