Commit Graph

166 Commits

Author SHA1 Message Date
Allison Karlitskaya
115033338b ghash: fix small array handling in g_hash_table_remove_all_nodes()
Factor out the code for setting up the hash table size, mask and mod,
detecting valgrind and allocating the arrays for hashes, keys, and
values.

Make use of this new function from g_hash_table_remove_all_nodes().

The handling of have_big_keys and have_big_values was never correct in
this function because it reallocated the array without changing the
flags in the struct.  Any calls in to the hashtable from destroy
notifies would find the table in an inconsistent state.

Many thanks to Thomas Haller who is essentially responsible for all the
real work in this patch: both discovering and identifying the original
problem, as well as finding the solution to it.
2019-05-20 17:03:49 +02:00
Allison Karlitskaya
9add93e5a4 ghash: Be more explicit about memory in g_hash_table_destroy_all_nodes()
Make it clear that there is a reference transfer going on here, rather
than relying on the fields being overwritten on each branch of the
conditional below.
2019-05-20 17:03:49 +02:00
Allison Karlitskaya
b3fbf6c18b ghash: do less work when destroying the table
We were calling g_hash_table_set_shift() to reinitialise the hash table
even in the case of destroying it.  Only do that for the non-destruction
case, and fill the relevant fields with zeros for the destruction case.
This has a nice side effect of causing more certain crashes in case of
invalid reuse of the table after (or during) destruction.
2019-05-20 17:03:49 +02:00
Allison Karlitskaya
c5462cb3c1 ghash: Improve internal documentation
The changes introduced by 18745ff674 made
the comment at the top of g_hash_table_remove_all_nodes() no longer
correct.  Fix that inaccuracy and add more documentation all-around.
2019-05-20 17:03:49 +02:00
Allison Karlitskaya
96ce92025d ghash: fix bug introduced by valgrind fix
g_hash_table_new_full() had an invocation of
g_hash_table_realloc_key_or_value_array() with the @is_big argument
incorrectly hardcoded to FALSE, even though later in the function the
values of have_big_keys and have_big_values would be set conditionally.

This never caused problems before because on 64bit platforms, this would
result in the allocation of a guint-sized array (which would be fine, as
have_big_keys and have_big_values would always start out as false) and
on 32bit platforms, this function ignored the value and always allocated
a gpointer-sized array.

Since merge request GNOME/glib!845 we have the possibility for
have_big_keys and have_big_values to start out as TRUE on 64bit
platforms.  We need to make sure we pass the argument through correctly.
2019-05-20 17:03:49 +02:00
Allison Karlitskaya
436ca1f376 ghash: Disable small-arrays under valgrind
Valgrind can't find 64bit pointers when we pack them into an array of
32bit values.  Disable this optimisation if we detect that we are
running under valgrind.

Fixes #1749
2019-05-14 16:31:37 +02:00
Emmanuele Bassi
6cb6b418bf Add a version check for duplicated-branches warning
The GHashTable code ignores the duplicated-branches GCC warning, but we
need to do a compiler and version check, as either non-GCC compatible
compilers, or older versions of GCC will warn about the unknown pragma
or diagnostic.

If we don't do this while turning warnings into error, we're going to
fail the build unnecessarily.
2019-04-30 14:49:00 +01:00
Philip Withnall
38de3e9dc3 docs: Use ‘look up’ as a verb, rather than the noun ‘lookup’
Another niggle fixed.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-04-26 12:12:31 +01:00
Emmanuel Fleury
e9f57495c6 Fix various signedness warnings in glib/ghash.c
To conform to a better signedness schema, this patch change
GHashTable.size field from gint to gsize (and change accordingly the
tests with it).
2019-03-17 19:05:35 +01:00
Tapasweni Pathak
58bbdcf6c0 gmacros: Add G_ALIGNOF superseding _g_alignof macro 2018-12-18 13:59:23 +05:30
Hans Petter Jansson
d3074a748f ghash: Fix out-of-range use of signed integer
We were mistakenly shifting a signed int literal by up to 31 places.
Specify unsigned int instead.

Closes #1570
2018-10-12 13:09:39 +02:00
Hans Petter Jansson
9986395638 ghash: Use realloc in place of alloc for key/value
Minor simplification resulting in the removal of redundant alloc wrappers.
2018-10-03 22:14:38 +02:00
Hans Petter Jansson
194eef5f17 ghash: Be less eager to opportunistically grow the table on cleanup
When g_hash_table_resize() gets called, we clear out tombstones and grow
the table at the same time if needed. However, the threshold was set too
low, so we'd grow if the load was greater than .5 after subtracting
tombstones. Increase this threshold to ~.75.
2018-10-03 22:14:38 +02:00
Hans Petter Jansson
7eaf018b29 ghash: Significantly reduce peak memory use
When resizing, we were keeping both the old and new hash, key and value
arrays around while we reinserted entries, resulting in a peak memory
overhead of 50%. Using a temporary bookkeeping array with one bit per
entry we can now grow and shrink the main arrays using realloc() and an
eviction scheme, reducing the overhead to .625% (assuming 64-bit keys and
values). Tests show the CPU overhead is negligible.
2018-10-03 22:14:32 +02:00
Hans Petter Jansson
dc983d74cc ghash: Use less memory when storing ints on 64-bit platforms
If int is smaller than void * on our arch, we start out with
int-sized keys and values and resize to pointer-sized entries as
needed. This saves a good amount of memory when the HT is being
used with e.g. GUINT_TO_POINTER().
2018-10-03 22:11:07 +02:00
Hans Petter Jansson
171f698ead ghash: Simplify g_hash_table_set_shift()
Even if we're using a prime modulo for the initial probe, our table is
power-of-two-sized, meaning we can set the mask simply by subtracting one
from the size.
2018-09-17 16:17:10 +02:00
Hans Petter Jansson
0dee62973c ghash: Fix poor performance with densely populated keyspaces
Sequential integers would be densely packed in the table, leaving the
high-index buckets unused and causing abnormally long probes for many
operations. This was especially noticeable with failed lookups and
when "aging" the table by repeatedly inserting and removing integers
from a narrow range using g_direct_hash() as the hashing function.

The solution is to multiply the hash by a small prime before applying
the modulo. The compiler optimizes this to a few left shifts and adds, so
the constant overhead is small, and the entries will be spread out,
yielding a lower average probe count.
2018-09-17 15:28:11 +02:00
Philip Withnall
8dc8b33dfa ghash: Clear out arguments to NULL in g_hash_table_lookup_extended()
If the given key is not found, clear the orig_key and value arguments to
NULL as well as returning FALSE. Then the caller can unconditionally
check them.

This makes the behaviour of g_hash_table_lookup_extended() consistent
with g_hash_table_steal_extended().

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2018-08-31 12:46:48 +01:00
Philip Withnall
ac690d9a8c docs: Amend the docs for g_str_equal() to reflect current general usage
People do (and should) use g_str_equal() for string comparisons outside
of hash tables, because it’s easier to read than
`strcmp (str1, str2) == 0`. That should not be discouraged.

However, we should still be careful to point out that g_str_equal() is
not NULL-safe, and g_strcmp0() is.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2018-06-14 18:12:42 +01:00
Emmanuele Bassi
927de4433e Port GHashTable to gatomicrefcount
Use the newly added API for reference counting instead of rolling our
own.
2018-06-11 14:59:39 +01:00
Philip Withnall
6acece5074 ghash: Add g_hash_table_steal_extended()
This is a combination of g_hash_table_lookup_extended() and
g_hash_table_steal(), so that users can combine the two to reduce code
and eliminate a pointless second hash table lookup by
g_hash_table_steal().

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=795302
2018-05-08 12:41:13 +01:00
Emmanuele Bassi
77419cf578 docs: Mention the newly added return values
The return value of the g_hash_table_add(), g_hash_table_insert(), and
g_hash_table_replace() functions was changed from void to gboolean in
GLib 2.40, but it was not mentioned in the API reference or the release
notes.

https://bugzilla.gnome.org/show_bug.cgi?id=793300
2018-02-15 11:25:53 +00:00
Philip Withnall
e430541378 docs: Remove XML-style comments from documentation strings
gtk-doc doesn’t support them any more since it was ported to Markdown,
so they end up appearing in the generated documentation, which isn’t
great.

Mostly, they were used to split up things invisibly, which we can do in
other ways.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Reviewed-by: nobody
2018-01-12 15:29:29 +00:00
Sébastien Wilmet
f9faac7661 glib/: LGPLv2+ -> LGPLv2.1+
All glib/*.{c,h} files have been processed, as well as gtester-report.

12 of those files are not licensed under LGPL:

	gbsearcharray.h
	gconstructor.h
	glibintl.h
	gmirroringtable.h
	gscripttable.h
	gtranslit-data.h
	gunibreak.h
	gunichartables.h
	gunicomp.h
	gunidecomp.h
	valgrind.h
	win_iconv.c

Some of them are generated files, some are licensed under a BSD-style
license and win_iconv.c is in the public domain.

Sub-directories inside glib/:

	deprecated/: processed in a previous commit
	glib-mirroring-tab/: already LGPLv2.1+
	gnulib/: not modified, the code is copied from gnulib
	libcharset/: a copy
	pcre/: a copy
	tests/: processed in a previous commit

https://bugzilla.gnome.org/show_bug.cgi?id=776504
2017-05-24 11:58:19 +02:00
Philip Withnall
404c2d2454 ghash: Document that GHashTable is not suitable for static hash tables
Instead, gperf should be used for that kind of thing.

Inspired by http://stackoverflow.com/q/42372382/2931197.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2017-05-02 16:59:50 +01:00
Philip Withnall
b63469d726 docs: Fix (nullable) (optional) annotations
There are a few places where commit 18a33f72 replaced valid (nullable)
(optional) annotations with just (optional). That has a different
meaning.

(nullable) (optional) can only be applied to gpointer* parameters, and
means that both the gpointer* and returned gpointer can be NULL. i.e.
The caller can pass in NULL to ignore the return value; and the returned
value can be NULL.

(optional) can be applied to anything* parameters, and means that the
anything* can be NULL. i.e. The caller can pass in NULL to ignore the
return value. The return value cannot be NULL.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2017-04-28 12:32:12 +01:00
Philip Withnall
b3362bb834 ghash: Document order of parameters in GEqualFunc usage
See https://bugzilla.gnome.org/show_bug.cgi?id=698064#c15.
2017-03-23 15:55:31 +00:00
Philip Withnall
fb5a07ad39 ghash: Fix gtk-doc syntax in a documentation comment 2017-03-23 15:55:05 +00:00
Christian Hergert
18a33f72db introspection: use (nullable) or (optional) instead of (allow-none)
If we have an input parameter (or return value) we need to use (nullable).
However, if it is an (inout) or (out) parameter, (optional) is sufficient.

It looks like (nullable) could be used for everything according to the
Annotation documentation, but (optional) is more specific.
2016-11-22 14:14:37 -08:00
Mikhail Zabaluev
0a10d38d15 Return value of g_hash_table_get_{keys,values} is (transfer container)
https://bugzilla.gnome.org/show_bug.cgi?id=757742
2015-11-07 18:55:17 +02:00
Mikhail Zabaluev
42463b840f ghash: Correctly annotate (nullable) and (out) parameters
https://bugzilla.gnome.org/show_bug.cgi?id=757742
2015-11-07 18:55:17 +02:00
Philip Withnall
25a7c817d3 glib: Add missing (nullable) and (optional) annotations
Add various (nullable) and (optional) annotations which were missing
from a variety of functions. Also port a couple of existing (allow-none)
annotations in the same files to use (nullable) and (optional) as
appropriate instead.

Secondly, add various (not nullable) annotations as needed by the new
default in gobject-introspection of marking gpointers as (nullable). See
https://bugzilla.gnome.org/show_bug.cgi?id=729660.

This includes adding some stub documentation comments for the
assertion macro error functions, which weren’t previously documented.
The new comments are purely to allow for annotations, and hence are
marked as (skip) to prevent the symbols appearing in the GIR file.

https://bugzilla.gnome.org/show_bug.cgi?id=719966
2015-11-07 10:48:32 +01:00
Matthias Clasen
17871e6881 Add a note to the g_str_hash docs
Point out some shortcomings of the djb hash, as found in

https://bugzilla.gnome.org/show_bug.cgi?id=751610
2015-07-27 06:51:17 -04:00
Peter Meerwald
0441ae1ccf ghash: Fix typo in g_hash_table_replace() documentation
https://bugzilla.gnome.org/show_bug.cgi?id=752767
2015-07-24 12:41:01 -04:00
Philip Withnall
59748c3be0 ghash: Document that g_hash_get_[keys|values]() are expensive
And definitely not the right way to iterate over a hash table (as seen
in code in the wild).

https://bugzilla.gnome.org/show_bug.cgi?id=741779
2015-03-03 18:40:33 +00:00
Xavier Claessens
1a2a689dea Doc: glib: Fix all undocumented/unused/undeclared symbols
There is one issue left in gscanner.h due to a bug #741305 in gtk-doc.

https://bugzilla.gnome.org/show_bug.cgi?id=740814
2014-12-12 11:01:37 -05:00
Ryan Lortie
de65723877 ghash: minor docs tweak
We should not advise people to cast the result of
g_hash_table_get_keys_as_array() to a type that looks suitable for use
with g_strfreev().  Advise to use (const gchar **) instead.
2014-12-11 18:50:07 -05:00
Ryan Lortie
e9b7c70240 GHashTable: small docs fix
We use g_hash_table_unref() here, not g_object_unref().
2014-10-17 14:39:09 +02:00
Benjamin Berg
18745ff674 Allow hash table destroy notifiers to remove other entries
With this patch it is fine to call g_hash_table_lookup and
g_hash_table_remove from destroy notification functions. Before
this could lead to an infinitie loop if g_hash_table_remove_all
was used.

https://bugzilla.gnome.org/show_bug.cgi?id=695082
2014-10-17 14:29:26 +02:00
Matthias Clasen
90671cd3cd docs: Add missing language annotations 2014-06-01 09:38:49 -04:00
Xavier Claessens
6fcaa7aa96 GHashTable: Explicitly document that _iter_remove() is safe while iterating
https://bugzilla.gnome.org/show_bug.cgi?id=723316
2014-02-21 15:39:31 -05:00
William Jon McCann
20f4d1820b docs: use "Returns:" consistently
Instead of "Return value:".
2014-02-19 19:41:52 -05:00
Matthias Clasen
bc6ee788b4 docs: let go of &ast;
Since we are no longer using sgml mode, using /&ast; &ast;/ to
escape block comments inside examples does not work anymore.
Switch to using line comments with //
2014-02-14 21:33:36 -05:00
Matthias Clasen
e7fd3de86d Eradicate links and xrefs
These are all replaced by markdown ref links.
2014-02-08 12:26:56 -05:00
Matthias Clasen
3232425785 Docs: replace <literal> by ` 2014-02-06 08:07:16 -05:00
Matthias Clasen
cb588d4532 Convert external links to markdown syntax 2014-02-05 21:23:28 -05:00
Matthias Clasen
adf892e96a Annotate all examples with their language
The C ones, at least.
2014-02-01 15:11:49 -05:00
Daniel Mustieles
078dbda148 Updated FSF's address 2014-01-31 14:31:55 +01:00
Simon McVittie
a4480d5f71 GHashTable: statically assert that GHashTableIter works as intended
https://bugzilla.gnome.org/show_bug.cgi?id=688406
2014-01-19 20:23:43 -05:00
Matthias Clasen
910191597a Add boolean returns to some hash functions
The functions g_hash_table_insert, g_hash_table_replace
and g_hash_table_add now return TRUE if they inserted a
new key/value pair.

https://bugzilla.gnome.org/show_bug.cgi?id=697828
2013-11-24 01:22:44 -05:00