[FFmpeg-devel] [PATCH v2 1/3] avutil/dict: add av_dict_pop
Stefano Sabatini
stefasab at gmail.com
Sun Jul 2 11:43:07 EEST 2023
On date Sunday 2023-06-25 12:49:05 +0200, Marvin Scholz wrote:
> This new API allows to remove an entry and obtain ownership of the
> key/value that was associated with the removed entry.
> ---
>
> Changes since v1:
> - Clarify documentation about av_free having to be used.
> - Fix fate test to not rely on specific error code value
>
> doc/APIchanges | 4 ++++
> libavutil/dict.c | 27 +++++++++++++++++++++++++++
> libavutil/dict.h | 26 ++++++++++++++++++++++++++
> libavutil/tests/dict.c | 38 ++++++++++++++++++++++++++++++++++++++
> libavutil/version.h | 4 ++--
> tests/ref/fate/dict | 12 ++++++++++++
> 6 files changed, 109 insertions(+), 2 deletions(-)
>
> diff --git a/doc/APIchanges b/doc/APIchanges
> index f040211f7d..d55821f682 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -2,6 +2,10 @@ The last version increases of all libraries were on 2023-02-09
>
> API changes, most recent first:
>
> +2023-06-02 - xxxxxxxxxx - lavu 58.14.100 - dict.h
> + Add av_dict_pop() to remove an entry from a dict
> + and get ownership of the removed key/value.
> +
> 2023-05-29 - xxxxxxxxxx - lavc 60.16.100 - avcodec.h codec_id.h
> Add AV_CODEC_ID_EVC, FF_PROFILE_EVC_BASELINE, and FF_PROFILE_EVC_MAIN.
>
> diff --git a/libavutil/dict.c b/libavutil/dict.c
> index f673977a98..ac41771994 100644
> --- a/libavutil/dict.c
> +++ b/libavutil/dict.c
> @@ -173,6 +173,33 @@ int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value,
> return av_dict_set(pm, key, valuestr, flags);
> }
>
> +int av_dict_pop(AVDictionary **pm, const char *key,
> + char **out_key, char **out_value, int flags)
> +{
> + AVDictionary *m = *pm;
> + AVDictionaryEntry *entry = NULL;
> + entry = (AVDictionaryEntry *)av_dict_get(m, key, NULL, flags);
> + if (!entry)
> + return AVERROR(ENOENT);
> +
> + if (out_key)
> + *out_key = entry->key;
> + else
> + av_free(entry->key);
> +
> + if (out_value)
> + *out_value = entry->value;
> + else
> + av_free(entry->value);
> +
> + *entry = m->elems[--m->count];
> + if (m && !m->count) {
> + av_freep(&m->elems);
> + av_freep(pm);
> + }
> + return 0;
> +}
> +
> static int parse_key_value_pair(AVDictionary **pm, const char **buf,
> const char *key_val_sep, const char *pairs_sep,
> int flags)
> diff --git a/libavutil/dict.h b/libavutil/dict.h
> index 713c9e361a..31d38dabec 100644
> --- a/libavutil/dict.h
> +++ b/libavutil/dict.h
> @@ -172,6 +172,32 @@ int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags
> */
> int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags);
>
> +/**
> + * Remove the entry with the given key from the dictionary.
> + *
> + * Search for an entry matching @p key and remove it, if found. Optionally
> + * the found key and/or value can be returned using the @p out_key and
> + * @p out_value arguments.
Note: I checked the code and we see that in some cases we use `param`
(e.g. in mem.h) but I prefer this format (although apparently not used
in other places) since it looks more consistent with the doxygen
syntax.
> + *
> + * If more than one entry matches, only one entry is removed and returned
> + * on each call. Which entry is returned first in that case is undefined.
> + *
> + * @param pm Pointer to a pointer to a dictionary struct.
> + * @param key Entry key to match.
> + * @param out_key Pointer whose pointee will be set to the matched
> + * entry key. Must be freed using av_dict_free() by
> + * the caller. May be NULL.
> + * @param out_value Pointer whose pointee will be set to the matched
> + * entry value. Must be freed using av_dict_free() by
> + * the caller. May be NULL.
missing docs for flags, something like:
@param flags flags passed to av_dict_get to look for the entry
should be fine
[...]
Looks good otherwise, thanks.
More information about the ffmpeg-devel
mailing list