pipewire: properties: add helper to remove item

This commit is contained in:
Barnabás Pőcze 2021-06-12 15:20:27 +02:00
parent 2088f0652e
commit 71c5fe0af6
2 changed files with 13 additions and 8 deletions

View file

@ -67,6 +67,7 @@ struct pw_array {
#define pw_array_first(a) ((a)->data)
#define pw_array_end(a) SPA_PTROFF((a)->data, (a)->size, void)
#define pw_array_last(a, T) (((T *) pw_array_end(a)) - 1)
#define pw_array_check(a,p) (SPA_PTROFF(p,sizeof(*p),void) <= pw_array_end(a))
#define pw_array_for_each(pos, array) \

View file

@ -59,6 +59,17 @@ static int add_func(struct pw_properties *this, char *key, char *value)
return 0;
}
static void remove_item(struct pw_properties *this, struct spa_dict_item *item)
{
struct properties *impl = SPA_CONTAINER_OF(this, struct properties, this);
const struct spa_dict_item *last = pw_array_last(&impl->items, struct spa_dict_item);
item->key = last->key;
item->value = last->value;
impl->items.size -= sizeof(*item);
this->dict.n_items--;
}
static void clear_item(struct spa_dict_item *item)
{
free((char *) item->key);
@ -385,7 +396,6 @@ void pw_properties_free(struct pw_properties *properties)
static int do_replace(struct pw_properties *properties, const char *key, char *value, bool copy)
{
struct properties *impl = SPA_CONTAINER_OF(properties, struct properties, this);
struct spa_dict_item *item;
if (key == NULL || key[0] == 0)
@ -403,14 +413,8 @@ static int do_replace(struct pw_properties *properties, const char *key, char *v
goto exit_noupdate;
if (value == NULL) {
struct spa_dict_item *last = pw_array_get_unchecked(&impl->items,
pw_array_get_len(&impl->items, struct spa_dict_item) - 1,
struct spa_dict_item);
clear_item(item);
item->key = last->key;
item->value = last->value;
impl->items.size -= sizeof(struct spa_dict_item);
properties->dict.n_items--;
remove_item(properties, item);
SPA_FLAG_CLEAR(properties->dict.flags, SPA_DICT_FLAG_SORTED);
} else {
free((char *) item->value);