diff options
| author | ruki <[email protected]> | 2021-09-20 21:08:57 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-09-20 21:08:57 +0800 |
| commit | 07cabd568b07120e227e29a945bc45dcc05a0db2 (patch) | |
| tree | 2f685119d2c0661296c19d0c40970f139da76d19 | |
| parent | d6cf1e9c4b73507fb2bcfa0d173ebb62a28faa64 (diff) | |
add libc.memset
| -rw-r--r-- | core/src/xmake/engine.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/libc/memset.c | 53 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 1 | ||||
| -rw-r--r-- | xmake/core/base/libc.lua | 9 |
4 files changed, 65 insertions, 0 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 2aa8f17cd..63196680d 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -225,6 +225,7 @@ tb_int_t xm_semver_select(lua_State* lua); tb_int_t xm_libc_malloc(lua_State* lua); tb_int_t xm_libc_free(lua_State* lua); tb_int_t xm_libc_memcpy(lua_State* lua); +tb_int_t xm_libc_memset(lua_State* lua); #ifdef XM_CONFIG_API_HAVE_CURSES // register curses @@ -423,6 +424,7 @@ static luaL_Reg const g_libc_functions[] = { "malloc", xm_libc_malloc } , { "free", xm_libc_free } , { "memcpy", xm_libc_memcpy } +, { "memset", xm_libc_memset } , { tb_null, tb_null } }; diff --git a/core/src/xmake/libc/memset.c b/core/src/xmake/libc/memset.c new file mode 100644 index 000000000..c3171bace --- /dev/null +++ b/core/src/xmake/libc/memset.c @@ -0,0 +1,53 @@ +/*!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 memset.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "memset" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_libc_memset(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // check arguments? + if (!xm_lua_ispointer(lua, 1) || !lua_isnumber(lua, 2) || !lua_isnumber(lua, 3)) + xm_libc_return_error(lua, "memset(invalid args)!"); + + // do memset + tb_pointer_t data = (tb_pointer_t)xm_lua_topointer(lua, 1); + tb_char_t ch = (tb_char_t)lua_tointeger(lua, 2); + tb_int_t size = (tb_int_t)lua_tointeger(lua, 3); + if (data && size > 0) + tb_memset(data, ch, size); + return 0; +} + diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 2c5eefa01..84b6a03c5 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -118,6 +118,7 @@ xmake_C_FILES += \ readline/clear_history \ libc/malloc \ libc/free \ + libc/memset \ libc/memcpy iswin = diff --git a/xmake/core/base/libc.lua b/xmake/core/base/libc.lua index 4b3a9f3ec..ff6a88401 100644 --- a/xmake/core/base/libc.lua +++ b/xmake/core/base/libc.lua @@ -25,6 +25,7 @@ local libc = libc or {} libc._malloc = libc._malloc or libc.malloc libc._free = libc._free or libc.free libc._memcpy = libc._memcpy or libc.memcpy +libc._memset = libc._memset or libc.memset -- load modules local ffi = xmake._LUAJIT and require("ffi") @@ -61,5 +62,13 @@ function libc.memcpy(dst, src, size) end end +function libc.memset(data, ch, size) + if ffi then + return ffi.fill(data, size, ch) + else + return libc._memset(data, ch, size) + end +end + -- return module: libc return libc |
