summaryrefslogtreecommitdiff
path: root/xmake/core/base
diff options
context:
space:
mode:
authorruki <[email protected]>2021-09-21 00:25:24 +0800
committerruki <[email protected]>2021-09-21 00:25:24 +0800
commit1e14751dcb53640e9592be678e3ebf6a46d611e1 (patch)
tree864f09358840610ac892790fa1e09f7d945b38f8 /xmake/core/base
parent07a9a02dbd44f4d58cdb704c6db23f5e62fd2ccf (diff)
improve bytes for lua
Diffstat (limited to 'xmake/core/base')
-rw-r--r--xmake/core/base/bytes.lua15
-rw-r--r--xmake/core/base/libc.lua33
2 files changed, 29 insertions, 19 deletions
diff --git a/xmake/core/base/bytes.lua b/xmake/core/base/bytes.lua
index 8b2fa08cc..55c08e774 100644
--- a/xmake/core/base/bytes.lua
+++ b/xmake/core/base/bytes.lua
@@ -24,7 +24,6 @@ local _instance = _instance or {}
-- load modules
local bit = require("base/bit")
-local ffi = xmake._LUAJIT and require("ffi") or nil
local os = require("base/os")
local utils = require("base/utils")
local todisplay = require("base/todisplay")
@@ -52,7 +51,7 @@ function _instance.new(...)
local ptr = arg2
local manage = arg3
if manage then
- instance._CDATA = ffi.gc(libc.dataptr(ptr), ffi.C.free)
+ instance._CDATA = libc.dataptr(ptr, {gc = true})
instance._MANAGED = true
else
instance._CDATA = libc.dataptr(ptr)
@@ -70,7 +69,7 @@ function _instance.new(...)
os.raise("invalid arguments #2 for bytes(size, ...), cdata, string, number or nil expected!")
end
end
- local ptr = libc.gcmalloc(size)
+ local ptr = libc.malloc(size, {gc = true})
if init then
libc.memset(ptr, init, size)
end
@@ -107,7 +106,7 @@ function _instance.new(...)
for _, b in ipairs(args) do
instance._SIZE = instance._SIZE + b:size()
end
- instance._CDATA = libc.gcmalloc(instance._SIZE)
+ instance._CDATA = libc.malloc(instance._SIZE, {gc = true})
local offset = 0
for _, b in ipairs(args) do
libc.memcpy(instance._CDATA + offset, b:cdata(), b:size())
@@ -122,7 +121,7 @@ function _instance.new(...)
for _, b in ipairs(args) do
instance._SIZE = instance._SIZE + b:size()
end
- instance._CDATA = libc.gcmalloc(instance._SIZE)
+ instance._CDATA = libc.malloc(instance._SIZE, {gc = true})
local offset = 0
for _, b in ipairs(args) do
libc.memcpy(instance._CDATA + offset, b._CDATA, b:size())
@@ -325,7 +324,7 @@ end
-- convert bytes to string
function _instance:str(i, j)
local offset = i and i - 1 or 0
- return ffi.string(self:cdata() + offset, (j or self:size()) - offset)
+ return libc.strndup(self:cdata() + offset, (j or self:size()) - offset)
end
-- get uint8 value
@@ -451,6 +450,10 @@ end
-- it's only called for lua runtime, because bytes is not userdata
function _instance:__gc()
print("gc")
+ if self._MANAGED and self._CDATA then
+ libc.free(self._CDATA)
+ self._CDATA = nil
+ end
end
-- new an bytes instance
diff --git a/xmake/core/base/libc.lua b/xmake/core/base/libc.lua
index 20caff02b..1954f0fb1 100644
--- a/xmake/core/base/libc.lua
+++ b/xmake/core/base/libc.lua
@@ -40,23 +40,18 @@ if ffi then
]]
end
-function libc.malloc(size)
+function libc.malloc(size, opt)
if ffi then
- return ffi.cast("unsigned char*", ffi.C.malloc(size))
+ if opt and opt.gc then
+ return ffi.gc(ffi.cast("unsigned char*", ffi.C.malloc(size)), ffi.C.free)
+ else
+ return ffi.cast("unsigned char*", ffi.C.malloc(size))
+ end
else
return 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)
@@ -81,9 +76,21 @@ function libc.memset(data, ch, size)
end
end
-function libc.dataptr(data)
+function libc.strndup(s, n)
+ if ffi then
+ return ffi.string(s, n)
+ else
+ return libc._strndup(s, n)
+ end
+end
+
+function libc.dataptr(data, opt)
if ffi then
- return ffi.cast("unsigned char*", data)
+ if opt and opt.gc then
+ return ffi.gc(ffi.cast("unsigned char*", data), ffi.C.free)
+ else
+ return ffi.cast("unsigned char*", data)
+ end
else
return libc._dataptr(data)
end