]> git.ipfire.org Git - thirdparty/systemd.git/commit
basic: implement the IteratedCache
authorVito Caputo <vcaputo@pengaru.com>
Sat, 27 Jan 2018 00:38:01 +0000 (16:38 -0800)
committerVito Caputo <vcaputo@pengaru.com>
Sat, 27 Jan 2018 21:11:50 +0000 (13:11 -0800)
commit45ea84d8edf57261a8e179f8058db6ba707dcde7
treea47b2e91624f6c6a2b03ebbbe4ca9beccba40b10
parent84dcca75b4a2c2274e220a1131ab9104c739d356
basic: implement the IteratedCache

Adds the basics of the IteratedCache and constructor support for the
Hashmap and OrderedHashmap types.

iterated_cache_get() is responsible for synchronizing the cache with
the associated Hashmap and making it available to the caller at the
supplied result pointers.  Since iterated_cache_get() may need to
allocate memory, it may fail, so callers must check the return value.

On success, pointer arrays containing pointers to the associated
Hashmap's keys and values, in as-iterated order, are returned in
res_keys and res_values, respectively.  Either may be supplied as NULL
to inhibit caching of the keys or values, respectively.

Note that if the cached Hashmap hasn't changed since the previous call
to iterated_cache_get(), and it's not a call activating caching of the
values or keys, the cost is effectively zero as the resulting pointers
will simply refer to the previously returned arrays as-is.

A cleanup function has also been added, iterated_cache_free().

This only frees the IteratedCache container and related arrays.  The
associated Hashmap, its keys, and values are not affected.  Also note
that the associated Hashmap does not automatically free its associated
IteratedCache when freed.

One could, in theory, safely access the arrays returned by a
successful iterated_cache_get() call after its associated Hashmap has
been freed, including the referenced values and keys.  Provided the
iterated_cache_get() was performed prior to the hashmap free, and that
the type of hashmap free performed didn't free keys and/or values as
well.
src/basic/hashmap.c
src/basic/hashmap.h