Merge pull request #15811 from s-hadinger/berry_comobj_dealloc

Berry comobj standard deallocator
This commit is contained in:
s-hadinger 2022-06-16 22:48:14 +02:00 committed by GitHub
commit b6409a6a6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 34 deletions

View File

@ -71,3 +71,14 @@ void be_commonobj_delete(bvm *vm, bgcobject *obj)
be_free(vm, co, sizeof(bcommomobj));
}
}
/* generic destroy method for comobj, just call be_os_free() on the pointer */
int be_commonobj_destroy_generic(bvm* vm)
{
int argc = be_top(vm);
if (argc > 0) {
void * obj = be_tocomptr(vm, 1);
if (obj != NULL) { be_os_free(obj); }
}
be_return_nil(vm);
}

View File

@ -259,5 +259,6 @@ typedef const char* (*breader)(void*, size_t*);
const char* be_vtype2str(bvalue *v);
bvalue* be_indexof(bvm *vm, int idx);
void be_commonobj_delete(bvm *vm, bgcobject *obj);
int be_commonobj_destroy_generic(bvm* vm);
#endif

View File

@ -10,18 +10,6 @@
#include "be_exec.h"
#include "esp_rom_md5.h"
int free_ctx(bvm* vm) {
int argc = be_top(vm);
if (argc > 0) {
be_getmember(vm, 1, ".p");
md5_context_t * ctx = (md5_context_t *) be_tocomptr(vm, -1);
if (ctx != NULL) {
be_os_free(ctx);
}
}
be_return_nil(vm);
}
// `Md5.init() -> `
int32_t m_md5_init(struct bvm *vm);
int32_t m_md5_init(struct bvm *vm) {
@ -32,7 +20,7 @@ int32_t m_md5_init(struct bvm *vm) {
}
esp_rom_md5_init(ctx);
be_newcomobj(vm, ctx, &free_ctx);
be_newcomobj(vm, ctx, &be_commonobj_destroy_generic);
be_setmember(vm, 1, ".p");
be_return_nil(vm);
}

View File

@ -7,6 +7,7 @@
*******************************************************************/
#include "be_constobj.h"
#include "be_mem.h"
#include "be_object.h"
#include "re1.5.h"
/********************************************************************
@ -33,15 +34,6 @@ re.split = def (regex_str, str) end # native
extern const bclass be_class_re_pattern;
int be_free_comobj(bvm* vm) {
int argc = be_top(vm);
if (argc > 0) {
void * obj = be_tocomptr(vm, 1);
if (obj != NULL) { be_os_free(obj); }
}
be_return_nil(vm);
}
// Native functions be_const_func()
// Berry: `re.compile(pattern:string) -> instance(be_pattern)`
int be_re_compile(bvm *vm) {
@ -60,7 +52,7 @@ int be_re_compile(bvm *vm) {
}
be_pushntvclass(vm, &be_class_re_pattern);
be_call(vm, 0);
be_newcomobj(vm, code, &be_free_comobj);
be_newcomobj(vm, code, &be_commonobj_destroy_generic);
be_setmember(vm, -2, "_p");
be_pop(vm, 1);
be_return(vm);

View File

@ -23,6 +23,7 @@
#include <berry.h>
#include "be_mem.h"
#include "be_object.h"
/*********************************************************************************************\
* AES class
@ -30,15 +31,6 @@
\*********************************************************************************************/
extern "C" {
int free_br_obj(bvm* vm) {
int argc = be_top(vm);
if (argc > 0) {
void * obj = be_tocomptr(vm, 1);
if (obj != NULL) { be_os_free(obj); }
}
be_return_nil(vm);
}
// `AES_GCM.init(secret_key:bytes(32), iv:bytes(12)) -> instance`
int32_t m_aes_gcm_init(struct bvm *vm);
int32_t m_aes_gcm_init(struct bvm *vm) {
@ -64,14 +56,14 @@ extern "C" {
br_aes_small_ctr_keys * ctr_ctx = (br_aes_small_ctr_keys *) be_os_malloc(sizeof(br_aes_small_ctr_keys));
if (!ctr_ctx) { be_throw(vm, BE_MALLOC_FAIL); }
br_aes_small_ctr_init(ctr_ctx, bytes, length);
be_newcomobj(vm, ctr_ctx, &free_br_obj);
be_newcomobj(vm, ctr_ctx, &be_commonobj_destroy_generic);
be_setmember(vm, 1, ".p1");
// Initialize an AES GCM structure based on this CTR engine
br_gcm_context * gcm_ctx = (br_gcm_context *) be_os_malloc(sizeof(br_gcm_context));
if (!gcm_ctx) { be_throw(vm, BE_MALLOC_FAIL); }
br_gcm_init(gcm_ctx, &ctr_ctx->vtable, &br_ghash_ctmul32);
be_newcomobj(vm, gcm_ctx, &free_br_obj);
be_newcomobj(vm, gcm_ctx, &be_commonobj_destroy_generic);
be_setmember(vm, 1, ".p2");
// Reset GCM context with provided IV