diff options
| author | Opportunity <[email protected]> | 2020-01-18 21:34:08 +0800 |
|---|---|---|
| committer | Opportunity <[email protected]> | 2020-01-19 13:01:09 +0800 |
| commit | 03fde30e800970b004863d32ace885556d3346b4 (patch) | |
| tree | 40ab8285b1fa9bfccab7a8913961a6e638b18c05 /xmake/core/base/bytes.lua | |
| parent | c4c151b8b05ff57a71199fd7cdea6b8b3b5bbd15 (diff) | |
add init
Diffstat (limited to 'xmake/core/base/bytes.lua')
| -rw-r--r-- | xmake/core/base/bytes.lua | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/xmake/core/base/bytes.lua b/xmake/core/base/bytes.lua index 54343a962..0a906fcbf 100644 --- a/xmake/core/base/bytes.lua +++ b/xmake/core/base/bytes.lua @@ -37,7 +37,7 @@ ffi.cdef[[ -- new a bytes instance -- --- bytes(size): allocates a buffer of given size +-- bytes(size[, init]): allocates a buffer of given size, init with given number or char value -- bytes(size, ptr [, manage]): mounts buffer on existing storage (manage memory or not) -- bytes(str): mounts a buffer from the given string -- bytes(bytes, start, last): mounts a buffer from another one, with start/last limits @@ -51,9 +51,9 @@ function _instance.new(...) local instance = table.inherit(_instance) if type(arg1) == "number" then local size = arg1 - local ptr = arg2 - if ptr then + if type(arg2) == "cdata" then -- bytes(size, ptr [, manage]): mounts buffer on existing storage (manage memory or not) + local ptr = arg2 local manage = arg3 if manage then instance._CDATA = ffi.gc(ffi.cast("unsigned char*", ptr), ffi.C.free) @@ -63,8 +63,21 @@ function _instance.new(...) instance._MANAGED = false end else - -- bytes(size): allocates a buffer of given size - ptr = ffi.C.malloc(size) + -- bytes(size[, init]): allocates a buffer of given size + local init + if arg2 then + if type(arg2) == "number" then + init = arg2 + elseif type(arg2) == "string" then + init = arg2:byte() + else + os.raise("invalid arguments #2 for bytes(size, ...), cdata, string, number or nil expected!") + end + end + local ptr = ffi.C.malloc(size) + if init then + ffi.fill(ptr, size, init) + end instance._CDATA = ffi.gc(ffi.cast("unsigned char*", ptr), ffi.C.free) instance._MANAGED = true end |
