From 3bcc653ce1cd11e24c23f1a7dc6e57c0fc224410 Mon Sep 17 00:00:00 2001 From: Aurelien DARRAGON Date: Mon, 25 Nov 2024 15:29:40 +0100 Subject: [PATCH] MINOR: hlua_fcn: add Patref:giveup() If Patref:commit() was used and the new version (generation) isn't going to be committed, calling Patref:giveup() will allow allocated resources to be freed and reused. It is a good habit to call this if commit() isn't called after a prepare(). --- doc/lua-api/index.rst | 7 ++++++- src/hlua_fcn.c | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst index db594c5cc..b2bda2da1 100644 --- a/doc/lua-api/index.rst +++ b/doc/lua-api/index.rst @@ -3478,7 +3478,12 @@ Patref class :returns: true on success and nil on failure (followed by an error message). - See :js:func:`Patref.prepare()` + See :js:func:`Patref.prepare()` and :js:func:`Patref.giveup()` + +.. js:function:: Patref.giveup(ref) + + Drop the pending patref version created using Patref:prepare(): get back to + live dataset. .. _applethttp_class: diff --git a/src/hlua_fcn.c b/src/hlua_fcn.c index 49219e26a..d44d63733 100644 --- a/src/hlua_fcn.c +++ b/src/hlua_fcn.c @@ -2694,6 +2694,28 @@ int hlua_patref_commit(lua_State *L) return _hlua_patref_clear(L, LUA_OK, 0); } +int hlua_patref_giveup(lua_State *L) +{ + struct hlua_patref *ref; + + ref = hlua_checkudata(L, 1, class_patref_ref); + BUG_ON(!ref); + + if (!(ref->flags & HLUA_PATREF_FL_GEN)) { + /* nothing to do */ + return 0; + } + + lua_pushinteger(L, ref->curr_gen); // from + lua_pushinteger(L, ref->curr_gen); // to + _hlua_patref_clear(L, LUA_OK, 0); + + /* didn't make use of the generation ID, give it back to the API */ + pat_ref_giveup(ref->ptr, ref->curr_gen); + + return 0; +} + int hlua_patref_prepare(lua_State *L) { struct hlua_patref *ref; @@ -2746,6 +2768,7 @@ void hlua_fcn_new_patref(lua_State *L, struct pat_ref *ref) hlua_class_function(L, "is_map", hlua_patref_is_map); hlua_class_function(L, "prepare", hlua_patref_prepare); hlua_class_function(L, "commit", hlua_patref_commit); + hlua_class_function(L, "giveup", hlua_patref_giveup); hlua_class_function(L, "purge", hlua_patref_purge); }