Change save/restore API to have explicit alloc/free functions. Python has explicit save/update functions.

This commit is contained in:
Andrew Dutcher
2016-10-07 11:39:42 -07:00
parent 6c54f9ce48
commit 1ab2154fe5
4 changed files with 117 additions and 39 deletions

47
uc.c
View File

@@ -1174,20 +1174,47 @@ size_t cpu_regs_size(uc_arch arch, uc_mode mode)
}
UNICORN_EXPORT
void *uc_context_save(uc_engine *uc, void *buffer)
uc_err uc_context_alloc(uc_engine *uc, uc_context **context)
{
size_t sz = cpu_regs_size(uc->arch, uc->mode);
if (!buffer) {
buffer = malloc(sz);
size_t size = cpu_regs_size(uc->arch, uc->mode);
*context = malloc(size + sizeof(uc_context));
if (*context) {
(*context)->size = size;
(*context)->arch = uc->arch;
(*context)->mode = uc->mode;
(*context)->used = false;
return UC_ERR_OK;
} else {
return UC_ERR_NOMEM;
}
memcpy(buffer, uc->cpu->env_ptr, sz);
return buffer;
}
UNICORN_EXPORT
void uc_context_restore(uc_engine *uc, void *buffer)
uc_err uc_context_free(uc_context *context)
{
size_t sz = cpu_regs_size(uc->arch, uc->mode);
memcpy(uc->cpu->env_ptr, buffer, sz);
free(context);
return UC_ERR_OK;
}
UNICORN_EXPORT
uc_err uc_context_save(uc_engine *uc, uc_context *context)
{
if (context->arch != uc->arch || context->mode != uc->mode) {
return UC_ERR_ARG;
} else {
memcpy(context->data, uc->cpu->env_ptr, context->size);
context->used = true;
return UC_ERR_OK;
}
}
UNICORN_EXPORT
uc_err uc_context_restore(uc_engine *uc, uc_context *context)
{
if (context->arch != uc->arch || context->mode != uc->mode || !context->used) {
return UC_ERR_ARG;
} else {
memcpy(uc->cpu->env_ptr, context->data, context->size);
return UC_ERR_OK;
}
}