*: Remove most calls to tv_list_item_alloc

Still left calls in eval/typval.c and test/unit/eval/helpers.lua. Latter is the 
only reason why function did not receive `static` modifier.
This commit is contained in:
ZyX
2017-12-24 01:43:42 +03:00
parent 249bdb07dd
commit 0c533a488f
6 changed files with 179 additions and 116 deletions

View File

@@ -127,9 +127,7 @@ static inline int json_decoder_pop(ValuesStackItem obj,
return FAIL;
}
assert(last_container.special_val == NULL);
listitem_T *obj_li = tv_list_item_alloc();
*TV_LIST_ITEM_TV(obj_li) = obj.val;
tv_list_append(last_container.container.vval.v_list, obj_li);
tv_list_append_owned_tv(last_container.container.vval.v_list, obj.val);
} else if (last_container.stack_index == kv_size(*stack) - 2) {
if (!obj.didcolon) {
EMSG2(_("E474: Expected colon before dictionary value: %s"),
@@ -154,12 +152,8 @@ static inline int json_decoder_pop(ValuesStackItem obj,
} else {
list_T *const kv_pair = tv_list_alloc();
tv_list_append_list(last_container.special_val, kv_pair);
listitem_T *const key_li = tv_list_item_alloc();
*TV_LIST_ITEM_TV(key_li) = key.val;
tv_list_append(kv_pair, key_li);
listitem_T *const val_li = tv_list_item_alloc();
*TV_LIST_ITEM_TV(val_li) = obj.val;
tv_list_append(kv_pair, val_li);
tv_list_append_owned_tv(kv_pair, key.val);
tv_list_append_owned_tv(kv_pair, obj.val);
}
} else {
// Object with key only
@@ -1047,10 +1041,10 @@ int msgpack_to_vim(const msgpack_object mobj, typval_T *const rettv)
.vval = { .v_list = list },
};
for (size_t i = 0; i < mobj.via.array.size; i++) {
listitem_T *const li = tv_list_item_alloc();
TV_LIST_ITEM_TV(li)->v_type = VAR_UNKNOWN;
tv_list_append(list, li);
if (msgpack_to_vim(mobj.via.array.ptr[i], TV_LIST_ITEM_TV(li))
// Not populated yet, need to create list item to push.
tv_list_append_owned_tv(list, (typval_T) { .v_type = VAR_UNKNOWN });
if (msgpack_to_vim(mobj.via.array.ptr[i],
TV_LIST_ITEM_TV(tv_list_last(list)))
== FAIL) {
return FAIL;
}
@@ -1095,20 +1089,20 @@ msgpack_to_vim_generic_map: {}
for (size_t i = 0; i < mobj.via.map.size; i++) {
list_T *const kv_pair = tv_list_alloc();
tv_list_append_list(list, kv_pair);
listitem_T *const key_li = tv_list_item_alloc();
TV_LIST_ITEM_TV(key_li)->v_type = VAR_UNKNOWN;
tv_list_append(kv_pair, key_li);
listitem_T *const val_li = tv_list_item_alloc();
TV_LIST_ITEM_TV(val_li)->v_type = VAR_UNKNOWN;
tv_list_append(kv_pair, val_li);
if (msgpack_to_vim(mobj.via.map.ptr[i].key, TV_LIST_ITEM_TV(key_li))
== FAIL) {
typval_T key_tv = { .v_type = VAR_UNKNOWN };
if (msgpack_to_vim(mobj.via.map.ptr[i].key, &key_tv) == FAIL) {
tv_clear(&key_tv);
return FAIL;
}
if (msgpack_to_vim(mobj.via.map.ptr[i].val, TV_LIST_ITEM_TV(val_li))
== FAIL) {
tv_list_append_owned_tv(kv_pair, key_tv);
typval_T val_tv = { .v_type = VAR_UNKNOWN };
if (msgpack_to_vim(mobj.via.map.ptr[i].val, &val_tv) == FAIL) {
tv_clear(&val_tv);
return FAIL;
}
tv_list_append_owned_tv(kv_pair, val_tv);
}
break;
}

View File

@@ -393,19 +393,30 @@ void tv_list_append_tv(list_T *const l, typval_T *const tv)
tv_list_append(l, li);
}
/// Like tv_list_append_tv(), but tv is moved to a list
///
/// This means that it is no longer valid to use contents of the typval_T after
/// function exits.
void tv_list_append_owned_tv(list_T *const l, typval_T tv)
FUNC_ATTR_NONNULL_ALL
{
listitem_T *const li = tv_list_item_alloc();
*TV_LIST_ITEM_TV(li) = tv;
tv_list_append(l, li);
}
/// Append a list to a list as one item
///
/// @param[out] l List to append to.
/// @param[in,out] itemlist List to append. Reference count is increased.
void tv_list_append_list(list_T *const list, list_T *const itemlist)
void tv_list_append_list(list_T *const l, list_T *const itemlist)
FUNC_ATTR_NONNULL_ARG(1)
{
listitem_T *const li = tv_list_item_alloc();
TV_LIST_ITEM_TV(li)->v_type = VAR_LIST;
TV_LIST_ITEM_TV(li)->v_lock = VAR_UNLOCKED;
TV_LIST_ITEM_TV(li)->vval.v_list = itemlist;
tv_list_append(list, li);
tv_list_append_owned_tv(l, (typval_T) {
.v_type = VAR_LIST,
.v_lock = VAR_UNLOCKED,
.vval.v_list = itemlist,
});
tv_list_ref(itemlist);
}
@@ -413,15 +424,14 @@ void tv_list_append_list(list_T *const list, list_T *const itemlist)
///
/// @param[out] l List to append to.
/// @param[in,out] dict Dictionary to append. Reference count is increased.
void tv_list_append_dict(list_T *const list, dict_T *const dict)
void tv_list_append_dict(list_T *const l, dict_T *const dict)
FUNC_ATTR_NONNULL_ARG(1)
{
listitem_T *const li = tv_list_item_alloc();
TV_LIST_ITEM_TV(li)->v_type = VAR_DICT;
TV_LIST_ITEM_TV(li)->v_lock = VAR_UNLOCKED;
TV_LIST_ITEM_TV(li)->vval.v_dict = dict;
tv_list_append(list, li);
tv_list_append_owned_tv(l, (typval_T) {
.v_type = VAR_DICT,
.v_lock = VAR_UNLOCKED,
.vval.v_dict = dict,
});
if (dict != NULL) {
dict->dv_refcount++;
}
@@ -438,14 +448,15 @@ void tv_list_append_string(list_T *const l, const char *const str,
const ssize_t len)
FUNC_ATTR_NONNULL_ARG(1)
{
if (str == NULL) {
assert(len == 0 || len == -1);
tv_list_append_allocated_string(l, NULL);
} else {
tv_list_append_allocated_string(l, (len >= 0
? xmemdupz(str, (size_t)len)
: xstrdup(str)));
}
tv_list_append_owned_tv(l, (typval_T) {
.v_type = VAR_STRING,
.v_lock = VAR_UNLOCKED,
.vval.v_string = (str == NULL
? NULL
: (len >= 0
? xmemdupz(str, (size_t)len)
: xstrdup(str))),
});
}
/// Append given string to the list
@@ -457,12 +468,11 @@ void tv_list_append_string(list_T *const l, const char *const str,
void tv_list_append_allocated_string(list_T *const l, char *const str)
FUNC_ATTR_NONNULL_ARG(1)
{
listitem_T *const li = tv_list_item_alloc();
tv_list_append(l, li);
TV_LIST_ITEM_TV(li)->v_type = VAR_STRING;
TV_LIST_ITEM_TV(li)->v_lock = VAR_UNLOCKED;
TV_LIST_ITEM_TV(li)->vval.v_string = (char_u *)str;
tv_list_append_owned_tv(l, (typval_T) {
.v_type = VAR_STRING,
.v_lock = VAR_UNLOCKED,
.vval.v_string = (char_u *)str,
});
}
/// Append number to the list
@@ -472,11 +482,11 @@ void tv_list_append_allocated_string(list_T *const l, char *const str)
/// listitem_T.
void tv_list_append_number(list_T *const l, const varnumber_T n)
{
listitem_T *const li = tv_list_item_alloc();
TV_LIST_ITEM_TV(li)->v_type = VAR_NUMBER;
TV_LIST_ITEM_TV(li)->v_lock = VAR_UNLOCKED;
TV_LIST_ITEM_TV(li)->vval.v_number = n;
tv_list_append(l, li);
tv_list_append_owned_tv(l, (typval_T) {
.v_type = VAR_NUMBER,
.v_lock = VAR_UNLOCKED,
.vval.v_number = n,
});
}
//{{{2 Operations on the whole list