diff options
| author | ruki <[email protected]> | 2021-09-20 21:01:12 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-09-20 21:01:12 +0800 |
| commit | c1933e6e8e7212f16443e99d66e7d656438da317 (patch) | |
| tree | b82357d2933cf64ae9f6309fda5bec4e7fdd3ca2 | |
| parent | 33b05eff154f9158a68b0808f4be0f6c88aa6361 (diff) | |
add libc.free
| -rw-r--r-- | core/src/xmake/engine.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/libc/free.c | 50 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 3 | ||||
| -rw-r--r-- | xmake/core/base/libc.lua | 9 |
4 files changed, 63 insertions, 1 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 0c52f6cf0..dcfdd428c 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -223,6 +223,7 @@ tb_int_t xm_semver_select(lua_State* lua); // the libc functions tb_int_t xm_libc_malloc(lua_State* lua); +tb_int_t xm_libc_free(lua_State* lua); #ifdef XM_CONFIG_API_HAVE_CURSES // register curses @@ -419,6 +420,7 @@ static luaL_Reg const g_semver_functions[] = static luaL_Reg const g_libc_functions[] = { { "malloc", xm_libc_malloc } +, { "free", xm_libc_free } , { tb_null, tb_null } }; diff --git a/core/src/xmake/libc/free.c b/core/src/xmake/libc/free.c new file mode 100644 index 000000000..b4d17f1d0 --- /dev/null +++ b/core/src/xmake/libc/free.c @@ -0,0 +1,50 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file free.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "free" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_libc_free(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // check arguments? + if (!xm_lua_ispointer(lua, 1)) + xm_libc_return_error(lua, "free(invalid data)!"); + + // do free + tb_pointer_t data = (tb_pointer_t)xm_lua_topointer(lua, 1); + if (data) tb_free(data); + return 0; +} + diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index a25bd0936..bd8046c67 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -116,7 +116,8 @@ xmake_C_FILES += \ readline/history_list \ readline/add_history \ readline/clear_history \ - libc/malloc + libc/malloc \ + libc/free iswin = ifeq ($(PLAT),windows) diff --git a/xmake/core/base/libc.lua b/xmake/core/base/libc.lua index bac224df3..914065f2e 100644 --- a/xmake/core/base/libc.lua +++ b/xmake/core/base/libc.lua @@ -23,6 +23,7 @@ local libc = libc or {} -- save original interfaces libc._malloc = libc._malloc or libc.malloc +libc._free = libc._free or libc.free -- load modules local ffi = xmake._LUAJIT and require("ffi") @@ -43,5 +44,13 @@ function libc.malloc(size) end end +function libc.free(data) + if ffi then + return ffi.C.free(data) + else + return libc._free(data) + end +end + -- return module: libc return libc |
