mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-10 13:31:49 +00:00
Merge pull request #871 from MihailRis/fix-bytearray
update bytearray,lua
This commit is contained in:
commit
f362bdfbbd
1 changed files with 9 additions and 2 deletions
|
|
@ -12,6 +12,8 @@ FFI.cdef[[
|
||||||
} bytearray_t;
|
} bytearray_t;
|
||||||
]]
|
]]
|
||||||
|
|
||||||
|
local free = FFI.C.free
|
||||||
|
|
||||||
local FFIBytearray
|
local FFIBytearray
|
||||||
local bytearray_type
|
local bytearray_type
|
||||||
|
|
||||||
|
|
@ -31,7 +33,7 @@ local function malloc(size)
|
||||||
allocated_bytes = 0
|
allocated_bytes = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
return FFI.gc(raw, FFI.C.free)
|
return raw
|
||||||
end
|
end
|
||||||
|
|
||||||
local function grow_buffer(self, elems)
|
local function grow_buffer(self, elems)
|
||||||
|
|
@ -40,6 +42,7 @@ local function grow_buffer(self, elems)
|
||||||
self.bytes = malloc(new_capacity)
|
self.bytes = malloc(new_capacity)
|
||||||
FFI.copy(self.bytes, prev, self.size)
|
FFI.copy(self.bytes, prev, self.size)
|
||||||
self.capacity = new_capacity
|
self.capacity = new_capacity
|
||||||
|
free(prev)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function trim_buffer(self)
|
local function trim_buffer(self)
|
||||||
|
|
@ -51,6 +54,7 @@ local function trim_buffer(self)
|
||||||
self.bytes = malloc(size)
|
self.bytes = malloc(size)
|
||||||
FFI.copy(self.bytes, prev, self.size)
|
FFI.copy(self.bytes, prev, self.size)
|
||||||
self.capacity = size
|
self.capacity = size
|
||||||
|
free(prev)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function count_elements(b)
|
local function count_elements(b)
|
||||||
|
|
@ -129,6 +133,7 @@ local function reserve(self, new_capacity)
|
||||||
self.bytes = malloc(new_capacity)
|
self.bytes = malloc(new_capacity)
|
||||||
FFI.copy(self.bytes, prev, self.size)
|
FFI.copy(self.bytes, prev, self.size)
|
||||||
self.capacity = new_capacity
|
self.capacity = new_capacity
|
||||||
|
free(prev)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_capacity(self)
|
local function get_capacity(self)
|
||||||
|
|
@ -187,7 +192,9 @@ local bytearray_mt = {
|
||||||
__len = function(self)
|
__len = function(self)
|
||||||
return tonumber(self.size)
|
return tonumber(self.size)
|
||||||
end,
|
end,
|
||||||
__gc = function(self) end,
|
__gc = function(self)
|
||||||
|
free(self.bytes)
|
||||||
|
end,
|
||||||
__ipairs = function(self)
|
__ipairs = function(self)
|
||||||
local i = 0
|
local i = 0
|
||||||
return function()
|
return function()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue