eval/typval: Remove tv_list_item_free() as it is unused

This commit is contained in:
ZyX
2017-12-24 02:11:46 +03:00
parent 6bf3dc77c4
commit 608c3d7baf
3 changed files with 49 additions and 95 deletions

View File

@@ -58,18 +58,6 @@ static listitem_T *tv_list_item_alloc(void)
return xmalloc(sizeof(listitem_T)); return xmalloc(sizeof(listitem_T));
} }
/// Free a list item
///
/// Also clears the value. Does not touch watchers.
///
/// @param[out] item Item to free.
void tv_list_item_free(listitem_T *const item)
FUNC_ATTR_NONNULL_ALL
{
tv_clear(TV_LIST_ITEM_TV(item));
xfree(item);
}
/// Remove a list item from a List and free it /// Remove a list item from a List and free it
/// ///
/// Also clears the value. /// Also clears the value.
@@ -80,7 +68,8 @@ void tv_list_item_remove(list_T *const l, listitem_T *const item)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
{ {
tv_list_remove_items(l, item, item); tv_list_remove_items(l, item, item);
tv_list_item_free(item); tv_clear(TV_LIST_ITEM_TV(item));
xfree(item);
} }
//{{{2 List watchers //{{{2 List watchers

View File

@@ -28,8 +28,13 @@ local function tv_list_item_alloc()
return ffi.cast('listitem_T*', eval.xmalloc(ffi.sizeof('listitem_T'))) return ffi.cast('listitem_T*', eval.xmalloc(ffi.sizeof('listitem_T')))
end end
local function tv_list_item_free(li)
eval.tv_clear(li.li_tv)
eval.xfree(li)
end
local function li_alloc(nogc) local function li_alloc(nogc)
local gcfunc = eval.tv_list_item_free local gcfunc = tv_list_item_free
if nogc then gcfunc = nil end if nogc then gcfunc = nil end
local li = ffi.gc(tv_list_item_alloc(), gcfunc) local li = ffi.gc(tv_list_item_alloc(), gcfunc)
li.li_next = nil li.li_next = nil
@@ -537,6 +542,7 @@ return {
typvalt=typvalt, typvalt=typvalt,
li_alloc=li_alloc, li_alloc=li_alloc,
tv_list_item_free=tv_list_item_free,
dict_iter=dict_iter, dict_iter=dict_iter,
list_iter=list_iter, list_iter=list_iter,

View File

@@ -41,6 +41,7 @@ local tbl2callback = eval_helpers.tbl2callback
local dict_watchers = eval_helpers.dict_watchers local dict_watchers = eval_helpers.dict_watchers
local concat_tables = global_helpers.concat_tables local concat_tables = global_helpers.concat_tables
local map = global_helpers.map
local lib = cimport('./src/nvim/eval/typval.h', './src/nvim/memory.h', local lib = cimport('./src/nvim/eval/typval.h', './src/nvim/memory.h',
'./src/nvim/mbyte.h', './src/nvim/garray.h', './src/nvim/mbyte.h', './src/nvim/garray.h',
@@ -121,87 +122,6 @@ end
describe('typval.c', function() describe('typval.c', function()
describe('list', function() describe('list', function()
describe('item', function() describe('item', function()
describe('alloc()/free()', function()
itp('works', function()
local li = li_alloc(true)
neq(nil, li)
lib.tv_list_item_free(li)
alloc_log:check({
a.li(li),
a.freed(li),
})
end)
itp('also frees the value', function()
local li
local s
local l
local tv
li = li_alloc(true)
li.li_tv.v_type = lib.VAR_NUMBER
li.li_tv.vval.v_number = 10
lib.tv_list_item_free(li)
alloc_log:check({
a.li(li),
a.freed(li),
})
li = li_alloc(true)
li.li_tv.v_type = lib.VAR_FLOAT
li.li_tv.vval.v_float = 10.5
lib.tv_list_item_free(li)
alloc_log:check({
a.li(li),
a.freed(li),
})
li = li_alloc(true)
li.li_tv.v_type = lib.VAR_STRING
li.li_tv.vval.v_string = nil
lib.tv_list_item_free(li)
alloc_log:check({
a.li(li),
a.freed(alloc_log.null),
a.freed(li),
})
li = li_alloc(true)
li.li_tv.v_type = lib.VAR_STRING
s = to_cstr_nofree('test')
li.li_tv.vval.v_string = s
lib.tv_list_item_free(li)
alloc_log:check({
a.li(li),
a.str(s, #('test')),
a.freed(s),
a.freed(li),
})
li = li_alloc(true)
li.li_tv.v_type = lib.VAR_LIST
l = ffi.gc(list(), nil)
l.lv_refcount = 2
li.li_tv.vval.v_list = l
lib.tv_list_item_free(li)
alloc_log:check({
a.li(li),
a.list(l),
a.freed(li),
})
eq(1, l.lv_refcount)
li = li_alloc(true)
tv = lua2typvalt({})
tv.vval.v_dict.dv_refcount = 2
li.li_tv = tv
lib.tv_list_item_free(li)
alloc_log:check({
a.li(li),
a.dict(tv.vval.v_dict),
a.freed(li),
})
eq(1, tv.vval.v_dict.dv_refcount)
end)
end)
describe('remove()', function() describe('remove()', function()
itp('works', function() itp('works', function()
local l = list(1, 2, 3, 4, 5, 6, 7) local l = list(1, 2, 3, 4, 5, 6, 7)
@@ -236,6 +156,45 @@ describe('typval.c', function()
}) })
eq(lis, list_items(l)) eq(lis, list_items(l))
end) end)
itp('also frees the value', function()
local l = list('a', 'b', 'c', 'd')
neq(nil, l)
local lis = list_items(l)
alloc_log:check({
a.list(l),
a.str(lis[1].li_tv.vval.v_string, 1),
a.li(lis[1]),
a.str(lis[2].li_tv.vval.v_string, 1),
a.li(lis[2]),
a.str(lis[3].li_tv.vval.v_string, 1),
a.li(lis[3]),
a.str(lis[4].li_tv.vval.v_string, 1),
a.li(lis[4]),
})
local strings = map(function(li) return li.li_tv.vval.v_string end,
lis)
lib.tv_list_item_remove(l, lis[1])
alloc_log:check({
a.freed(table.remove(strings, 1)),
a.freed(table.remove(lis, 1)),
})
eq(lis, list_items(l))
lib.tv_list_item_remove(l, lis[2])
alloc_log:check({
a.freed(table.remove(strings, 2)),
a.freed(table.remove(lis, 2)),
})
eq(lis, list_items(l))
lib.tv_list_item_remove(l, lis[2])
alloc_log:check({
a.freed(table.remove(strings, 2)),
a.freed(table.remove(lis, 2)),
})
eq(lis, list_items(l))
end)
itp('works and adjusts watchers correctly', function() itp('works and adjusts watchers correctly', function()
local l = ffi.gc(list(1, 2, 3, 4, 5, 6, 7), nil) local l = ffi.gc(list(1, 2, 3, 4, 5, 6, 7), nil)
neq(nil, l) neq(nil, l)