diff options
| author | ruki <[email protected]> | 2021-09-21 00:33:49 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-09-21 00:33:49 +0800 |
| commit | 6bd955fbacc03792d8b36dffa087967e9e056e31 (patch) | |
| tree | 91c05b8f4a7c047add5f7d61c323e47361a8aef7 /xmake/core | |
| parent | 1e14751dcb53640e9592be678e3ebf6a46d611e1 (diff) | |
fix strndup
Diffstat (limited to 'xmake/core')
| -rw-r--r-- | xmake/core/base/bytes.lua | 9 | ||||
| -rw-r--r-- | xmake/core/base/libc.lua | 24 |
2 files changed, 25 insertions, 8 deletions
diff --git a/xmake/core/base/bytes.lua b/xmake/core/base/bytes.lua index 55c08e774..f35f20790 100644 --- a/xmake/core/base/bytes.lua +++ b/xmake/core/base/bytes.lua @@ -96,7 +96,7 @@ function _instance.new(...) os.raise("incorrect bounds(%d-%d) for bytes(...)!", start, last) end instance._SIZE = last - start + 1 - instance._CDATA = b:cdata() - 1 + start + instance._CDATA = libc.diffptr(b:cdata(), -1 + start) instance._REF = b -- keep lua ref for GC instance._MANAGED = false instance._READONLY = b:readonly() @@ -109,7 +109,7 @@ function _instance.new(...) 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()) + libc.memcpy(libc.diffptr(instance._CDATA, offset), b:cdata(), b:size()) offset = offset + b:size() end instance._MANAGED = true @@ -124,7 +124,7 @@ function _instance.new(...) 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()) + libc.memcpy(libc.diffptr(instance._CDATA, offset), b._CDATA, b:size()) offset = offset + b:size() end instance._MANAGED = true @@ -324,7 +324,7 @@ end -- convert bytes to string function _instance:str(i, j) local offset = i and i - 1 or 0 - return libc.strndup(self:cdata() + offset, (j or self:size()) - offset) + return libc.strndup(libc.diffptr(self:cdata(), offset), (j or self:size()) - offset) end -- get uint8 value @@ -449,7 +449,6 @@ 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 diff --git a/xmake/core/base/libc.lua b/xmake/core/base/libc.lua index 1954f0fb1..18c14a669 100644 --- a/xmake/core/base/libc.lua +++ b/xmake/core/base/libc.lua @@ -26,8 +26,10 @@ 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 +libc._strndup = libc._strndup or libc.strndup libc._dataptr = libc._dataptr or libc.dataptr libc._ptraddr = libc._ptraddr or libc.ptraddr +libc._diffptr = libc._diffptr or libc.diffptr -- load modules local ffi = xmake._LUAJIT and require("ffi") @@ -48,7 +50,11 @@ function libc.malloc(size, opt) return ffi.cast("unsigned char*", ffi.C.malloc(size)) end else - return libc._malloc(size) + local data, errors = libc._malloc(size) + if not data then + os.raise(errors) + end + return data end end @@ -72,7 +78,7 @@ function libc.memset(data, ch, size) if ffi then return ffi.fill(data, size, ch) else - return libc._memset(data, ch, size) + libc._memset(data, ch, size) end end @@ -80,7 +86,19 @@ function libc.strndup(s, n) if ffi then return ffi.string(s, n) else - return libc._strndup(s, n) + local s, errors = libc._strndup(s, n) + if not s then + os.raise(errors) + end + return s + end +end + +function libc.diffptr(data, offset) + if ffi then + return data + offset + else + return libc._diffptr(data, offset) end end |
