From dd364c92dade440baa998426004f782e200862f9 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Thu, 16 Jun 2022 22:37:33 +0200 Subject: [PATCH] Berry comobj standard deallocator --- lib/libesp32/berry/src/be_object.c | 11 +++++++++++ lib/libesp32/berry/src/be_object.h | 1 + lib/libesp32/berry_tasmota/src/be_md5_lib.c | 14 +------------- lib/libesp32/berry_tasmota/src/be_re_lib.c | 12 ++---------- .../tasmota_xdrv_driver/xdrv_52_3_berry_crypto.ino | 14 +++----------- 5 files changed, 18 insertions(+), 34 deletions(-) diff --git a/lib/libesp32/berry/src/be_object.c b/lib/libesp32/berry/src/be_object.c index 606d2bb0f..aa2f71799 100644 --- a/lib/libesp32/berry/src/be_object.c +++ b/lib/libesp32/berry/src/be_object.c @@ -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); +} diff --git a/lib/libesp32/berry/src/be_object.h b/lib/libesp32/berry/src/be_object.h index 13d4be006..dbde8317f 100644 --- a/lib/libesp32/berry/src/be_object.h +++ b/lib/libesp32/berry/src/be_object.h @@ -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 diff --git a/lib/libesp32/berry_tasmota/src/be_md5_lib.c b/lib/libesp32/berry_tasmota/src/be_md5_lib.c index a4bcb23f4..3b73940ce 100644 --- a/lib/libesp32/berry_tasmota/src/be_md5_lib.c +++ b/lib/libesp32/berry_tasmota/src/be_md5_lib.c @@ -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); } diff --git a/lib/libesp32/berry_tasmota/src/be_re_lib.c b/lib/libesp32/berry_tasmota/src/be_re_lib.c index bc28c0e67..971814a2d 100644 --- a/lib/libesp32/berry_tasmota/src/be_re_lib.c +++ b/lib/libesp32/berry_tasmota/src/be_re_lib.c @@ -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); diff --git a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_crypto.ino b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_crypto.ino index e80f585ce..f89ed23c6 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_crypto.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_crypto.ino @@ -23,6 +23,7 @@ #include #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