From cfac40fbb247b1bbbb8cfa6e88c102784562fe8f Mon Sep 17 00:00:00 2001
From: Maksim Tiushev <mandesero@gmail.com>
Date: Tue, 23 Jul 2024 12:36:08 +0000
Subject: [PATCH] refactor: use Lua C API instead of G(L)

To ensure better encapsulation, maintainability, and portability of
the code, it is necessary to replace direct access to the fields of
global structures with calls using the Lua C API.

Closes #10284

NO_DOC=refactoring
NO_TEST=refactoring
NO_CHANGELOG=refactoring

(cherry picked from commit f7bb3fc7a1222107753dc420675ecda043b0a5d2)
---
 src/box/lua/info.c  | 2 +-
 src/box/lua/slab.cc | 2 +-
 src/lua/utils.c     | 6 ++++++
 src/lua/utils.h     | 7 +++++++
 4 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/box/lua/info.c b/src/box/lua/info.c
index 96253734bf..19fa752f05 100644
--- a/src/box/lua/info.c
+++ b/src/box/lua/info.c
@@ -411,7 +411,7 @@ lbox_info_memory_call(struct lua_State *L)
 	lua_settable(L, -3);
 
 	lua_pushstring(L, "lua");
-	lua_pushinteger(L, G(L)->gc.total);
+	lua_pushinteger(L, luaL_getgctotal(L));
 	lua_settable(L, -3);
 
 	return 1;
diff --git a/src/box/lua/slab.cc b/src/box/lua/slab.cc
index 309d2b1f55..e128b0ade0 100644
--- a/src/box/lua/slab.cc
+++ b/src/box/lua/slab.cc
@@ -259,7 +259,7 @@ lbox_runtime_info(struct lua_State *L)
 	 * Lua GC heap size
 	 */
 	lua_pushstring(L, "lua");
-	lua_pushinteger(L, G(L)->gc.total);
+	lua_pushinteger(L, luaL_getgctotal(L));
 	lua_settable(L, -3);
 
 	luaL_pushuint64(L, tuple_runtime_memory_used());
diff --git a/src/lua/utils.c b/src/lua/utils.c
index cccde3a912..74d8544714 100644
--- a/src/lua/utils.c
+++ b/src/lua/utils.c
@@ -337,6 +337,12 @@ luaL_setcdatagc(struct lua_State *L, int idx)
 	lua_pop(L, 1);
 }
 
+size_t
+luaL_getgctotal(struct lua_State *L)
+{
+	return (lua_getgccount(L) * 1024ULL) + lua_gc(L, LUA_GCCOUNTB, 0);
+}
+
 /**
  * A helper to register a single type metatable.
  */
diff --git a/src/lua/utils.h b/src/lua/utils.h
index c07ab10318..9fb97002cb 100644
--- a/src/lua/utils.h
+++ b/src/lua/utils.h
@@ -201,6 +201,13 @@ luaL_checkcdata(struct lua_State *L, int idx, uint32_t *ctypeid);
 LUA_API void
 luaL_setcdatagc(struct lua_State *L, int idx);
 
+/**
+ * @brief Return size of currently allocated memory.
+ * @param L Lua State
+ */
+size_t
+luaL_getgctotal(struct lua_State *L);
+
 /**
 * @brief Return CTypeID (FFI) of given СDATA type
 * @param L Lua State
-- 
GitLab