diff options
| author | ruki <[email protected]> | 2021-09-20 22:52:37 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-09-20 22:52:37 +0800 |
| commit | 68f4ab938564a43656236de3a0eb67f9222bc876 (patch) | |
| tree | e1df5be901cd0c63cdeaaf61218107a86d9e9d66 | |
| parent | 07cabd568b07120e227e29a945bc45dcc05a0db2 (diff) | |
improve bytes
| -rw-r--r-- | tests/modules/bytes/test.lua | 5 | ||||
| -rw-r--r-- | xmake/core/base/bytes.lua | 22 | ||||
| -rw-r--r-- | xmake/core/base/libc.lua | 9 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/debug.lua | 13 |
4 files changed, 35 insertions, 14 deletions
diff --git a/tests/modules/bytes/test.lua b/tests/modules/bytes/test.lua index aa29e3493..8d4504f2c 100644 --- a/tests/modules/bytes/test.lua +++ b/tests/modules/bytes/test.lua @@ -12,16 +12,19 @@ function test_ctor(t) t:are_equal(bytes(10):size(), 10) t:are_equal(bytes(bytes("123"), bytes("456"), bytes("789")):str(), "123456789") t:are_equal(bytes({bytes("123"), bytes("456"), bytes("789")}):str(), "123456789") + debug.collectgarbage() end function test_clone(t) t:are_equal(bytes(10):clone():size(), 10) t:are_equal(bytes("123456789"):clone():str(), "123456789") + debug.collectgarbage() end function test_slice(t) t:are_equal(bytes(10):slice(1, 2):size(), 2) t:are_equal(bytes("123456789"):slice(1, 4):str(), "1234") + debug.collectgarbage() end function test_index(t) @@ -34,9 +37,11 @@ function test_index(t) b[1] = string.byte('2') t:are_equal(b:str(), "223456789") t:will_raise(function() b[100] = string.byte('2') end) + debug.collectgarbage() end function test_concat(t) t:are_equal((bytes("123") .. bytes("456")):str(), "123456") t:are_equal(bytes(bytes("123"), bytes("456")):str(), "123456") + debug.collectgarbage() end diff --git a/xmake/core/base/bytes.lua b/xmake/core/base/bytes.lua index 359a67f36..ba44d3403 100644 --- a/xmake/core/base/bytes.lua +++ b/xmake/core/base/bytes.lua @@ -28,6 +28,7 @@ local ffi = xmake._LUAJIT and require("ffi") or nil local os = require("base/os") local utils = require("base/utils") local todisplay = require("base/todisplay") +local libc = require("base/libc") -- define ffi interfaces if ffi then @@ -77,11 +78,11 @@ function _instance.new(...) os.raise("invalid arguments #2 for bytes(size, ...), cdata, string, number or nil expected!") end end - local ptr = ffi.C.malloc(size) + local ptr = libc.gcmalloc(size) if init then - ffi.fill(ptr, size, init) + libc.memset(ptr, init, size) end - instance._CDATA = ffi.gc(ffi.cast("unsigned char*", ptr), ffi.C.free) + instance._CDATA = ptr instance._MANAGED = true end instance._SIZE = size @@ -114,10 +115,10 @@ function _instance.new(...) for _, b in ipairs(args) do instance._SIZE = instance._SIZE + b:size() end - instance._CDATA = ffi.gc(ffi.cast("unsigned char*", ffi.C.malloc(instance._SIZE)), ffi.C.free) + instance._CDATA = libc.gcmalloc(instance._SIZE) local offset = 0 for _, b in ipairs(args) do - ffi.copy(instance._CDATA + offset, b:cdata(), b:size()) + libc.memcpy(instance._CDATA + offset, b:cdata(), b:size()) offset = offset + b:size() end instance._MANAGED = true @@ -129,10 +130,10 @@ function _instance.new(...) for _, b in ipairs(args) do instance._SIZE = instance._SIZE + b:size() end - instance._CDATA = ffi.gc(ffi.cast("unsigned char*", ffi.C.malloc(instance._SIZE)), ffi.C.free) + instance._CDATA = libc.gcmalloc(instance._SIZE) local offset = 0 for _, b in ipairs(args) do - ffi.copy(instance._CDATA + offset, b._CDATA, b:size()) + libc.memcpy(instance._CDATA + offset, b._CDATA, b:size()) offset = offset + b:size() end instance._MANAGED = true @@ -218,7 +219,7 @@ function _instance:copy(src) if src:size() ~= self:size() then os.raise("%s: cannot copy bytes, src and dst must have same size(%d->%d)!", self, src:size(), self:size()) end - ffi.copy(self:cdata(), src:cdata(), self:size()) + libc.memcpy(self:cdata(), src:cdata(), self:size()) return self end @@ -455,6 +456,11 @@ function _instance:__todisplay() return "bytes${reset}(" .. todisplay(self:size()) .. ") <${color.dump.number}" .. table.concat(parts, " ") .. (self:size() > 8 and "${reset} ..>" or "${reset}>") end +-- it's only called for lua runtime, because bytes is not userdata +function _instance:__gc() + print("gc") +end + -- new an bytes instance function bytes.new(...) return _instance.new(...) diff --git a/xmake/core/base/libc.lua b/xmake/core/base/libc.lua index ff6a88401..cedabef1b 100644 --- a/xmake/core/base/libc.lua +++ b/xmake/core/base/libc.lua @@ -46,6 +46,15 @@ function libc.malloc(size) end end +function libc.gcmalloc(size) + if ffi then + return ffi.gc(ffi.cast("unsigned char*", ffi.C.malloc(size)), ffi.C.free) + else + -- @note we need free it in lua/__gc manually + return libc._malloc(size) + end +end + function libc.free(data) if ffi then return ffi.C.free(data) diff --git a/xmake/core/sandbox/modules/debug.lua b/xmake/core/sandbox/modules/debug.lua index a533720e7..2a3004537 100644 --- a/xmake/core/sandbox/modules/debug.lua +++ b/xmake/core/sandbox/modules/debug.lua @@ -24,11 +24,13 @@ local table = require("base/table") -- define module local sandbox_debug = sandbox_debug or table.join(debug) -sandbox_debug.rawget = rawget -sandbox_debug.rawset = rawset -sandbox_debug.rawequal = rawequal -sandbox_debug.rawlen = rawlen -sandbox_debug.require = require +sandbox_debug.rawget = rawget +sandbox_debug.rawset = rawset +sandbox_debug.rawequal = rawequal +sandbox_debug.rawlen = rawlen +sandbox_debug.require = require +sandbox_debug.collectgarbage = collectgarbage + function sandbox_debug.global(key) if key == nil then return _G @@ -36,6 +38,5 @@ function sandbox_debug.global(key) return _G[key] end - -- return module return sandbox_debug |
