summaryrefslogtreecommitdiff
path: root/src/basic/hashmap.c
Commit message (Collapse)AuthorAgeFilesLines
* hashmap: introduce hashmap_first_key_and_value() and friendsYu Watanabe2018-10-131-40/+8
|
* Move use_pool() to mempool.c and rename to mempool_enabled()Zbigniew Jędrzejewski-Szmek2018-10-111-14/+1
| | | | The only user is in hashmap.c, but it's a mempool thing.
* hashmap: add an explicit assert() for detecting when objects migrated ↵Lennart Poettering2018-08-031-2/+4
| | | | | | | | | | | between threads When clients don't follow protocol and use the same object from different threads, then we previously would silently corrupt memory. With this assert we'll fail with an assert(). This doesn't fix anything but certainly makes mis-uses easier to detect and debug. Triggered by https://bugzilla.redhat.com/show_bug.cgi?id=1609349
* hashmap: add an environment variable to turn off the memory pool used by ↵Lennart Poettering2018-08-031-6/+18
| | | | | | hashmaps Triggered by https://bugzilla.redhat.com/show_bug.cgi?id=1609349
* tree-wide: drop copyright headers from frequent contributorsZbigniew Jędrzejewski-Szmek2018-06-201-3/+0
| | | | | | | | Fixes #9320. for p in Shapovalov Chevalier Rozhkov Sievers Mack Herrmann Schmidt Rudenberg Sahani Landden Andersen Watanabe; do git grep -e 'Copyright.*'$p -l|xargs perl -i -0pe 's|/([*][*])?[*]\s+([*#]\s+)?Copyright[^\n]*'$p'[^\n]*\s*[*]([*][*])?/\n*|\n|gms; s|\s+([*#]\s+)?Copyright[^\n]*'$p'[^\n]*\n*|\n|gms' done
* tree-wide: beautify remaining copyright statementsLennart Poettering2018-06-141-1/+1
| | | | | | Let's unify an beautify our remaining copyright statements, with a unicode ©. This means our copyright statements are now always formatted the same way. Yay.
* tree-wide: remove Lennart's copyright linesLennart Poettering2018-06-141-1/+0
| | | | | | | | | | | These lines are generally out-of-date, incomplete and unnecessary. With SPDX and git repository much more accurate and fine grained information about licensing and authorship is available, hence let's drop the per-file copyright notice. Of course, removing copyright lines of others is problematic, hence this commit only removes my own lines and leaves all others untouched. It might be nicer if sooner or later those could go away too, making git the only and accurate source of authorship information.
* tree-wide: drop 'This file is part of systemd' blurbLennart Poettering2018-06-141-2/+0
| | | | | | | | | | | | | | | | This part of the copyright blurb stems from the GPL use recommendations: https://www.gnu.org/licenses/gpl-howto.en.html The concept appears to originate in times where version control was per file, instead of per tree, and was a way to glue the files together. Ultimately, we nowadays don't live in that world anymore, and this information is entirely useless anyway, as people are very welcome to copy these files into any projects they like, and they shouldn't have to change bits that are part of our copyright header for that. hence, let's just get rid of this old cruft, and shorten our codebase a bit.
* Turn VALGRIND variable into a meson configuration switchZbigniew Jędrzejewski-Szmek2018-05-171-1/+1
| | | | | | | | | | Configuration through environment variable is inconvenient with meson, because they cannot be convieniently changed and/or are not preserved during reconfiguration (https://github.com/mesonbuild/meson/issues/1503). This adds -Dvalgrind=true/false, which has the advantage that it can be set at any time with meson configure -Dvalgrind=... and ninja will rebuild targets as necessary. Additional minor advantages are better consistency with the options for hashmap debugging, and typo avoidance with '#if' instead of '#ifdef'.
* tree-wide: drop license boilerplateZbigniew Jędrzejewski-Szmek2018-04-061-13/+0
| | | | | | | | | | Files which are installed as-is (any .service and other unit files, .conf files, .policy files, etc), are left as is. My assumption is that SPDX identifiers are not yet that well known, so it's better to retain the extended header to avoid any doubt. I also kept any copyright lines. We can probably remove them, but it'd nice to obtain explicit acks from all involved authors before doing that.
* basic/hashmap: tweak code to avoid pointless gcc warningZbigniew Jędrzejewski-Szmek2018-02-021-5/+5
| | | | | | | | | | | | | | | | | gcc says: [196/1142] Compiling C object 'src/basic/basic@sta/hashmap.c.o'. ../src/basic/hashmap.c: In function ‘cachemem_maintain’: ../src/basic/hashmap.c:1913:17: warning: suggest parentheses around assignment used as truth value [-Wparentheses] mem->active = r = true; ^~~ which conflates two things: the first is transitive assignent a = b = c = d; the second is assignment of the value of an expression, which happens to be a an assignment expression here, and boolean. While the second _should_ be parenthesized, the first should _not_, and it's more natural to understand our code as the first, and gcc should treat this as an exception and not emit the warning. But since it's a while until this will be fixed, let's update our code too.
* basic: implement the IteratedCacheVito Caputo2018-01-271-1/+124
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* basic: track dirty state in HashmapBaseVito Caputo2018-01-261-0/+15
| | | | | | | This only adds marking the HashmapBase as dirty, no clearing of the dirty state happens yet. No functional changes.
* Add SPDX license identifiers to source files under the LGPLZbigniew Jędrzejewski-Szmek2017-11-191-0/+1
| | | | | This follows what the kernel is doing, c.f. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5fd54ace4721fc5ce2bb5aef6318fcf17f421460.
* basic/hashmap: add cleanup of memory pools (#7164)Zbigniew Jędrzejewski-Szmek2017-11-101-0/+24
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | It was dropped in 89439d4fc0d29f04ac68432fd06ab84bc4e36e20. As a result, every process that uses a hashmap allocates and then leaks the hashmap mempools. The mempools are only allocated in the main thread, but we don't know where the memory is used. So let's check if we are the last thread and free the mempools then. This is fairly heavy, because /proc/self/status has to be opened and parsed, but we do it only when compiled for valgrind, i.e. not by default, and compared to running under valgrind or asan, the extra cost is acceptable. The big advantage is that we don't have to think or filter out this false positive. As a micro-opt, cleanup is attempted only in the main thread. We could allow any thread to check if it is the last one and perform cleanup, but that'd mean that we'd have to _do_ the check in every thread. We don't use threads like that, our non-main threads are always short-lived, so let's just accept the possibility that we'll leak memory if a thread survives. The check is also non-atomic, but it's called in a destructor of the main thread _and_ we do cleanup only when there are no other threads, so the risk of some library suddenly spawning another thread is very low. All in all, this is not perfect, but should work in 999‰ of cases. Fixes the following valgrind warning: ==22564== HEAP SUMMARY: ==22564== in use at exit: 8,192 bytes in 2 blocks ==22564== total heap usage: 243 allocs, 241 frees, 151,905 bytes allocated ==22564== ==22564== 4,096 bytes in 1 blocks are still reachable in loss record 1 of 2 ==22564== at 0x4C2FB6B: malloc (vg_replace_malloc.c:299) ==22564== by 0x4F08A8C: mempool_alloc_tile (mempool.c:62) ==22564== by 0x4F08B16: mempool_alloc0_tile (mempool.c:81) ==22564== by 0x4EF8DE0: hashmap_base_new (hashmap.c:748) ==22564== by 0x4EF8ED9: internal_hashmap_new (hashmap.c:782) ==22564== by 0x11045D: test_hashmap_copy (test-hashmap-plain.c:87) ==22564== by 0x115722: test_hashmap_funcs (test-hashmap-plain.c:914) ==22564== by 0x10FC9D: main (test-hashmap.c:60) ==22564== ==22564== 4,096 bytes in 1 blocks are still reachable in loss record 2 of 2 ==22564== at 0x4C2FB6B: malloc (vg_replace_malloc.c:299) ==22564== by 0x4F08A8C: mempool_alloc_tile (mempool.c:62) ==22564== by 0x4F08B16: mempool_alloc0_tile (mempool.c:81) ==22564== by 0x4EF8DE0: hashmap_base_new (hashmap.c:748) ==22564== by 0x4EF8EF8: internal_ordered_hashmap_new (hashmap.c:786) ==22564== by 0x10A2A0: test_ordered_hashmap_copy (test-hashmap-ordered.c:89) ==22564== by 0x10F70F: test_ordered_hashmap_funcs (test-hashmap-ordered.c:916) ==22564== by 0x10FCA2: main (test-hashmap.c:61) ==22564== ==22564== LEAK SUMMARY: ==22564== definitely lost: 0 bytes in 0 blocks ==22564== indirectly lost: 0 bytes in 0 blocks ==22564== possibly lost: 0 bytes in 0 blocks ==22564== still reachable: 8,192 bytes in 2 blocks ==22564== suppressed: 0 bytes in 0 blocks v2: - check if we are the main thread v3: - check if there are no other threads
* Merge pull request #6974 from keszybz/clean-up-definesLennart Poettering2017-10-041-11/+11
|\ | | | | Clean up define definitions
| * build-sys: use #if Y instead of #ifdef Y everywhereZbigniew Jędrzejewski-Szmek2017-10-041-11/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The advantage is that is the name is mispellt, cpp will warn us. $ git grep -Ee "conf.set\('(HAVE|ENABLE)_" -l|xargs sed -r -i "s/conf.set\('(HAVE|ENABLE)_/conf.set10('\1_/" $ git grep -Ee '#ifn?def (HAVE|ENABLE)' -l|xargs sed -r -i 's/#ifdef (HAVE|ENABLE)/#if \1/; s/#ifndef (HAVE|ENABLE)/#if ! \1/;' $ git grep -Ee 'if.*defined\(HAVE' -l|xargs sed -i -r 's/defined\((HAVE_[A-Z0-9_]*)\)/\1/g' $ git grep -Ee 'if.*defined\(ENABLE' -l|xargs sed -i -r 's/defined\((ENABLE_[A-Z0-9_]*)\)/\1/g' + manual changes to meson.build squash! build-sys: use #if Y instead of #ifdef Y everywhere v2: - fix incorrect setting of HAVE_LIBIDN2
* | tree-wide: use IN_SET macro (#6977)Yu Watanabe2017-10-041-1/+1
|/
* tree-wide: use IN_SET where possibleAndreas Rammhold2017-10-021-1/+1
| | | | | In addition to the changes from #6933 this handles cases that could be matched with the included cocci file.
* set: add new set_put_strsplit() callLennart Poettering2016-06-211-0/+25
| | | | | It's like set_put_strdup(), but splits up a string via an extract_first_word() loop.
* hashmap: optimize set_put_strdup() a bitLennart Poettering2016-04-291-6/+4
| | | | | Hashing should be quicker than allocating, hence let's first check if the string already exists and only then allocate a new copy for it.
* hashmap: use void* and uint8_t* for generic pointersLennart Poettering2016-02-221-7/+7
| | | | | | As suggested by CODING_STYLE we should use "void*" as type for generic memory, and uint8_t* for generic bytes. Hence use that instead of "char*", which should really be used only for strings these days.
* tree-wide: remove Emacs lines from all filesDaniel Mack2016-02-101-2/+0
| | | | | This should be handled fine now by .dir-locals.el, so need to carry that stuff in every file.
* basic: split hash functions into their own header filesLennart Poettering2016-01-111-60/+0
| | | | The hash operations are not really that specific to hashmaps, hence split them into a .c module of their own.
* basic: ENABLE_DEBUG_HASHMAP needs <pthread.h>Henrik Kaare Poulsen2015-12-151-0/+1
| | | | this is a follow-up for commit 11c3a36649e5e5e77db499c92f3
* basic: include only what we useThomas Hindoe Paaboel Andersen2015-11-301-1/+2
| | | | | This is a cleaned up result of running iwyu but without forward declarations on src/basic.
* siphash24: let siphash24_finalize() and siphash24() return the result directlyDaniel Mack2015-11-161-1/+1
| | | | | | | | | | Rather than passing a pointer to return the result, return it directly from the function calls. Also, return the result in native endianess, and let the callers care about the conversion. For hash tables and bloom filters, we don't care, but in order to keep MAC addresses and DHCP client IDs stable, we explicitly convert to LE.
* siphash24: change result argument to uint64_tMartin Pitt2015-11-161-1/+1
| | | | | | | | | | | | Change the "out" parameter from uint8_t[8] to uint64_t. On architectures which enforce pointer alignment this fixes crashes when we previously cast an unaligned array to uint64_t*, and on others this should at least improve performance as the compiler now aligns these properly. This also simplifies the code in most cases by getting rid of typecasts. The only place which we can't change is struct duid's en.id, as that is _packed_ and public API, so we can't enforce alignment of the "id" field and have to use memcpy instead.
* util-lib: split out allocation calls into alloc-util.[ch]Lennart Poettering2015-10-271-0/+1
|
* util-lib: move is_main_thread() to process-util.[ch]Lennart Poettering2015-10-271-5/+6
|
* siphash24: unify APITom Gundersen2015-10-061-2/+5
| | | | | | | Make the API of the new helpers more similar to the old wrapper. In particular we now return the hash as a byte string to avoid any endianness problems.
* hashmap: hash_funcs - make inputs unambiguousTom Gundersen2015-10-051-1/+1
| | | | | | | | | Make sure all variable-length inputs are properly terminated or that their length is encoded in some way. This avoids ambiguity of adjacent inputs. E.g., in case of a hash function taking two strings, compressing "ab" followed by "c" is now distinct from "a" followed by "bc".
* hashmap: refactor hash_funcTom Gundersen2015-10-051-17/+15
| | | | | | | | | | | | All our hash functions are based on siphash24(), factor out siphash_init() and siphash24_finalize() and pass the siphash state to the hash functions rather than the hash key. This simplifies the hash functions, and in particular makes composition simpler as calling siphash24_compress() repeatedly on separate chunks of input has the same effect as first concatenating the input and then calling siphash23_compress() on the result.
* Merge pull request #294 from teg/hashmap-debug-locking-2David Herrmann2015-06-181-1/+7
|\ | | | | hashmap: debug - lock access to the global hashmap list
| * hashmap: debug - lock access to the global hashmap listTom Gundersen2015-06-181-1/+7
| | | | | | | | | | | | | | | | This may be used from multi-threaded programs (say through nss-resolve), so we must protect the global list. This is still only relevant for debug builds, so we do not try to handle cases where the locking fail, but simply assert.
* | hashmap: allow NULL key in ordered_hashmap_next()Michal Schmidt2015-06-171-2/+0
|/ | | | | | There is no reason to require key to be non-NULL. Change test_ordered_hashmap_next() to use trivial_hash_ops in order to test NULL key too.
* hashmap: fix iterators to not skip entriesDavid Herrmann2015-06-141-5/+9
| | | | | | | | | | | | | | | Currently, the HASHMAP iterators stop at the first NULL entry in a hashmap. This is non-obvious and breaks users like sd-device, which legitimately store NULL values in a hashmap. Fix all the iterators by taking a pointer to the value storage, instead of returning it. The iterators now return a boolean that tells whether the end of the list was reached. Current users of HASHMAP_FOREACH() are *NOT* changed to explicitly check for NULL. If it turns out, there were users that inserted NULL into hashmaps, but didn't properly check for it during iteration, then we really want to find those and fix them.
* build-sys: split internal basic/ library from shared/Kay Sievers2015-06-111-0/+1854
basic/ can be used by everything cannot use anything outside of basic/ libsystemd/ can use basic/ cannot use shared/ shared/ can use libsystemd/