2019-04-24 05:12:16 +08:00
|
|
|
/* Interface to hashtable implementations.
|
2024-01-04 19:52:08 +08:00
|
|
|
Copyright (C) 2006-2024 Free Software Foundation, Inc.
|
2019-04-24 05:12:16 +08:00
|
|
|
|
|
|
|
This file is part of libctf.
|
|
|
|
|
|
|
|
libctf is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation; either version 3, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; see the file COPYING. If not see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#include <ctf-impl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "libiberty.h"
|
|
|
|
#include "hashtab.h"
|
|
|
|
|
libctf: remove static/dynamic name lookup distinction
libctf internally maintains a set of hash tables for type name lookups,
one for each valid C type namespace (struct, union, enum, and everything
else).
Or, rather, it maintains *two* sets of hash tables: one, a ctf_hash *,
is meant for lookups in ctf_(buf)open()ed dicts with fixed content; the
other, a ctf_dynhash *, is meant for lookups in ctf_create()d dicts.
This distinction was somewhat valuable in the far pre-binutils past when
two different hashtable implementations were used (one expanding, the
other fixed-size), but those days are long gone: the hash table
implementations are almost identical, both wrappers around the libiberty
hashtab. The ctf_dynhash has many more capabilities than the ctf_hash
(iteration, deletion, etc etc) and has no downsides other than starting
at a fixed, arbitrary small size.
That limitation is easy to lift (via a new ctf_dynhash_create_sized()),
following which we can throw away nearly all the ctf_hash
implementation, and all the code to choose between readable and writable
hashtabs; the few convenience functions that are still useful (for
insertion of name -> type mappings) can also be generalized a bit so
that the extra string verification they do is potentially available to
other string lookups as well.
(libctf still has two hashtable implementations, ctf_dynhash, above,
and ctf_dynset, which is a key-only hashtab that can avoid a great many
malloc()s, used for high-volume applications in the deduplicator.)
libctf/
* ctf-create.c (ctf_create): Eliminate ctn_writable.
(ctf_dtd_insert): Likewise.
(ctf_dtd_delete): Likewise.
(ctf_rollback): Likewise.
(ctf_name_table): Eliminate ctf_names_t.
* ctf-hash.c (ctf_dynhash_create): Comment update.
Reimplement in terms of...
(ctf_dynhash_create_sized): ... this new function.
(ctf_hash_create): Remove.
(ctf_hash_size): Remove.
(ctf_hash_define_type): Remove.
(ctf_hash_destroy): Remove.
(ctf_hash_lookup_type): Rename to...
(ctf_dynhash_lookup_type): ... this.
(ctf_hash_insert_type): Rename to...
(ctf_dynhash_insert_type): ... this, moving validation to...
* ctf-string.c (ctf_strptr_validate): ... this new function.
* ctf-impl.h (struct ctf_names): Extirpate.
(struct ctf_lookup.ctl_hash): Now a ctf_dynhash_t.
(struct ctf_dict): All ctf_names_t fields are now ctf_dynhash_t.
(ctf_name_table): Now returns a ctf_dynhash_t.
(ctf_lookup_by_rawhash): Remove.
(ctf_hash_create): Likewise.
(ctf_hash_insert_type): Likewise.
(ctf_hash_define_type): Likewise.
(ctf_hash_lookup_type): Likewise.
(ctf_hash_size): Likewise.
(ctf_hash_destroy): Likewise.
(ctf_dynhash_create_sized): New.
(ctf_dynhash_insert_type): New.
(ctf_dynhash_lookup_type): New.
(ctf_strptr_validate): New.
* ctf-lookup.c (ctf_lookup_by_name_internal): Adapt.
* ctf-open.c (init_types): Adapt.
(ctf_set_ctl_hashes): Adapt.
(ctf_dict_close): Adapt.
* ctf-serialize.c (ctf_serialize): Adapt.
* ctf-types.c (ctf_lookup_by_rawhash): Remove.
2023-12-19 01:47:48 +08:00
|
|
|
/* We have two hashtable implementations:
|
libctf, hash: introduce the ctf_dynset
There are many places in the deduplicator which use hashtables as tiny
sets: keys with no value (and usually, but not always, no freeing
function) often with only one or a few members. For each of these, even
after the last change to not store the freeing functions, we are storing
a little malloced block for each item just to track the key/value pair,
and a little malloced block for the hash table itself just to track the
freeing function because we can't use libiberty hashtab's freeing
function because we are using that to free the little malloced per-item
block.
If we only have a key, we don't need any of that: we can ditch the
per-malloced block because we don't have a value, and we can ditch the
per-hashtab structure because we don't need to independently track the
freeing functions since libiberty hashtab is doing it for us. That
means we don't need an owner field in the (now nonexistent) item block
either.
Roughly speaking, this datatype saves about 25% in time and 20% in peak
memory usage for normal links, even fairly big ones. So this might seem
redundant, but it's really worth it.
Instead of a _lookup function, a dynset has two distinct functions:
ctf_dynset_exists, which returns true or false and an optional pointer
to the set member, and ctf_dynhash_lookup_any, which is used if all
members of the set are expected to be equivalent and we just want *any*
member and we don't care which one.
There is no iterator in this set of functions, not because we don't
iterate over dynset members -- we do, a lot -- but because the iterator
here is a member of an entirely new family of much more convenient
iteration functions, introduced in the next commit.
libctf/
* ctf-hash.c (ctf_dynset_eq_string): New.
(ctf_dynset_create): New.
(DYNSET_EMPTY_ENTRY_REPLACEMENT): New.
(DYNSET_DELETED_ENTRY_REPLACEMENT): New.
(key_to_internal): New.
(internal_to_key): New.
(ctf_dynset_insert): New.
(ctf_dynset_remove): New.
(ctf_dynset_destroy): New.
(ctf_dynset_lookup): New.
(ctf_dynset_exists): New.
(ctf_dynset_lookup_any): New.
(ctf_hash_insert_type): Coding style.
(ctf_hash_define_type): Likewise.
* ctf-impl.h (ctf_dynset_t): New.
(ctf_dynset_eq_string): New.
(ctf_dynset_create): New.
(ctf_dynset_insert): New.
(ctf_dynset_remove): New.
(ctf_dynset_destroy): New.
(ctf_dynset_lookup): New.
(ctf_dynset_exists): New.
(ctf_dynset_lookup_any): New.
* ctf-inlines.h (ctf_dynset_cinsert): New.
2020-06-03 05:26:38 +08:00
|
|
|
|
|
|
|
- ctf_dynhash_* is an interface to a dynamically-expanding hash with
|
libctf: remove static/dynamic name lookup distinction
libctf internally maintains a set of hash tables for type name lookups,
one for each valid C type namespace (struct, union, enum, and everything
else).
Or, rather, it maintains *two* sets of hash tables: one, a ctf_hash *,
is meant for lookups in ctf_(buf)open()ed dicts with fixed content; the
other, a ctf_dynhash *, is meant for lookups in ctf_create()d dicts.
This distinction was somewhat valuable in the far pre-binutils past when
two different hashtable implementations were used (one expanding, the
other fixed-size), but those days are long gone: the hash table
implementations are almost identical, both wrappers around the libiberty
hashtab. The ctf_dynhash has many more capabilities than the ctf_hash
(iteration, deletion, etc etc) and has no downsides other than starting
at a fixed, arbitrary small size.
That limitation is easy to lift (via a new ctf_dynhash_create_sized()),
following which we can throw away nearly all the ctf_hash
implementation, and all the code to choose between readable and writable
hashtabs; the few convenience functions that are still useful (for
insertion of name -> type mappings) can also be generalized a bit so
that the extra string verification they do is potentially available to
other string lookups as well.
(libctf still has two hashtable implementations, ctf_dynhash, above,
and ctf_dynset, which is a key-only hashtab that can avoid a great many
malloc()s, used for high-volume applications in the deduplicator.)
libctf/
* ctf-create.c (ctf_create): Eliminate ctn_writable.
(ctf_dtd_insert): Likewise.
(ctf_dtd_delete): Likewise.
(ctf_rollback): Likewise.
(ctf_name_table): Eliminate ctf_names_t.
* ctf-hash.c (ctf_dynhash_create): Comment update.
Reimplement in terms of...
(ctf_dynhash_create_sized): ... this new function.
(ctf_hash_create): Remove.
(ctf_hash_size): Remove.
(ctf_hash_define_type): Remove.
(ctf_hash_destroy): Remove.
(ctf_hash_lookup_type): Rename to...
(ctf_dynhash_lookup_type): ... this.
(ctf_hash_insert_type): Rename to...
(ctf_dynhash_insert_type): ... this, moving validation to...
* ctf-string.c (ctf_strptr_validate): ... this new function.
* ctf-impl.h (struct ctf_names): Extirpate.
(struct ctf_lookup.ctl_hash): Now a ctf_dynhash_t.
(struct ctf_dict): All ctf_names_t fields are now ctf_dynhash_t.
(ctf_name_table): Now returns a ctf_dynhash_t.
(ctf_lookup_by_rawhash): Remove.
(ctf_hash_create): Likewise.
(ctf_hash_insert_type): Likewise.
(ctf_hash_define_type): Likewise.
(ctf_hash_lookup_type): Likewise.
(ctf_hash_size): Likewise.
(ctf_hash_destroy): Likewise.
(ctf_dynhash_create_sized): New.
(ctf_dynhash_insert_type): New.
(ctf_dynhash_lookup_type): New.
(ctf_strptr_validate): New.
* ctf-lookup.c (ctf_lookup_by_name_internal): Adapt.
* ctf-open.c (init_types): Adapt.
(ctf_set_ctl_hashes): Adapt.
(ctf_dict_close): Adapt.
* ctf-serialize.c (ctf_serialize): Adapt.
* ctf-types.c (ctf_lookup_by_rawhash): Remove.
2023-12-19 01:47:48 +08:00
|
|
|
unknown size that should support addition of large numbers of items,
|
|
|
|
and removal as well, and is used only at type-insertion time and during
|
|
|
|
linking. It can be constructed with an expected initial number of
|
|
|
|
elements, but need not be.
|
libctf, hash: introduce the ctf_dynset
There are many places in the deduplicator which use hashtables as tiny
sets: keys with no value (and usually, but not always, no freeing
function) often with only one or a few members. For each of these, even
after the last change to not store the freeing functions, we are storing
a little malloced block for each item just to track the key/value pair,
and a little malloced block for the hash table itself just to track the
freeing function because we can't use libiberty hashtab's freeing
function because we are using that to free the little malloced per-item
block.
If we only have a key, we don't need any of that: we can ditch the
per-malloced block because we don't have a value, and we can ditch the
per-hashtab structure because we don't need to independently track the
freeing functions since libiberty hashtab is doing it for us. That
means we don't need an owner field in the (now nonexistent) item block
either.
Roughly speaking, this datatype saves about 25% in time and 20% in peak
memory usage for normal links, even fairly big ones. So this might seem
redundant, but it's really worth it.
Instead of a _lookup function, a dynset has two distinct functions:
ctf_dynset_exists, which returns true or false and an optional pointer
to the set member, and ctf_dynhash_lookup_any, which is used if all
members of the set are expected to be equivalent and we just want *any*
member and we don't care which one.
There is no iterator in this set of functions, not because we don't
iterate over dynset members -- we do, a lot -- but because the iterator
here is a member of an entirely new family of much more convenient
iteration functions, introduced in the next commit.
libctf/
* ctf-hash.c (ctf_dynset_eq_string): New.
(ctf_dynset_create): New.
(DYNSET_EMPTY_ENTRY_REPLACEMENT): New.
(DYNSET_DELETED_ENTRY_REPLACEMENT): New.
(key_to_internal): New.
(internal_to_key): New.
(ctf_dynset_insert): New.
(ctf_dynset_remove): New.
(ctf_dynset_destroy): New.
(ctf_dynset_lookup): New.
(ctf_dynset_exists): New.
(ctf_dynset_lookup_any): New.
(ctf_hash_insert_type): Coding style.
(ctf_hash_define_type): Likewise.
* ctf-impl.h (ctf_dynset_t): New.
(ctf_dynset_eq_string): New.
(ctf_dynset_create): New.
(ctf_dynset_insert): New.
(ctf_dynset_remove): New.
(ctf_dynset_destroy): New.
(ctf_dynset_lookup): New.
(ctf_dynset_exists): New.
(ctf_dynset_lookup_any): New.
* ctf-inlines.h (ctf_dynset_cinsert): New.
2020-06-03 05:26:38 +08:00
|
|
|
|
|
|
|
- ctf_dynset_* is an interface to a dynamically-expanding hash that contains
|
|
|
|
only keys: no values.
|
|
|
|
|
|
|
|
These can be implemented by the same underlying hashmap if you wish. */
|
2019-04-24 05:12:16 +08:00
|
|
|
|
libctf: remove static/dynamic name lookup distinction
libctf internally maintains a set of hash tables for type name lookups,
one for each valid C type namespace (struct, union, enum, and everything
else).
Or, rather, it maintains *two* sets of hash tables: one, a ctf_hash *,
is meant for lookups in ctf_(buf)open()ed dicts with fixed content; the
other, a ctf_dynhash *, is meant for lookups in ctf_create()d dicts.
This distinction was somewhat valuable in the far pre-binutils past when
two different hashtable implementations were used (one expanding, the
other fixed-size), but those days are long gone: the hash table
implementations are almost identical, both wrappers around the libiberty
hashtab. The ctf_dynhash has many more capabilities than the ctf_hash
(iteration, deletion, etc etc) and has no downsides other than starting
at a fixed, arbitrary small size.
That limitation is easy to lift (via a new ctf_dynhash_create_sized()),
following which we can throw away nearly all the ctf_hash
implementation, and all the code to choose between readable and writable
hashtabs; the few convenience functions that are still useful (for
insertion of name -> type mappings) can also be generalized a bit so
that the extra string verification they do is potentially available to
other string lookups as well.
(libctf still has two hashtable implementations, ctf_dynhash, above,
and ctf_dynset, which is a key-only hashtab that can avoid a great many
malloc()s, used for high-volume applications in the deduplicator.)
libctf/
* ctf-create.c (ctf_create): Eliminate ctn_writable.
(ctf_dtd_insert): Likewise.
(ctf_dtd_delete): Likewise.
(ctf_rollback): Likewise.
(ctf_name_table): Eliminate ctf_names_t.
* ctf-hash.c (ctf_dynhash_create): Comment update.
Reimplement in terms of...
(ctf_dynhash_create_sized): ... this new function.
(ctf_hash_create): Remove.
(ctf_hash_size): Remove.
(ctf_hash_define_type): Remove.
(ctf_hash_destroy): Remove.
(ctf_hash_lookup_type): Rename to...
(ctf_dynhash_lookup_type): ... this.
(ctf_hash_insert_type): Rename to...
(ctf_dynhash_insert_type): ... this, moving validation to...
* ctf-string.c (ctf_strptr_validate): ... this new function.
* ctf-impl.h (struct ctf_names): Extirpate.
(struct ctf_lookup.ctl_hash): Now a ctf_dynhash_t.
(struct ctf_dict): All ctf_names_t fields are now ctf_dynhash_t.
(ctf_name_table): Now returns a ctf_dynhash_t.
(ctf_lookup_by_rawhash): Remove.
(ctf_hash_create): Likewise.
(ctf_hash_insert_type): Likewise.
(ctf_hash_define_type): Likewise.
(ctf_hash_lookup_type): Likewise.
(ctf_hash_size): Likewise.
(ctf_hash_destroy): Likewise.
(ctf_dynhash_create_sized): New.
(ctf_dynhash_insert_type): New.
(ctf_dynhash_lookup_type): New.
(ctf_strptr_validate): New.
* ctf-lookup.c (ctf_lookup_by_name_internal): Adapt.
* ctf-open.c (init_types): Adapt.
(ctf_set_ctl_hashes): Adapt.
(ctf_dict_close): Adapt.
* ctf-serialize.c (ctf_serialize): Adapt.
* ctf-types.c (ctf_lookup_by_rawhash): Remove.
2023-12-19 01:47:48 +08:00
|
|
|
/* The helem is used for general key/value mappings in the ctf_dynhash: the
|
|
|
|
owner may not have space allocated for it, and will be garbage (not
|
|
|
|
NULL!) in that case. */
|
2020-06-03 05:00:14 +08:00
|
|
|
|
2019-04-24 05:12:16 +08:00
|
|
|
typedef struct ctf_helem
|
|
|
|
{
|
|
|
|
void *key; /* Either a pointer, or a coerced ctf_id_t. */
|
|
|
|
void *value; /* The value (possibly a coerced int). */
|
2020-06-03 05:00:14 +08:00
|
|
|
ctf_dynhash_t *owner; /* The hash that owns us. */
|
2019-04-24 05:12:16 +08:00
|
|
|
} ctf_helem_t;
|
|
|
|
|
2020-06-03 05:00:14 +08:00
|
|
|
/* Equally, the key_free and value_free may not exist. */
|
|
|
|
|
2019-04-24 05:12:16 +08:00
|
|
|
struct ctf_dynhash
|
|
|
|
{
|
|
|
|
struct htab *htab;
|
|
|
|
ctf_hash_free_fun key_free;
|
|
|
|
ctf_hash_free_fun value_free;
|
|
|
|
};
|
|
|
|
|
libctf, hash: introduce the ctf_dynset
There are many places in the deduplicator which use hashtables as tiny
sets: keys with no value (and usually, but not always, no freeing
function) often with only one or a few members. For each of these, even
after the last change to not store the freeing functions, we are storing
a little malloced block for each item just to track the key/value pair,
and a little malloced block for the hash table itself just to track the
freeing function because we can't use libiberty hashtab's freeing
function because we are using that to free the little malloced per-item
block.
If we only have a key, we don't need any of that: we can ditch the
per-malloced block because we don't have a value, and we can ditch the
per-hashtab structure because we don't need to independently track the
freeing functions since libiberty hashtab is doing it for us. That
means we don't need an owner field in the (now nonexistent) item block
either.
Roughly speaking, this datatype saves about 25% in time and 20% in peak
memory usage for normal links, even fairly big ones. So this might seem
redundant, but it's really worth it.
Instead of a _lookup function, a dynset has two distinct functions:
ctf_dynset_exists, which returns true or false and an optional pointer
to the set member, and ctf_dynhash_lookup_any, which is used if all
members of the set are expected to be equivalent and we just want *any*
member and we don't care which one.
There is no iterator in this set of functions, not because we don't
iterate over dynset members -- we do, a lot -- but because the iterator
here is a member of an entirely new family of much more convenient
iteration functions, introduced in the next commit.
libctf/
* ctf-hash.c (ctf_dynset_eq_string): New.
(ctf_dynset_create): New.
(DYNSET_EMPTY_ENTRY_REPLACEMENT): New.
(DYNSET_DELETED_ENTRY_REPLACEMENT): New.
(key_to_internal): New.
(internal_to_key): New.
(ctf_dynset_insert): New.
(ctf_dynset_remove): New.
(ctf_dynset_destroy): New.
(ctf_dynset_lookup): New.
(ctf_dynset_exists): New.
(ctf_dynset_lookup_any): New.
(ctf_hash_insert_type): Coding style.
(ctf_hash_define_type): Likewise.
* ctf-impl.h (ctf_dynset_t): New.
(ctf_dynset_eq_string): New.
(ctf_dynset_create): New.
(ctf_dynset_insert): New.
(ctf_dynset_remove): New.
(ctf_dynset_destroy): New.
(ctf_dynset_lookup): New.
(ctf_dynset_exists): New.
(ctf_dynset_lookup_any): New.
* ctf-inlines.h (ctf_dynset_cinsert): New.
2020-06-03 05:26:38 +08:00
|
|
|
/* Hash and eq functions for the dynhash and hash. */
|
2019-04-24 05:12:16 +08:00
|
|
|
|
|
|
|
unsigned int
|
|
|
|
ctf_hash_integer (const void *ptr)
|
|
|
|
{
|
|
|
|
ctf_helem_t *hep = (ctf_helem_t *) ptr;
|
|
|
|
|
|
|
|
return htab_hash_pointer (hep->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ctf_hash_eq_integer (const void *a, const void *b)
|
|
|
|
{
|
|
|
|
ctf_helem_t *hep_a = (ctf_helem_t *) a;
|
|
|
|
ctf_helem_t *hep_b = (ctf_helem_t *) b;
|
|
|
|
|
|
|
|
return htab_eq_pointer (hep_a->key, hep_b->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
ctf_hash_string (const void *ptr)
|
|
|
|
{
|
|
|
|
ctf_helem_t *hep = (ctf_helem_t *) ptr;
|
|
|
|
|
|
|
|
return htab_hash_string (hep->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ctf_hash_eq_string (const void *a, const void *b)
|
|
|
|
{
|
|
|
|
ctf_helem_t *hep_a = (ctf_helem_t *) a;
|
|
|
|
ctf_helem_t *hep_b = (ctf_helem_t *) b;
|
|
|
|
|
|
|
|
return !strcmp((const char *) hep_a->key, (const char *) hep_b->key);
|
|
|
|
}
|
|
|
|
|
2020-06-05 00:21:10 +08:00
|
|
|
/* Hash a type_key. */
|
2019-07-14 04:31:26 +08:00
|
|
|
unsigned int
|
2020-06-05 00:21:10 +08:00
|
|
|
ctf_hash_type_key (const void *ptr)
|
2019-07-14 04:31:26 +08:00
|
|
|
{
|
|
|
|
ctf_helem_t *hep = (ctf_helem_t *) ptr;
|
2020-06-05 00:21:10 +08:00
|
|
|
ctf_link_type_key_t *k = (ctf_link_type_key_t *) hep->key;
|
2019-07-14 04:31:26 +08:00
|
|
|
|
2020-06-05 00:21:10 +08:00
|
|
|
return htab_hash_pointer (k->cltk_fp) + 59
|
|
|
|
* htab_hash_pointer ((void *) (uintptr_t) k->cltk_idx);
|
2019-07-14 04:31:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-06-05 00:21:10 +08:00
|
|
|
ctf_hash_eq_type_key (const void *a, const void *b)
|
2019-07-14 04:31:26 +08:00
|
|
|
{
|
|
|
|
ctf_helem_t *hep_a = (ctf_helem_t *) a;
|
|
|
|
ctf_helem_t *hep_b = (ctf_helem_t *) b;
|
2020-06-05 00:21:10 +08:00
|
|
|
ctf_link_type_key_t *key_a = (ctf_link_type_key_t *) hep_a->key;
|
|
|
|
ctf_link_type_key_t *key_b = (ctf_link_type_key_t *) hep_b->key;
|
2019-07-14 04:31:26 +08:00
|
|
|
|
2020-06-05 00:21:10 +08:00
|
|
|
return (key_a->cltk_fp == key_b->cltk_fp)
|
|
|
|
&& (key_a->cltk_idx == key_b->cltk_idx);
|
2019-07-14 04:31:26 +08:00
|
|
|
}
|
|
|
|
|
2020-06-06 01:35:46 +08:00
|
|
|
/* Hash a type_id_key. */
|
|
|
|
unsigned int
|
|
|
|
ctf_hash_type_id_key (const void *ptr)
|
|
|
|
{
|
|
|
|
ctf_helem_t *hep = (ctf_helem_t *) ptr;
|
|
|
|
ctf_type_id_key_t *k = (ctf_type_id_key_t *) hep->key;
|
|
|
|
|
|
|
|
return htab_hash_pointer ((void *) (uintptr_t) k->ctii_input_num)
|
|
|
|
+ 59 * htab_hash_pointer ((void *) (uintptr_t) k->ctii_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ctf_hash_eq_type_id_key (const void *a, const void *b)
|
|
|
|
{
|
|
|
|
ctf_helem_t *hep_a = (ctf_helem_t *) a;
|
|
|
|
ctf_helem_t *hep_b = (ctf_helem_t *) b;
|
|
|
|
ctf_type_id_key_t *key_a = (ctf_type_id_key_t *) hep_a->key;
|
|
|
|
ctf_type_id_key_t *key_b = (ctf_type_id_key_t *) hep_b->key;
|
|
|
|
|
|
|
|
return (key_a->ctii_input_num == key_b->ctii_input_num)
|
|
|
|
&& (key_a->ctii_type == key_b->ctii_type);
|
|
|
|
}
|
libctf, hash: introduce the ctf_dynset
There are many places in the deduplicator which use hashtables as tiny
sets: keys with no value (and usually, but not always, no freeing
function) often with only one or a few members. For each of these, even
after the last change to not store the freeing functions, we are storing
a little malloced block for each item just to track the key/value pair,
and a little malloced block for the hash table itself just to track the
freeing function because we can't use libiberty hashtab's freeing
function because we are using that to free the little malloced per-item
block.
If we only have a key, we don't need any of that: we can ditch the
per-malloced block because we don't have a value, and we can ditch the
per-hashtab structure because we don't need to independently track the
freeing functions since libiberty hashtab is doing it for us. That
means we don't need an owner field in the (now nonexistent) item block
either.
Roughly speaking, this datatype saves about 25% in time and 20% in peak
memory usage for normal links, even fairly big ones. So this might seem
redundant, but it's really worth it.
Instead of a _lookup function, a dynset has two distinct functions:
ctf_dynset_exists, which returns true or false and an optional pointer
to the set member, and ctf_dynhash_lookup_any, which is used if all
members of the set are expected to be equivalent and we just want *any*
member and we don't care which one.
There is no iterator in this set of functions, not because we don't
iterate over dynset members -- we do, a lot -- but because the iterator
here is a member of an entirely new family of much more convenient
iteration functions, introduced in the next commit.
libctf/
* ctf-hash.c (ctf_dynset_eq_string): New.
(ctf_dynset_create): New.
(DYNSET_EMPTY_ENTRY_REPLACEMENT): New.
(DYNSET_DELETED_ENTRY_REPLACEMENT): New.
(key_to_internal): New.
(internal_to_key): New.
(ctf_dynset_insert): New.
(ctf_dynset_remove): New.
(ctf_dynset_destroy): New.
(ctf_dynset_lookup): New.
(ctf_dynset_exists): New.
(ctf_dynset_lookup_any): New.
(ctf_hash_insert_type): Coding style.
(ctf_hash_define_type): Likewise.
* ctf-impl.h (ctf_dynset_t): New.
(ctf_dynset_eq_string): New.
(ctf_dynset_create): New.
(ctf_dynset_insert): New.
(ctf_dynset_remove): New.
(ctf_dynset_destroy): New.
(ctf_dynset_lookup): New.
(ctf_dynset_exists): New.
(ctf_dynset_lookup_any): New.
* ctf-inlines.h (ctf_dynset_cinsert): New.
2020-06-03 05:26:38 +08:00
|
|
|
|
2019-04-24 05:12:16 +08:00
|
|
|
/* The dynhash, used for hashes whose size is not known at creation time. */
|
|
|
|
|
2020-06-03 05:00:14 +08:00
|
|
|
/* Free a single ctf_helem with arbitrary key/value functions. */
|
2019-04-24 05:12:16 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
ctf_dynhash_item_free (void *item)
|
|
|
|
{
|
|
|
|
ctf_helem_t *helem = item;
|
|
|
|
|
2020-06-03 05:00:14 +08:00
|
|
|
if (helem->owner->key_free && helem->key)
|
|
|
|
helem->owner->key_free (helem->key);
|
|
|
|
if (helem->owner->value_free && helem->value)
|
|
|
|
helem->owner->value_free (helem->value);
|
2019-04-24 05:12:16 +08:00
|
|
|
free (helem);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctf_dynhash_t *
|
libctf: remove static/dynamic name lookup distinction
libctf internally maintains a set of hash tables for type name lookups,
one for each valid C type namespace (struct, union, enum, and everything
else).
Or, rather, it maintains *two* sets of hash tables: one, a ctf_hash *,
is meant for lookups in ctf_(buf)open()ed dicts with fixed content; the
other, a ctf_dynhash *, is meant for lookups in ctf_create()d dicts.
This distinction was somewhat valuable in the far pre-binutils past when
two different hashtable implementations were used (one expanding, the
other fixed-size), but those days are long gone: the hash table
implementations are almost identical, both wrappers around the libiberty
hashtab. The ctf_dynhash has many more capabilities than the ctf_hash
(iteration, deletion, etc etc) and has no downsides other than starting
at a fixed, arbitrary small size.
That limitation is easy to lift (via a new ctf_dynhash_create_sized()),
following which we can throw away nearly all the ctf_hash
implementation, and all the code to choose between readable and writable
hashtabs; the few convenience functions that are still useful (for
insertion of name -> type mappings) can also be generalized a bit so
that the extra string verification they do is potentially available to
other string lookups as well.
(libctf still has two hashtable implementations, ctf_dynhash, above,
and ctf_dynset, which is a key-only hashtab that can avoid a great many
malloc()s, used for high-volume applications in the deduplicator.)
libctf/
* ctf-create.c (ctf_create): Eliminate ctn_writable.
(ctf_dtd_insert): Likewise.
(ctf_dtd_delete): Likewise.
(ctf_rollback): Likewise.
(ctf_name_table): Eliminate ctf_names_t.
* ctf-hash.c (ctf_dynhash_create): Comment update.
Reimplement in terms of...
(ctf_dynhash_create_sized): ... this new function.
(ctf_hash_create): Remove.
(ctf_hash_size): Remove.
(ctf_hash_define_type): Remove.
(ctf_hash_destroy): Remove.
(ctf_hash_lookup_type): Rename to...
(ctf_dynhash_lookup_type): ... this.
(ctf_hash_insert_type): Rename to...
(ctf_dynhash_insert_type): ... this, moving validation to...
* ctf-string.c (ctf_strptr_validate): ... this new function.
* ctf-impl.h (struct ctf_names): Extirpate.
(struct ctf_lookup.ctl_hash): Now a ctf_dynhash_t.
(struct ctf_dict): All ctf_names_t fields are now ctf_dynhash_t.
(ctf_name_table): Now returns a ctf_dynhash_t.
(ctf_lookup_by_rawhash): Remove.
(ctf_hash_create): Likewise.
(ctf_hash_insert_type): Likewise.
(ctf_hash_define_type): Likewise.
(ctf_hash_lookup_type): Likewise.
(ctf_hash_size): Likewise.
(ctf_hash_destroy): Likewise.
(ctf_dynhash_create_sized): New.
(ctf_dynhash_insert_type): New.
(ctf_dynhash_lookup_type): New.
(ctf_strptr_validate): New.
* ctf-lookup.c (ctf_lookup_by_name_internal): Adapt.
* ctf-open.c (init_types): Adapt.
(ctf_set_ctl_hashes): Adapt.
(ctf_dict_close): Adapt.
* ctf-serialize.c (ctf_serialize): Adapt.
* ctf-types.c (ctf_lookup_by_rawhash): Remove.
2023-12-19 01:47:48 +08:00
|
|
|
ctf_dynhash_create_sized (unsigned long nelems, ctf_hash_fun hash_fun,
|
|
|
|
ctf_hash_eq_fun eq_fun, ctf_hash_free_fun key_free,
|
|
|
|
ctf_hash_free_fun value_free)
|
2019-04-24 05:12:16 +08:00
|
|
|
{
|
|
|
|
ctf_dynhash_t *dynhash;
|
2020-06-03 05:00:14 +08:00
|
|
|
htab_del del = ctf_dynhash_item_free;
|
2019-04-24 05:12:16 +08:00
|
|
|
|
2020-06-03 05:00:14 +08:00
|
|
|
if (key_free || value_free)
|
|
|
|
dynhash = malloc (sizeof (ctf_dynhash_t));
|
|
|
|
else
|
|
|
|
dynhash = malloc (offsetof (ctf_dynhash_t, key_free));
|
2019-04-24 05:12:16 +08:00
|
|
|
if (!dynhash)
|
|
|
|
return NULL;
|
|
|
|
|
2020-06-03 05:00:14 +08:00
|
|
|
if (key_free == NULL && value_free == NULL)
|
|
|
|
del = free;
|
|
|
|
|
libctf: remove static/dynamic name lookup distinction
libctf internally maintains a set of hash tables for type name lookups,
one for each valid C type namespace (struct, union, enum, and everything
else).
Or, rather, it maintains *two* sets of hash tables: one, a ctf_hash *,
is meant for lookups in ctf_(buf)open()ed dicts with fixed content; the
other, a ctf_dynhash *, is meant for lookups in ctf_create()d dicts.
This distinction was somewhat valuable in the far pre-binutils past when
two different hashtable implementations were used (one expanding, the
other fixed-size), but those days are long gone: the hash table
implementations are almost identical, both wrappers around the libiberty
hashtab. The ctf_dynhash has many more capabilities than the ctf_hash
(iteration, deletion, etc etc) and has no downsides other than starting
at a fixed, arbitrary small size.
That limitation is easy to lift (via a new ctf_dynhash_create_sized()),
following which we can throw away nearly all the ctf_hash
implementation, and all the code to choose between readable and writable
hashtabs; the few convenience functions that are still useful (for
insertion of name -> type mappings) can also be generalized a bit so
that the extra string verification they do is potentially available to
other string lookups as well.
(libctf still has two hashtable implementations, ctf_dynhash, above,
and ctf_dynset, which is a key-only hashtab that can avoid a great many
malloc()s, used for high-volume applications in the deduplicator.)
libctf/
* ctf-create.c (ctf_create): Eliminate ctn_writable.
(ctf_dtd_insert): Likewise.
(ctf_dtd_delete): Likewise.
(ctf_rollback): Likewise.
(ctf_name_table): Eliminate ctf_names_t.
* ctf-hash.c (ctf_dynhash_create): Comment update.
Reimplement in terms of...
(ctf_dynhash_create_sized): ... this new function.
(ctf_hash_create): Remove.
(ctf_hash_size): Remove.
(ctf_hash_define_type): Remove.
(ctf_hash_destroy): Remove.
(ctf_hash_lookup_type): Rename to...
(ctf_dynhash_lookup_type): ... this.
(ctf_hash_insert_type): Rename to...
(ctf_dynhash_insert_type): ... this, moving validation to...
* ctf-string.c (ctf_strptr_validate): ... this new function.
* ctf-impl.h (struct ctf_names): Extirpate.
(struct ctf_lookup.ctl_hash): Now a ctf_dynhash_t.
(struct ctf_dict): All ctf_names_t fields are now ctf_dynhash_t.
(ctf_name_table): Now returns a ctf_dynhash_t.
(ctf_lookup_by_rawhash): Remove.
(ctf_hash_create): Likewise.
(ctf_hash_insert_type): Likewise.
(ctf_hash_define_type): Likewise.
(ctf_hash_lookup_type): Likewise.
(ctf_hash_size): Likewise.
(ctf_hash_destroy): Likewise.
(ctf_dynhash_create_sized): New.
(ctf_dynhash_insert_type): New.
(ctf_dynhash_lookup_type): New.
(ctf_strptr_validate): New.
* ctf-lookup.c (ctf_lookup_by_name_internal): Adapt.
* ctf-open.c (init_types): Adapt.
(ctf_set_ctl_hashes): Adapt.
(ctf_dict_close): Adapt.
* ctf-serialize.c (ctf_serialize): Adapt.
* ctf-types.c (ctf_lookup_by_rawhash): Remove.
2023-12-19 01:47:48 +08:00
|
|
|
if ((dynhash->htab = htab_create_alloc (nelems, (htab_hash) hash_fun, eq_fun,
|
2020-06-03 05:00:14 +08:00
|
|
|
del, xcalloc, free)) == NULL)
|
2019-04-24 05:12:16 +08:00
|
|
|
{
|
|
|
|
free (dynhash);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-06-03 05:00:14 +08:00
|
|
|
if (key_free || value_free)
|
|
|
|
{
|
|
|
|
dynhash->key_free = key_free;
|
|
|
|
dynhash->value_free = value_free;
|
|
|
|
}
|
2019-04-24 05:12:16 +08:00
|
|
|
|
|
|
|
return dynhash;
|
|
|
|
}
|
|
|
|
|
libctf: remove static/dynamic name lookup distinction
libctf internally maintains a set of hash tables for type name lookups,
one for each valid C type namespace (struct, union, enum, and everything
else).
Or, rather, it maintains *two* sets of hash tables: one, a ctf_hash *,
is meant for lookups in ctf_(buf)open()ed dicts with fixed content; the
other, a ctf_dynhash *, is meant for lookups in ctf_create()d dicts.
This distinction was somewhat valuable in the far pre-binutils past when
two different hashtable implementations were used (one expanding, the
other fixed-size), but those days are long gone: the hash table
implementations are almost identical, both wrappers around the libiberty
hashtab. The ctf_dynhash has many more capabilities than the ctf_hash
(iteration, deletion, etc etc) and has no downsides other than starting
at a fixed, arbitrary small size.
That limitation is easy to lift (via a new ctf_dynhash_create_sized()),
following which we can throw away nearly all the ctf_hash
implementation, and all the code to choose between readable and writable
hashtabs; the few convenience functions that are still useful (for
insertion of name -> type mappings) can also be generalized a bit so
that the extra string verification they do is potentially available to
other string lookups as well.
(libctf still has two hashtable implementations, ctf_dynhash, above,
and ctf_dynset, which is a key-only hashtab that can avoid a great many
malloc()s, used for high-volume applications in the deduplicator.)
libctf/
* ctf-create.c (ctf_create): Eliminate ctn_writable.
(ctf_dtd_insert): Likewise.
(ctf_dtd_delete): Likewise.
(ctf_rollback): Likewise.
(ctf_name_table): Eliminate ctf_names_t.
* ctf-hash.c (ctf_dynhash_create): Comment update.
Reimplement in terms of...
(ctf_dynhash_create_sized): ... this new function.
(ctf_hash_create): Remove.
(ctf_hash_size): Remove.
(ctf_hash_define_type): Remove.
(ctf_hash_destroy): Remove.
(ctf_hash_lookup_type): Rename to...
(ctf_dynhash_lookup_type): ... this.
(ctf_hash_insert_type): Rename to...
(ctf_dynhash_insert_type): ... this, moving validation to...
* ctf-string.c (ctf_strptr_validate): ... this new function.
* ctf-impl.h (struct ctf_names): Extirpate.
(struct ctf_lookup.ctl_hash): Now a ctf_dynhash_t.
(struct ctf_dict): All ctf_names_t fields are now ctf_dynhash_t.
(ctf_name_table): Now returns a ctf_dynhash_t.
(ctf_lookup_by_rawhash): Remove.
(ctf_hash_create): Likewise.
(ctf_hash_insert_type): Likewise.
(ctf_hash_define_type): Likewise.
(ctf_hash_lookup_type): Likewise.
(ctf_hash_size): Likewise.
(ctf_hash_destroy): Likewise.
(ctf_dynhash_create_sized): New.
(ctf_dynhash_insert_type): New.
(ctf_dynhash_lookup_type): New.
(ctf_strptr_validate): New.
* ctf-lookup.c (ctf_lookup_by_name_internal): Adapt.
* ctf-open.c (init_types): Adapt.
(ctf_set_ctl_hashes): Adapt.
(ctf_dict_close): Adapt.
* ctf-serialize.c (ctf_serialize): Adapt.
* ctf-types.c (ctf_lookup_by_rawhash): Remove.
2023-12-19 01:47:48 +08:00
|
|
|
ctf_dynhash_t *
|
|
|
|
ctf_dynhash_create (ctf_hash_fun hash_fun, ctf_hash_eq_fun eq_fun,
|
|
|
|
ctf_hash_free_fun key_free, ctf_hash_free_fun value_free)
|
|
|
|
{
|
|
|
|
/* 7 is arbitrary and not benchmarked yet. */
|
|
|
|
|
|
|
|
return ctf_dynhash_create_sized (7, hash_fun, eq_fun, key_free, value_free);
|
|
|
|
}
|
|
|
|
|
2019-04-24 05:12:16 +08:00
|
|
|
static ctf_helem_t **
|
|
|
|
ctf_hashtab_lookup (struct htab *htab, const void *key, enum insert_option insert)
|
|
|
|
{
|
|
|
|
ctf_helem_t tmp = { .key = (void *) key };
|
|
|
|
return (ctf_helem_t **) htab_find_slot (htab, &tmp, insert);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ctf_helem_t *
|
2019-07-24 22:21:56 +08:00
|
|
|
ctf_hashtab_insert (struct htab *htab, void *key, void *value,
|
|
|
|
ctf_hash_free_fun key_free,
|
|
|
|
ctf_hash_free_fun value_free)
|
2019-04-24 05:12:16 +08:00
|
|
|
{
|
|
|
|
ctf_helem_t **slot;
|
|
|
|
|
|
|
|
slot = ctf_hashtab_lookup (htab, key, INSERT);
|
|
|
|
|
|
|
|
if (!slot)
|
|
|
|
{
|
2020-06-03 05:00:14 +08:00
|
|
|
errno = ENOMEM;
|
2019-04-24 05:12:16 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*slot)
|
|
|
|
{
|
2020-06-03 05:00:14 +08:00
|
|
|
/* Only spend space on the owner if we're going to use it: if there is a
|
|
|
|
key or value freeing function. */
|
|
|
|
if (key_free || value_free)
|
|
|
|
*slot = malloc (sizeof (ctf_helem_t));
|
|
|
|
else
|
|
|
|
*slot = malloc (offsetof (ctf_helem_t, owner));
|
2019-04-24 05:12:16 +08:00
|
|
|
if (!*slot)
|
|
|
|
return NULL;
|
2020-06-03 04:48:12 +08:00
|
|
|
(*slot)->key = key;
|
2019-04-24 05:12:16 +08:00
|
|
|
}
|
2019-07-24 22:21:56 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (key_free)
|
2020-06-03 04:48:12 +08:00
|
|
|
key_free (key);
|
2019-07-24 22:21:56 +08:00
|
|
|
if (value_free)
|
|
|
|
value_free ((*slot)->value);
|
|
|
|
}
|
2019-04-24 05:12:16 +08:00
|
|
|
(*slot)->value = value;
|
|
|
|
return *slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ctf_dynhash_insert (ctf_dynhash_t *hp, void *key, void *value)
|
|
|
|
{
|
|
|
|
ctf_helem_t *slot;
|
2020-06-03 05:00:14 +08:00
|
|
|
ctf_hash_free_fun key_free = NULL, value_free = NULL;
|
2019-04-24 05:12:16 +08:00
|
|
|
|
2020-06-03 05:00:14 +08:00
|
|
|
if (hp->htab->del_f == ctf_dynhash_item_free)
|
|
|
|
{
|
|
|
|
key_free = hp->key_free;
|
|
|
|
value_free = hp->value_free;
|
|
|
|
}
|
2019-07-24 22:21:56 +08:00
|
|
|
slot = ctf_hashtab_insert (hp->htab, key, value,
|
2020-06-03 05:00:14 +08:00
|
|
|
key_free, value_free);
|
2019-04-24 05:12:16 +08:00
|
|
|
|
|
|
|
if (!slot)
|
|
|
|
return errno;
|
|
|
|
|
2020-06-03 05:00:14 +08:00
|
|
|
/* Keep track of the owner, so that the del function can get at the key_free
|
|
|
|
and value_free functions. Only do this if one of those functions is set:
|
|
|
|
if not, the owner is not even present in the helem. */
|
2019-04-24 05:12:16 +08:00
|
|
|
|
2020-06-03 05:00:14 +08:00
|
|
|
if (key_free || value_free)
|
|
|
|
slot->owner = hp;
|
2019-04-24 05:12:16 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ctf_dynhash_remove (ctf_dynhash_t *hp, const void *key)
|
|
|
|
{
|
2020-06-03 05:00:14 +08:00
|
|
|
ctf_helem_t hep = { (void *) key, NULL, NULL };
|
2019-06-29 04:58:31 +08:00
|
|
|
htab_remove_elt (hp->htab, &hep);
|
2019-04-24 05:12:16 +08:00
|
|
|
}
|
|
|
|
|
2019-07-14 04:31:26 +08:00
|
|
|
void
|
|
|
|
ctf_dynhash_empty (ctf_dynhash_t *hp)
|
|
|
|
{
|
|
|
|
htab_empty (hp->htab);
|
|
|
|
}
|
|
|
|
|
2020-06-03 04:31:45 +08:00
|
|
|
size_t
|
|
|
|
ctf_dynhash_elements (ctf_dynhash_t *hp)
|
|
|
|
{
|
|
|
|
return htab_elements (hp->htab);
|
|
|
|
}
|
|
|
|
|
2019-04-24 05:12:16 +08:00
|
|
|
void *
|
|
|
|
ctf_dynhash_lookup (ctf_dynhash_t *hp, const void *key)
|
|
|
|
{
|
|
|
|
ctf_helem_t **slot;
|
|
|
|
|
|
|
|
slot = ctf_hashtab_lookup (hp->htab, key, NO_INSERT);
|
|
|
|
|
|
|
|
if (slot)
|
|
|
|
return (*slot)->value;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-06-03 04:31:45 +08:00
|
|
|
/* TRUE/FALSE return. */
|
|
|
|
int
|
|
|
|
ctf_dynhash_lookup_kv (ctf_dynhash_t *hp, const void *key,
|
|
|
|
const void **orig_key, void **value)
|
|
|
|
{
|
|
|
|
ctf_helem_t **slot;
|
|
|
|
|
|
|
|
slot = ctf_hashtab_lookup (hp->htab, key, NO_INSERT);
|
|
|
|
|
|
|
|
if (slot)
|
|
|
|
{
|
|
|
|
if (orig_key)
|
|
|
|
*orig_key = (*slot)->key;
|
|
|
|
if (value)
|
|
|
|
*value = (*slot)->value;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-27 20:30:22 +08:00
|
|
|
typedef struct ctf_traverse_cb_arg
|
|
|
|
{
|
|
|
|
ctf_hash_iter_f fun;
|
|
|
|
void *arg;
|
|
|
|
} ctf_traverse_cb_arg_t;
|
|
|
|
|
|
|
|
static int
|
|
|
|
ctf_hashtab_traverse (void **slot, void *arg_)
|
|
|
|
{
|
|
|
|
ctf_helem_t *helem = *((ctf_helem_t **) slot);
|
|
|
|
ctf_traverse_cb_arg_t *arg = (ctf_traverse_cb_arg_t *) arg_;
|
|
|
|
|
|
|
|
arg->fun (helem->key, helem->value, arg->arg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ctf_dynhash_iter (ctf_dynhash_t *hp, ctf_hash_iter_f fun, void *arg_)
|
|
|
|
{
|
|
|
|
ctf_traverse_cb_arg_t arg = { fun, arg_ };
|
|
|
|
htab_traverse (hp->htab, ctf_hashtab_traverse, &arg);
|
|
|
|
}
|
|
|
|
|
2020-06-03 04:31:45 +08:00
|
|
|
typedef struct ctf_traverse_find_cb_arg
|
|
|
|
{
|
|
|
|
ctf_hash_iter_find_f fun;
|
|
|
|
void *arg;
|
|
|
|
void *found_key;
|
|
|
|
} ctf_traverse_find_cb_arg_t;
|
|
|
|
|
|
|
|
static int
|
|
|
|
ctf_hashtab_traverse_find (void **slot, void *arg_)
|
|
|
|
{
|
|
|
|
ctf_helem_t *helem = *((ctf_helem_t **) slot);
|
|
|
|
ctf_traverse_find_cb_arg_t *arg = (ctf_traverse_find_cb_arg_t *) arg_;
|
|
|
|
|
|
|
|
if (arg->fun (helem->key, helem->value, arg->arg))
|
|
|
|
{
|
|
|
|
arg->found_key = helem->key;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
ctf_dynhash_iter_find (ctf_dynhash_t *hp, ctf_hash_iter_find_f fun, void *arg_)
|
|
|
|
{
|
|
|
|
ctf_traverse_find_cb_arg_t arg = { fun, arg_, NULL };
|
|
|
|
htab_traverse (hp->htab, ctf_hashtab_traverse_find, &arg);
|
|
|
|
return arg.found_key;
|
|
|
|
}
|
|
|
|
|
2019-06-27 20:30:22 +08:00
|
|
|
typedef struct ctf_traverse_remove_cb_arg
|
|
|
|
{
|
|
|
|
struct htab *htab;
|
|
|
|
ctf_hash_iter_remove_f fun;
|
|
|
|
void *arg;
|
|
|
|
} ctf_traverse_remove_cb_arg_t;
|
|
|
|
|
|
|
|
static int
|
|
|
|
ctf_hashtab_traverse_remove (void **slot, void *arg_)
|
|
|
|
{
|
|
|
|
ctf_helem_t *helem = *((ctf_helem_t **) slot);
|
|
|
|
ctf_traverse_remove_cb_arg_t *arg = (ctf_traverse_remove_cb_arg_t *) arg_;
|
|
|
|
|
|
|
|
if (arg->fun (helem->key, helem->value, arg->arg))
|
|
|
|
htab_clear_slot (arg->htab, slot);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ctf_dynhash_iter_remove (ctf_dynhash_t *hp, ctf_hash_iter_remove_f fun,
|
|
|
|
void *arg_)
|
|
|
|
{
|
|
|
|
ctf_traverse_remove_cb_arg_t arg = { hp->htab, fun, arg_ };
|
|
|
|
htab_traverse (hp->htab, ctf_hashtab_traverse_remove, &arg);
|
|
|
|
}
|
|
|
|
|
2020-06-03 23:36:18 +08:00
|
|
|
/* Traverse a dynhash in arbitrary order, in _next iterator form.
|
|
|
|
|
|
|
|
Mutating the dynhash while iterating is not supported (just as it isn't for
|
|
|
|
htab_traverse).
|
|
|
|
|
|
|
|
Note: unusually, this returns zero on success and a *positive* value on
|
|
|
|
error, because it does not take an fp, taking an error pointer would be
|
|
|
|
incredibly clunky, and nearly all error-handling ends up stuffing the result
|
|
|
|
of this into some sort of errno or ctf_errno, which is invariably
|
|
|
|
positive. So doing this simplifies essentially all callers. */
|
|
|
|
int
|
|
|
|
ctf_dynhash_next (ctf_dynhash_t *h, ctf_next_t **it, void **key, void **value)
|
|
|
|
{
|
|
|
|
ctf_next_t *i = *it;
|
|
|
|
ctf_helem_t *slot;
|
|
|
|
|
|
|
|
if (!i)
|
|
|
|
{
|
|
|
|
size_t size = htab_size (h->htab);
|
|
|
|
|
|
|
|
/* If the table has too many entries to fit in an ssize_t, just give up.
|
|
|
|
This might be spurious, but if any type-related hashtable has ever been
|
|
|
|
nearly as large as that then something very odd is going on. */
|
|
|
|
if (((ssize_t) size) < 0)
|
|
|
|
return EDOM;
|
|
|
|
|
|
|
|
if ((i = ctf_next_create ()) == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
i->u.ctn_hash_slot = h->htab->entries;
|
|
|
|
i->cu.ctn_h = h;
|
|
|
|
i->ctn_n = 0;
|
|
|
|
i->ctn_size = (ssize_t) size;
|
|
|
|
i->ctn_iter_fun = (void (*) (void)) ctf_dynhash_next;
|
|
|
|
*it = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((void (*) (void)) ctf_dynhash_next != i->ctn_iter_fun)
|
|
|
|
return ECTF_NEXT_WRONGFUN;
|
|
|
|
|
|
|
|
if (h != i->cu.ctn_h)
|
|
|
|
return ECTF_NEXT_WRONGFP;
|
|
|
|
|
|
|
|
if ((ssize_t) i->ctn_n == i->ctn_size)
|
|
|
|
goto hash_end;
|
|
|
|
|
|
|
|
while ((ssize_t) i->ctn_n < i->ctn_size
|
|
|
|
&& (*i->u.ctn_hash_slot == HTAB_EMPTY_ENTRY
|
|
|
|
|| *i->u.ctn_hash_slot == HTAB_DELETED_ENTRY))
|
|
|
|
{
|
|
|
|
i->u.ctn_hash_slot++;
|
|
|
|
i->ctn_n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ssize_t) i->ctn_n == i->ctn_size)
|
|
|
|
goto hash_end;
|
|
|
|
|
|
|
|
slot = *i->u.ctn_hash_slot;
|
|
|
|
|
|
|
|
if (key)
|
|
|
|
*key = slot->key;
|
|
|
|
if (value)
|
|
|
|
*value = slot->value;
|
|
|
|
|
|
|
|
i->u.ctn_hash_slot++;
|
|
|
|
i->ctn_n++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
hash_end:
|
|
|
|
ctf_next_destroy (i);
|
|
|
|
*it = NULL;
|
|
|
|
return ECTF_NEXT_END;
|
|
|
|
}
|
|
|
|
|
libctf: symbol type linking support
This adds facilities to write out the function info and data object
sections, which efficiently map from entries in the symbol table to
types. The write-side code is entirely new: the read-side code was
merely significantly changed and support for indexed tables added
(pointed to by the no-longer-unused cth_objtidxoff and cth_funcidxoff
header fields).
With this in place, you can use ctf_lookup_by_symbol to look up the
types of symbols of function and object type (and, as before, you can
use ctf_lookup_variable to look up types of file-scope variables not
present in the symbol table, as long as you know their name: but
variables that are also data objects are now found in the data object
section instead.)
(Compatible) file format change:
The CTF spec has always said that the function info section looks much
like the CTF_K_FUNCTIONs in the type section: an info word (including an
argument count) followed by a return type and N argument types. This
format is suboptimal: it means function symbols cannot be deduplicated
and it causes a lot of ugly code duplication in libctf. But
conveniently the compiler has never emitted this! Because it has always
emitted a rather different format that libctf has never accepted, we can
be sure that there are no instances of this function info section in the
wild, and can freely change its format without compatibility concerns or
a file format version bump. (And since it has never been emitted in any
code that generated any older file format version, either, we need keep
no code to read the format as specified at all!)
So the function info section is now specified as an array of uint32_t,
exactly like the object data section: each entry is a type ID in the
type section which must be of kind CTF_K_FUNCTION, the prototype of
this function.
This allows function types to be deduplicated and also correctly encodes
the fact that all functions declared in C really are types available to
the program: so they should be stored in the type section like all other
types. (In format v4, we will be able to represent the types of static
functions as well, but that really does require a file format change.)
We introduce a new header flag, CTF_F_NEWFUNCINFO, which is set if the
new function info format is in use. A sufficiently new compiler will
always set this flag. New libctf will always set this flag: old libctf
will refuse to open any CTF dicts that have this flag set. If the flag
is not set on a dict being read in, new libctf will disregard the
function info section. Format v4 will remove this flag (or, rather, the
flag has no meaning there and the bit position may be recycled for some
other purpose).
New API:
Symbol addition:
ctf_add_func_sym: Add a symbol with a given name and type. The
type must be of kind CTF_K_FUNCTION (a function
pointer). Internally this adds a name -> type
mapping to the ctf_funchash in the ctf_dict.
ctf_add_objt_sym: Add a symbol with a given name and type. The type
kind can be anything, including function pointers.
This adds to ctf_objthash.
These both treat symbols as name -> type mappings: the linker associates
symbol names with symbol indexes via the ctf_link_shuffle_syms callback,
which sets up the ctf_dynsyms/ctf_dynsymidx/ctf_dynsymmax fields in the
ctf_dict. Repeated relinks can add more symbols.
Variables that are also exposed as symbols are removed from the variable
section at serialization time.
CTF symbol type sections which have enough pads, defined by
CTF_INDEX_PAD_THRESHOLD (whether because they are in dicts with symbols
where most types are unknown, or in archive where most types are defined
in some child or parent dict, not in this specific dict) are sorted by
name rather than symidx and accompanied by an index which associates
each symbol type entry with a name: the existing ctf_lookup_by_symbol
will map symbol indexes to symbol names and look the names up in the
index automatically. (This is currently ELF-symbol-table-dependent, but
there is almost nothing specific to ELF in here and we can add support
for other symbol table formats easily).
The compiler also uses index sections to communicate the contents of
object file symbol tables without relying on any specific ordering of
symbols: it doesn't need to sort them, and libctf will detect an
unsorted index section via the absence of the new CTF_F_IDXSORTED header
flag, and sort it if needed.
Iteration:
ctf_symbol_next: Iterator which returns the types and names of symbols
one by one, either for function or data symbols.
This does not require any sorting: the ctf_link machinery uses it to
pull in all the compiler-provided symbols cheaply, but it is not
restricted to that use.
(Compatible) changes in API:
ctf_lookup_by_symbol: can now be called for object and function
symbols: never returns ECTF_NOTDATA (which is
now not thrown by anything, but is kept for
compatibility and because it is a plausible
error that we might start throwing again at some
later date).
Internally we also have changes to the ctf-string functionality so that
"external" strings (those where we track a string -> offset mapping, but
only write out an offset) can be consulted via the usual means
(ctf_strptr) before the strtab is written out. This is important
because ctf_link_add_linker_symbol can now be handed symbols named via
strtab offsets, and ctf_link_shuffle_syms must figure out their actual
names by looking in the external symtab we have just been fed by the
ctf_link_add_strtab callback, long before that strtab is written out.
include/ChangeLog
2020-11-20 Nick Alcock <nick.alcock@oracle.com>
* ctf-api.h (ctf_symbol_next): New.
(ctf_add_objt_sym): Likewise.
(ctf_add_func_sym): Likewise.
* ctf.h: Document new function info section format.
(CTF_F_NEWFUNCINFO): New.
(CTF_F_IDXSORTED): New.
(CTF_F_MAX): Adjust accordingly.
libctf/ChangeLog
2020-11-20 Nick Alcock <nick.alcock@oracle.com>
* ctf-impl.h (CTF_INDEX_PAD_THRESHOLD): New.
(_libctf_nonnull_): Likewise.
(ctf_in_flight_dynsym_t): New.
(ctf_dict_t) <ctf_funcidx_names>: Likewise.
<ctf_objtidx_names>: Likewise.
<ctf_nfuncidx>: Likewise.
<ctf_nobjtidx>: Likewise.
<ctf_funcidx_sxlate>: Likewise.
<ctf_objtidx_sxlate>: Likewise.
<ctf_objthash>: Likewise.
<ctf_funchash>: Likewise.
<ctf_dynsyms>: Likewise.
<ctf_dynsymidx>: Likewise.
<ctf_dynsymmax>: Likewise.
<ctf_in_flight_dynsym>: Likewise.
(struct ctf_next) <u.ctn_next>: Likewise.
(ctf_symtab_skippable): New prototype.
(ctf_add_funcobjt_sym): Likewise.
(ctf_dynhash_sort_by_name): Likewise.
(ctf_sym_to_elf64): Rename to...
(ctf_elf32_to_link_sym): ... this, and...
(ctf_elf64_to_link_sym): ... this.
* ctf-open.c (init_symtab): Check for lack of CTF_F_NEWFUNCINFO
flag, and presence of index sections. Refactor out
ctf_symtab_skippable and ctf_elf*_to_link_sym, and use them. Use
ctf_link_sym_t, not Elf64_Sym. Skip initializing objt or func
sxlate sections if corresponding index section is present. Adjust
for new func info section format.
(ctf_bufopen_internal): Add ctf_err_warn to corrupt-file error
handling. Report incorrect-length index sections. Always do an
init_symtab, even if there is no symtab section (there may be index
sections still).
(flip_objts): Adjust comment: func and objt sections are actually
identical in structure now, no need to caveat.
(ctf_dict_close): Free newly-added data structures.
* ctf-create.c (ctf_create): Initialize them.
(ctf_symtab_skippable): New, refactored out of
init_symtab, with st_nameidx_set check added.
(ctf_add_funcobjt_sym): New, add a function or object symbol to the
ctf_objthash or ctf_funchash, by name.
(ctf_add_objt_sym): Call it.
(ctf_add_func_sym): Likewise.
(symtypetab_delete_nonstatic_vars): New, delete vars also present as
data objects.
(CTF_SYMTYPETAB_EMIT_FUNCTION): New flag to symtypetab emitters:
this is a function emission, not a data object emission.
(CTF_SYMTYPETAB_EMIT_PAD): New flag to symtypetab emitters: emit
pads for symbols with no type (only set for unindexed sections).
(CTF_SYMTYPETAB_FORCE_INDEXED): New flag to symtypetab emitters:
always emit indexed.
(symtypetab_density): New, figure out section sizes.
(emit_symtypetab): New, emit a symtypetab.
(emit_symtypetab_index): New, emit a symtypetab index.
(ctf_serialize): Call them, emitting suitably sorted symtypetab
sections and indexes. Set suitable header flags. Copy over new
fields.
* ctf-hash.c (ctf_dynhash_sort_by_name): New, used to impose an
order on symtypetab index sections.
* ctf-link.c (ctf_add_type_mapping): Delete erroneous comment
relating to code that was never committed.
(ctf_link_one_variable): Improve variable name.
(check_sym): New, symtypetab analogue of check_variable.
(ctf_link_deduplicating_one_symtypetab): New.
(ctf_link_deduplicating_syms): Likewise.
(ctf_link_deduplicating): Call them.
(ctf_link_deduplicating_per_cu): Note that we don't call them in
this case (yet).
(ctf_link_add_strtab): Set the error on the fp correctly.
(ctf_link_add_linker_symbol): New (no longer a do-nothing stub), add
a linker symbol to the in-flight list.
(ctf_link_shuffle_syms): New (no longer a do-nothing stub), turn the
in-flight list into a mapping we can use, now its names are
resolvable in the external strtab.
* ctf-string.c (ctf_str_rollback_atom): Don't roll back atoms with
external strtab offsets.
(ctf_str_rollback): Adjust comment.
(ctf_str_write_strtab): Migrate ctf_syn_ext_strtab population from
writeout time...
(ctf_str_add_external): ... to string addition time.
* ctf-lookup.c (ctf_lookup_var_key_t): Rename to...
(ctf_lookup_idx_key_t): ... this, now we use it for syms too.
<clik_names>: New member, a name table.
(ctf_lookup_var): Adjust accordingly.
(ctf_lookup_variable): Likewise.
(ctf_lookup_by_id): Shuffle further up in the file.
(ctf_symidx_sort_arg_cb): New, callback for...
(sort_symidx_by_name): ... this new function to sort a symidx
found to be unsorted (likely originating from the compiler).
(ctf_symidx_sort): New, sort a symidx.
(ctf_lookup_symbol_name): Support dynamic symbols with indexes
provided by the linker. Use ctf_link_sym_t, not Elf64_Sym.
Check the parent if a child lookup fails.
(ctf_lookup_by_symbol): Likewise. Work for function symbols too.
(ctf_symbol_next): New, iterate over symbols with types (without
sorting).
(ctf_lookup_idx_name): New, bsearch for symbol names in indexes.
(ctf_try_lookup_indexed): New, attempt an indexed lookup.
(ctf_func_info): Reimplement in terms of ctf_lookup_by_symbol.
(ctf_func_args): Likewise.
(ctf_get_dict): Move...
* ctf-types.c (ctf_get_dict): ... here.
* ctf-util.c (ctf_sym_to_elf64): Re-express as...
(ctf_elf64_to_link_sym): ... this. Add new st_symidx field, and
st_nameidx_set (always 0, so st_nameidx can be ignored). Look in
the ELF strtab for names.
(ctf_elf32_to_link_sym): Likewise, for Elf32_Sym.
(ctf_next_destroy): Destroy ctf_next_t.u.ctn_next if need be.
* libctf.ver: Add ctf_symbol_next, ctf_add_objt_sym and
ctf_add_func_sym.
2020-11-20 21:34:04 +08:00
|
|
|
int
|
|
|
|
ctf_dynhash_sort_by_name (const ctf_next_hkv_t *one, const ctf_next_hkv_t *two,
|
|
|
|
void *unused _libctf_unused_)
|
|
|
|
{
|
|
|
|
return strcmp ((char *) one->hkv_key, (char *) two->hkv_key);
|
|
|
|
}
|
|
|
|
|
2020-06-03 23:36:18 +08:00
|
|
|
/* Traverse a sorted dynhash, in _next iterator form.
|
|
|
|
|
|
|
|
See ctf_dynhash_next for notes on error returns, etc.
|
|
|
|
|
|
|
|
Sort keys before iterating over them using the SORT_FUN and SORT_ARG.
|
|
|
|
|
|
|
|
If SORT_FUN is null, thunks to ctf_dynhash_next. */
|
|
|
|
int
|
|
|
|
ctf_dynhash_next_sorted (ctf_dynhash_t *h, ctf_next_t **it, void **key,
|
|
|
|
void **value, ctf_hash_sort_f sort_fun, void *sort_arg)
|
|
|
|
{
|
|
|
|
ctf_next_t *i = *it;
|
|
|
|
|
|
|
|
if (sort_fun == NULL)
|
|
|
|
return ctf_dynhash_next (h, it, key, value);
|
|
|
|
|
|
|
|
if (!i)
|
|
|
|
{
|
|
|
|
size_t els = ctf_dynhash_elements (h);
|
|
|
|
ctf_next_t *accum_i = NULL;
|
|
|
|
void *key, *value;
|
|
|
|
int err;
|
|
|
|
ctf_next_hkv_t *walk;
|
|
|
|
|
|
|
|
if (((ssize_t) els) < 0)
|
|
|
|
return EDOM;
|
|
|
|
|
|
|
|
if ((i = ctf_next_create ()) == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
if ((i->u.ctn_sorted_hkv = calloc (els, sizeof (ctf_next_hkv_t))) == NULL)
|
|
|
|
{
|
|
|
|
ctf_next_destroy (i);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
walk = i->u.ctn_sorted_hkv;
|
|
|
|
|
|
|
|
i->cu.ctn_h = h;
|
|
|
|
|
|
|
|
while ((err = ctf_dynhash_next (h, &accum_i, &key, &value)) == 0)
|
|
|
|
{
|
|
|
|
walk->hkv_key = key;
|
|
|
|
walk->hkv_value = value;
|
|
|
|
walk++;
|
|
|
|
}
|
|
|
|
if (err != ECTF_NEXT_END)
|
|
|
|
{
|
|
|
|
ctf_next_destroy (i);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sort_fun)
|
|
|
|
ctf_qsort_r (i->u.ctn_sorted_hkv, els, sizeof (ctf_next_hkv_t),
|
|
|
|
(int (*) (const void *, const void *, void *)) sort_fun,
|
|
|
|
sort_arg);
|
|
|
|
i->ctn_n = 0;
|
|
|
|
i->ctn_size = (ssize_t) els;
|
|
|
|
i->ctn_iter_fun = (void (*) (void)) ctf_dynhash_next_sorted;
|
|
|
|
*it = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((void (*) (void)) ctf_dynhash_next_sorted != i->ctn_iter_fun)
|
|
|
|
return ECTF_NEXT_WRONGFUN;
|
|
|
|
|
|
|
|
if (h != i->cu.ctn_h)
|
|
|
|
return ECTF_NEXT_WRONGFP;
|
|
|
|
|
|
|
|
if ((ssize_t) i->ctn_n == i->ctn_size)
|
|
|
|
{
|
|
|
|
ctf_next_destroy (i);
|
|
|
|
*it = NULL;
|
|
|
|
return ECTF_NEXT_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key)
|
|
|
|
*key = i->u.ctn_sorted_hkv[i->ctn_n].hkv_key;
|
|
|
|
if (value)
|
|
|
|
*value = i->u.ctn_sorted_hkv[i->ctn_n].hkv_value;
|
|
|
|
i->ctn_n++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-24 05:12:16 +08:00
|
|
|
void
|
|
|
|
ctf_dynhash_destroy (ctf_dynhash_t *hp)
|
|
|
|
{
|
|
|
|
if (hp != NULL)
|
|
|
|
htab_delete (hp->htab);
|
|
|
|
free (hp);
|
|
|
|
}
|
|
|
|
|
libctf, hash: introduce the ctf_dynset
There are many places in the deduplicator which use hashtables as tiny
sets: keys with no value (and usually, but not always, no freeing
function) often with only one or a few members. For each of these, even
after the last change to not store the freeing functions, we are storing
a little malloced block for each item just to track the key/value pair,
and a little malloced block for the hash table itself just to track the
freeing function because we can't use libiberty hashtab's freeing
function because we are using that to free the little malloced per-item
block.
If we only have a key, we don't need any of that: we can ditch the
per-malloced block because we don't have a value, and we can ditch the
per-hashtab structure because we don't need to independently track the
freeing functions since libiberty hashtab is doing it for us. That
means we don't need an owner field in the (now nonexistent) item block
either.
Roughly speaking, this datatype saves about 25% in time and 20% in peak
memory usage for normal links, even fairly big ones. So this might seem
redundant, but it's really worth it.
Instead of a _lookup function, a dynset has two distinct functions:
ctf_dynset_exists, which returns true or false and an optional pointer
to the set member, and ctf_dynhash_lookup_any, which is used if all
members of the set are expected to be equivalent and we just want *any*
member and we don't care which one.
There is no iterator in this set of functions, not because we don't
iterate over dynset members -- we do, a lot -- but because the iterator
here is a member of an entirely new family of much more convenient
iteration functions, introduced in the next commit.
libctf/
* ctf-hash.c (ctf_dynset_eq_string): New.
(ctf_dynset_create): New.
(DYNSET_EMPTY_ENTRY_REPLACEMENT): New.
(DYNSET_DELETED_ENTRY_REPLACEMENT): New.
(key_to_internal): New.
(internal_to_key): New.
(ctf_dynset_insert): New.
(ctf_dynset_remove): New.
(ctf_dynset_destroy): New.
(ctf_dynset_lookup): New.
(ctf_dynset_exists): New.
(ctf_dynset_lookup_any): New.
(ctf_hash_insert_type): Coding style.
(ctf_hash_define_type): Likewise.
* ctf-impl.h (ctf_dynset_t): New.
(ctf_dynset_eq_string): New.
(ctf_dynset_create): New.
(ctf_dynset_insert): New.
(ctf_dynset_remove): New.
(ctf_dynset_destroy): New.
(ctf_dynset_lookup): New.
(ctf_dynset_exists): New.
(ctf_dynset_lookup_any): New.
* ctf-inlines.h (ctf_dynset_cinsert): New.
2020-06-03 05:26:38 +08:00
|
|
|
/* The dynset, used for sets of keys with no value. The implementation of this
|
|
|
|
can be much simpler, because without a value the slot can simply be the
|
|
|
|
stored key, which means we don't need to store the freeing functions and the
|
|
|
|
dynset itself is just a htab. */
|
|
|
|
|
|
|
|
ctf_dynset_t *
|
|
|
|
ctf_dynset_create (htab_hash hash_fun, htab_eq eq_fun,
|
|
|
|
ctf_hash_free_fun key_free)
|
|
|
|
{
|
|
|
|
/* 7 is arbitrary and untested for now. */
|
|
|
|
return (ctf_dynset_t *) htab_create_alloc (7, (htab_hash) hash_fun, eq_fun,
|
|
|
|
key_free, xcalloc, free);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The dynset has one complexity: the underlying implementation reserves two
|
|
|
|
values for internal hash table implementation details (empty versus deleted
|
|
|
|
entries). These values are otherwise very useful for pointers cast to ints,
|
|
|
|
so transform the ctf_dynset_inserted value to allow for it. (This
|
|
|
|
introduces an ambiguity in that one can no longer store these two values in
|
|
|
|
the dynset, but if we pick high enough values this is very unlikely to be a
|
|
|
|
problem.)
|
|
|
|
|
|
|
|
We leak this implementation detail to the freeing functions on the grounds
|
|
|
|
that any use of these functions is overwhelmingly likely to be in sets using
|
|
|
|
real pointers, which will be unaffected. */
|
|
|
|
|
|
|
|
#define DYNSET_EMPTY_ENTRY_REPLACEMENT ((void *) (uintptr_t) -64)
|
|
|
|
#define DYNSET_DELETED_ENTRY_REPLACEMENT ((void *) (uintptr_t) -63)
|
|
|
|
|
|
|
|
static void *
|
|
|
|
key_to_internal (const void *key)
|
|
|
|
{
|
|
|
|
if (key == HTAB_EMPTY_ENTRY)
|
|
|
|
return DYNSET_EMPTY_ENTRY_REPLACEMENT;
|
|
|
|
else if (key == HTAB_DELETED_ENTRY)
|
|
|
|
return DYNSET_DELETED_ENTRY_REPLACEMENT;
|
|
|
|
|
|
|
|
return (void *) key;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
internal_to_key (const void *internal)
|
|
|
|
{
|
|
|
|
if (internal == DYNSET_EMPTY_ENTRY_REPLACEMENT)
|
|
|
|
return HTAB_EMPTY_ENTRY;
|
|
|
|
else if (internal == DYNSET_DELETED_ENTRY_REPLACEMENT)
|
|
|
|
return HTAB_DELETED_ENTRY;
|
|
|
|
return (void *) internal;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ctf_dynset_insert (ctf_dynset_t *hp, void *key)
|
|
|
|
{
|
|
|
|
struct htab *htab = (struct htab *) hp;
|
|
|
|
void **slot;
|
|
|
|
|
2024-07-16 03:50:25 +08:00
|
|
|
slot = htab_find_slot (htab, key_to_internal (key), INSERT);
|
libctf, hash: introduce the ctf_dynset
There are many places in the deduplicator which use hashtables as tiny
sets: keys with no value (and usually, but not always, no freeing
function) often with only one or a few members. For each of these, even
after the last change to not store the freeing functions, we are storing
a little malloced block for each item just to track the key/value pair,
and a little malloced block for the hash table itself just to track the
freeing function because we can't use libiberty hashtab's freeing
function because we are using that to free the little malloced per-item
block.
If we only have a key, we don't need any of that: we can ditch the
per-malloced block because we don't have a value, and we can ditch the
per-hashtab structure because we don't need to independently track the
freeing functions since libiberty hashtab is doing it for us. That
means we don't need an owner field in the (now nonexistent) item block
either.
Roughly speaking, this datatype saves about 25% in time and 20% in peak
memory usage for normal links, even fairly big ones. So this might seem
redundant, but it's really worth it.
Instead of a _lookup function, a dynset has two distinct functions:
ctf_dynset_exists, which returns true or false and an optional pointer
to the set member, and ctf_dynhash_lookup_any, which is used if all
members of the set are expected to be equivalent and we just want *any*
member and we don't care which one.
There is no iterator in this set of functions, not because we don't
iterate over dynset members -- we do, a lot -- but because the iterator
here is a member of an entirely new family of much more convenient
iteration functions, introduced in the next commit.
libctf/
* ctf-hash.c (ctf_dynset_eq_string): New.
(ctf_dynset_create): New.
(DYNSET_EMPTY_ENTRY_REPLACEMENT): New.
(DYNSET_DELETED_ENTRY_REPLACEMENT): New.
(key_to_internal): New.
(internal_to_key): New.
(ctf_dynset_insert): New.
(ctf_dynset_remove): New.
(ctf_dynset_destroy): New.
(ctf_dynset_lookup): New.
(ctf_dynset_exists): New.
(ctf_dynset_lookup_any): New.
(ctf_hash_insert_type): Coding style.
(ctf_hash_define_type): Likewise.
* ctf-impl.h (ctf_dynset_t): New.
(ctf_dynset_eq_string): New.
(ctf_dynset_create): New.
(ctf_dynset_insert): New.
(ctf_dynset_remove): New.
(ctf_dynset_destroy): New.
(ctf_dynset_lookup): New.
(ctf_dynset_exists): New.
(ctf_dynset_lookup_any): New.
* ctf-inlines.h (ctf_dynset_cinsert): New.
2020-06-03 05:26:38 +08:00
|
|
|
|
|
|
|
if (!slot)
|
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*slot)
|
|
|
|
{
|
|
|
|
if (htab->del_f)
|
|
|
|
(*htab->del_f) (*slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
*slot = key_to_internal (key);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ctf_dynset_remove (ctf_dynset_t *hp, const void *key)
|
|
|
|
{
|
|
|
|
htab_remove_elt ((struct htab *) hp, key_to_internal (key));
|
|
|
|
}
|
|
|
|
|
libctf: prohibit addition of enums with overlapping enumerator constants
libctf has long prohibited addition of enums with overlapping constants in a
single enum, but now that we are properly considering enums with overlapping
constants to be conflciting types, we can go further and prohibit addition
of enumeration constants to a dict if they already exist in any enum in that
dict: the same rules as C itself.
We do this in a fashion vaguely similar to what we just did in the
deduplicator, by considering enumeration constants as identifiers and adding
them to the core type/identifier namespace, ctf_dict_t.ctf_names. This is a
little fiddly, because we do not want to prohibit opening of existing dicts
into which the deduplicator has stuffed enums with overlapping constants!
We just want to prohibit the addition of *new* enumerators that violate that
rule. Even then, it's fine to add overlapping enumerator constants as long
as at least one of them is in a non-root type. (This is essential for
proper deduplicator operation in cu-mapped mode, where multiple compilation
units can be smashed into one dict, with conflicting types marked as
hidden: these types may well contain overlapping enumerators.)
So, at open time, keep track of all enums observed, then do a third pass
through the enums alone, adding each enumerator either to the ctf_names
table as a mapping from the enumerator name to the enum it is part of (if
not already present), or to a new ctf_conflicting_enums hashtable that
tracks observed duplicates. (The latter is not used yet, but will be soon.)
(We need to do a third pass because it's quite possible to have an enum
containing an enumerator FOO followed by a type FOO: since they're processed
in order, the enumerator would be processed before the type, and at that
stage it seems nonconflicting. The easiest fix is to run through the
enumerators after all type names are interned.)
At ctf_add_enumerator time, if the enumerator to which we are adding a type
is root-visible, check for an already-present name and error out if found,
then intern the new name in the ctf_names table as is done at open time.
(We retain the existing code which scans the enum itself for duplicates
because it is still an error to add an enumerator twice to a
non-root-visible enum type; but we only need to do this if the enum is
non-root-visible, so the cost of enum addition is reduced.)
Tested in an upcoming commit.
libctf/
* ctf-impl.h (ctf_dict_t) <ctf_names>: Augment comment.
<ctf_conflicting_enums>: New.
(ctf_dynset_elements): New.
* ctf-hash.c (ctf_dynset_elements): Implement it.
* ctf-open.c (init_static_types): Split body into...
(init_static_types_internal): ... here. Count enumerators;
keep track of observed enums in pass 2; populate ctf_names and
ctf_conflicting_enums with enumerators in a third pass.
(ctf_dict_close): Free ctf_conflicting_enums.
* ctf-create.c (ctf_add_enumerator): Prohibit addition of duplicate
enumerators in root-visible enum types.
include/
* ctf-api.h (CTF_ADD_NONROOT): Describe what non-rootness
means for enumeration constants.
(ctf_add_enumerator): The name is not a misnomer.
We now require that enumerators have unique names.
Document the non-rootness of enumerators.
2024-06-12 03:33:03 +08:00
|
|
|
size_t
|
|
|
|
ctf_dynset_elements (ctf_dynset_t *hp)
|
|
|
|
{
|
|
|
|
return htab_elements ((struct htab *) hp);
|
|
|
|
}
|
|
|
|
|
libctf, hash: introduce the ctf_dynset
There are many places in the deduplicator which use hashtables as tiny
sets: keys with no value (and usually, but not always, no freeing
function) often with only one or a few members. For each of these, even
after the last change to not store the freeing functions, we are storing
a little malloced block for each item just to track the key/value pair,
and a little malloced block for the hash table itself just to track the
freeing function because we can't use libiberty hashtab's freeing
function because we are using that to free the little malloced per-item
block.
If we only have a key, we don't need any of that: we can ditch the
per-malloced block because we don't have a value, and we can ditch the
per-hashtab structure because we don't need to independently track the
freeing functions since libiberty hashtab is doing it for us. That
means we don't need an owner field in the (now nonexistent) item block
either.
Roughly speaking, this datatype saves about 25% in time and 20% in peak
memory usage for normal links, even fairly big ones. So this might seem
redundant, but it's really worth it.
Instead of a _lookup function, a dynset has two distinct functions:
ctf_dynset_exists, which returns true or false and an optional pointer
to the set member, and ctf_dynhash_lookup_any, which is used if all
members of the set are expected to be equivalent and we just want *any*
member and we don't care which one.
There is no iterator in this set of functions, not because we don't
iterate over dynset members -- we do, a lot -- but because the iterator
here is a member of an entirely new family of much more convenient
iteration functions, introduced in the next commit.
libctf/
* ctf-hash.c (ctf_dynset_eq_string): New.
(ctf_dynset_create): New.
(DYNSET_EMPTY_ENTRY_REPLACEMENT): New.
(DYNSET_DELETED_ENTRY_REPLACEMENT): New.
(key_to_internal): New.
(internal_to_key): New.
(ctf_dynset_insert): New.
(ctf_dynset_remove): New.
(ctf_dynset_destroy): New.
(ctf_dynset_lookup): New.
(ctf_dynset_exists): New.
(ctf_dynset_lookup_any): New.
(ctf_hash_insert_type): Coding style.
(ctf_hash_define_type): Likewise.
* ctf-impl.h (ctf_dynset_t): New.
(ctf_dynset_eq_string): New.
(ctf_dynset_create): New.
(ctf_dynset_insert): New.
(ctf_dynset_remove): New.
(ctf_dynset_destroy): New.
(ctf_dynset_lookup): New.
(ctf_dynset_exists): New.
(ctf_dynset_lookup_any): New.
* ctf-inlines.h (ctf_dynset_cinsert): New.
2020-06-03 05:26:38 +08:00
|
|
|
void
|
|
|
|
ctf_dynset_destroy (ctf_dynset_t *hp)
|
|
|
|
{
|
|
|
|
if (hp != NULL)
|
|
|
|
htab_delete ((struct htab *) hp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
ctf_dynset_lookup (ctf_dynset_t *hp, const void *key)
|
|
|
|
{
|
|
|
|
void **slot = htab_find_slot ((struct htab *) hp,
|
|
|
|
key_to_internal (key), NO_INSERT);
|
|
|
|
|
|
|
|
if (slot)
|
|
|
|
return internal_to_key (*slot);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TRUE/FALSE return. */
|
|
|
|
int
|
|
|
|
ctf_dynset_exists (ctf_dynset_t *hp, const void *key, const void **orig_key)
|
|
|
|
{
|
|
|
|
void **slot = htab_find_slot ((struct htab *) hp,
|
|
|
|
key_to_internal (key), NO_INSERT);
|
|
|
|
|
|
|
|
if (orig_key && slot)
|
|
|
|
*orig_key = internal_to_key (*slot);
|
|
|
|
return (slot != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look up a completely random value from the set, if any exist.
|
|
|
|
Keys with value zero cannot be distinguished from a nonexistent key. */
|
|
|
|
void *
|
|
|
|
ctf_dynset_lookup_any (ctf_dynset_t *hp)
|
|
|
|
{
|
|
|
|
struct htab *htab = (struct htab *) hp;
|
|
|
|
void **slot = htab->entries;
|
|
|
|
void **limit = slot + htab_size (htab);
|
|
|
|
|
|
|
|
while (slot < limit
|
|
|
|
&& (*slot == HTAB_EMPTY_ENTRY || *slot == HTAB_DELETED_ENTRY))
|
|
|
|
slot++;
|
|
|
|
|
|
|
|
if (slot < limit)
|
|
|
|
return internal_to_key (*slot);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-06-03 23:36:18 +08:00
|
|
|
/* Traverse a dynset in arbitrary order, in _next iterator form.
|
|
|
|
|
|
|
|
Otherwise, just like ctf_dynhash_next. */
|
|
|
|
int
|
|
|
|
ctf_dynset_next (ctf_dynset_t *hp, ctf_next_t **it, void **key)
|
|
|
|
{
|
|
|
|
struct htab *htab = (struct htab *) hp;
|
|
|
|
ctf_next_t *i = *it;
|
|
|
|
void *slot;
|
|
|
|
|
|
|
|
if (!i)
|
|
|
|
{
|
|
|
|
size_t size = htab_size (htab);
|
|
|
|
|
|
|
|
/* If the table has too many entries to fit in an ssize_t, just give up.
|
|
|
|
This might be spurious, but if any type-related hashtable has ever been
|
|
|
|
nearly as large as that then somthing very odd is going on. */
|
|
|
|
|
|
|
|
if (((ssize_t) size) < 0)
|
|
|
|
return EDOM;
|
|
|
|
|
|
|
|
if ((i = ctf_next_create ()) == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
i->u.ctn_hash_slot = htab->entries;
|
|
|
|
i->cu.ctn_s = hp;
|
|
|
|
i->ctn_n = 0;
|
|
|
|
i->ctn_size = (ssize_t) size;
|
|
|
|
i->ctn_iter_fun = (void (*) (void)) ctf_dynset_next;
|
|
|
|
*it = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((void (*) (void)) ctf_dynset_next != i->ctn_iter_fun)
|
|
|
|
return ECTF_NEXT_WRONGFUN;
|
|
|
|
|
|
|
|
if (hp != i->cu.ctn_s)
|
|
|
|
return ECTF_NEXT_WRONGFP;
|
|
|
|
|
|
|
|
if ((ssize_t) i->ctn_n == i->ctn_size)
|
|
|
|
goto set_end;
|
|
|
|
|
|
|
|
while ((ssize_t) i->ctn_n < i->ctn_size
|
|
|
|
&& (*i->u.ctn_hash_slot == HTAB_EMPTY_ENTRY
|
|
|
|
|| *i->u.ctn_hash_slot == HTAB_DELETED_ENTRY))
|
|
|
|
{
|
|
|
|
i->u.ctn_hash_slot++;
|
|
|
|
i->ctn_n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ssize_t) i->ctn_n == i->ctn_size)
|
|
|
|
goto set_end;
|
|
|
|
|
|
|
|
slot = *i->u.ctn_hash_slot;
|
|
|
|
|
|
|
|
if (key)
|
|
|
|
*key = internal_to_key (slot);
|
|
|
|
|
|
|
|
i->u.ctn_hash_slot++;
|
|
|
|
i->ctn_n++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
set_end:
|
|
|
|
ctf_next_destroy (i);
|
|
|
|
*it = NULL;
|
|
|
|
return ECTF_NEXT_END;
|
|
|
|
}
|
|
|
|
|
libctf: remove static/dynamic name lookup distinction
libctf internally maintains a set of hash tables for type name lookups,
one for each valid C type namespace (struct, union, enum, and everything
else).
Or, rather, it maintains *two* sets of hash tables: one, a ctf_hash *,
is meant for lookups in ctf_(buf)open()ed dicts with fixed content; the
other, a ctf_dynhash *, is meant for lookups in ctf_create()d dicts.
This distinction was somewhat valuable in the far pre-binutils past when
two different hashtable implementations were used (one expanding, the
other fixed-size), but those days are long gone: the hash table
implementations are almost identical, both wrappers around the libiberty
hashtab. The ctf_dynhash has many more capabilities than the ctf_hash
(iteration, deletion, etc etc) and has no downsides other than starting
at a fixed, arbitrary small size.
That limitation is easy to lift (via a new ctf_dynhash_create_sized()),
following which we can throw away nearly all the ctf_hash
implementation, and all the code to choose between readable and writable
hashtabs; the few convenience functions that are still useful (for
insertion of name -> type mappings) can also be generalized a bit so
that the extra string verification they do is potentially available to
other string lookups as well.
(libctf still has two hashtable implementations, ctf_dynhash, above,
and ctf_dynset, which is a key-only hashtab that can avoid a great many
malloc()s, used for high-volume applications in the deduplicator.)
libctf/
* ctf-create.c (ctf_create): Eliminate ctn_writable.
(ctf_dtd_insert): Likewise.
(ctf_dtd_delete): Likewise.
(ctf_rollback): Likewise.
(ctf_name_table): Eliminate ctf_names_t.
* ctf-hash.c (ctf_dynhash_create): Comment update.
Reimplement in terms of...
(ctf_dynhash_create_sized): ... this new function.
(ctf_hash_create): Remove.
(ctf_hash_size): Remove.
(ctf_hash_define_type): Remove.
(ctf_hash_destroy): Remove.
(ctf_hash_lookup_type): Rename to...
(ctf_dynhash_lookup_type): ... this.
(ctf_hash_insert_type): Rename to...
(ctf_dynhash_insert_type): ... this, moving validation to...
* ctf-string.c (ctf_strptr_validate): ... this new function.
* ctf-impl.h (struct ctf_names): Extirpate.
(struct ctf_lookup.ctl_hash): Now a ctf_dynhash_t.
(struct ctf_dict): All ctf_names_t fields are now ctf_dynhash_t.
(ctf_name_table): Now returns a ctf_dynhash_t.
(ctf_lookup_by_rawhash): Remove.
(ctf_hash_create): Likewise.
(ctf_hash_insert_type): Likewise.
(ctf_hash_define_type): Likewise.
(ctf_hash_lookup_type): Likewise.
(ctf_hash_size): Likewise.
(ctf_hash_destroy): Likewise.
(ctf_dynhash_create_sized): New.
(ctf_dynhash_insert_type): New.
(ctf_dynhash_lookup_type): New.
(ctf_strptr_validate): New.
* ctf-lookup.c (ctf_lookup_by_name_internal): Adapt.
* ctf-open.c (init_types): Adapt.
(ctf_set_ctl_hashes): Adapt.
(ctf_dict_close): Adapt.
* ctf-serialize.c (ctf_serialize): Adapt.
* ctf-types.c (ctf_lookup_by_rawhash): Remove.
2023-12-19 01:47:48 +08:00
|
|
|
/* Helper functions for insertion/removal of types. */
|
2019-04-24 05:12:16 +08:00
|
|
|
|
|
|
|
int
|
libctf: remove static/dynamic name lookup distinction
libctf internally maintains a set of hash tables for type name lookups,
one for each valid C type namespace (struct, union, enum, and everything
else).
Or, rather, it maintains *two* sets of hash tables: one, a ctf_hash *,
is meant for lookups in ctf_(buf)open()ed dicts with fixed content; the
other, a ctf_dynhash *, is meant for lookups in ctf_create()d dicts.
This distinction was somewhat valuable in the far pre-binutils past when
two different hashtable implementations were used (one expanding, the
other fixed-size), but those days are long gone: the hash table
implementations are almost identical, both wrappers around the libiberty
hashtab. The ctf_dynhash has many more capabilities than the ctf_hash
(iteration, deletion, etc etc) and has no downsides other than starting
at a fixed, arbitrary small size.
That limitation is easy to lift (via a new ctf_dynhash_create_sized()),
following which we can throw away nearly all the ctf_hash
implementation, and all the code to choose between readable and writable
hashtabs; the few convenience functions that are still useful (for
insertion of name -> type mappings) can also be generalized a bit so
that the extra string verification they do is potentially available to
other string lookups as well.
(libctf still has two hashtable implementations, ctf_dynhash, above,
and ctf_dynset, which is a key-only hashtab that can avoid a great many
malloc()s, used for high-volume applications in the deduplicator.)
libctf/
* ctf-create.c (ctf_create): Eliminate ctn_writable.
(ctf_dtd_insert): Likewise.
(ctf_dtd_delete): Likewise.
(ctf_rollback): Likewise.
(ctf_name_table): Eliminate ctf_names_t.
* ctf-hash.c (ctf_dynhash_create): Comment update.
Reimplement in terms of...
(ctf_dynhash_create_sized): ... this new function.
(ctf_hash_create): Remove.
(ctf_hash_size): Remove.
(ctf_hash_define_type): Remove.
(ctf_hash_destroy): Remove.
(ctf_hash_lookup_type): Rename to...
(ctf_dynhash_lookup_type): ... this.
(ctf_hash_insert_type): Rename to...
(ctf_dynhash_insert_type): ... this, moving validation to...
* ctf-string.c (ctf_strptr_validate): ... this new function.
* ctf-impl.h (struct ctf_names): Extirpate.
(struct ctf_lookup.ctl_hash): Now a ctf_dynhash_t.
(struct ctf_dict): All ctf_names_t fields are now ctf_dynhash_t.
(ctf_name_table): Now returns a ctf_dynhash_t.
(ctf_lookup_by_rawhash): Remove.
(ctf_hash_create): Likewise.
(ctf_hash_insert_type): Likewise.
(ctf_hash_define_type): Likewise.
(ctf_hash_lookup_type): Likewise.
(ctf_hash_size): Likewise.
(ctf_hash_destroy): Likewise.
(ctf_dynhash_create_sized): New.
(ctf_dynhash_insert_type): New.
(ctf_dynhash_lookup_type): New.
(ctf_strptr_validate): New.
* ctf-lookup.c (ctf_lookup_by_name_internal): Adapt.
* ctf-open.c (init_types): Adapt.
(ctf_set_ctl_hashes): Adapt.
(ctf_dict_close): Adapt.
* ctf-serialize.c (ctf_serialize): Adapt.
* ctf-types.c (ctf_lookup_by_rawhash): Remove.
2023-12-19 01:47:48 +08:00
|
|
|
ctf_dynhash_insert_type (ctf_dict_t *fp, ctf_dynhash_t *hp, uint32_t type,
|
|
|
|
uint32_t name)
|
2019-04-24 05:12:16 +08:00
|
|
|
{
|
libctf: remove static/dynamic name lookup distinction
libctf internally maintains a set of hash tables for type name lookups,
one for each valid C type namespace (struct, union, enum, and everything
else).
Or, rather, it maintains *two* sets of hash tables: one, a ctf_hash *,
is meant for lookups in ctf_(buf)open()ed dicts with fixed content; the
other, a ctf_dynhash *, is meant for lookups in ctf_create()d dicts.
This distinction was somewhat valuable in the far pre-binutils past when
two different hashtable implementations were used (one expanding, the
other fixed-size), but those days are long gone: the hash table
implementations are almost identical, both wrappers around the libiberty
hashtab. The ctf_dynhash has many more capabilities than the ctf_hash
(iteration, deletion, etc etc) and has no downsides other than starting
at a fixed, arbitrary small size.
That limitation is easy to lift (via a new ctf_dynhash_create_sized()),
following which we can throw away nearly all the ctf_hash
implementation, and all the code to choose between readable and writable
hashtabs; the few convenience functions that are still useful (for
insertion of name -> type mappings) can also be generalized a bit so
that the extra string verification they do is potentially available to
other string lookups as well.
(libctf still has two hashtable implementations, ctf_dynhash, above,
and ctf_dynset, which is a key-only hashtab that can avoid a great many
malloc()s, used for high-volume applications in the deduplicator.)
libctf/
* ctf-create.c (ctf_create): Eliminate ctn_writable.
(ctf_dtd_insert): Likewise.
(ctf_dtd_delete): Likewise.
(ctf_rollback): Likewise.
(ctf_name_table): Eliminate ctf_names_t.
* ctf-hash.c (ctf_dynhash_create): Comment update.
Reimplement in terms of...
(ctf_dynhash_create_sized): ... this new function.
(ctf_hash_create): Remove.
(ctf_hash_size): Remove.
(ctf_hash_define_type): Remove.
(ctf_hash_destroy): Remove.
(ctf_hash_lookup_type): Rename to...
(ctf_dynhash_lookup_type): ... this.
(ctf_hash_insert_type): Rename to...
(ctf_dynhash_insert_type): ... this, moving validation to...
* ctf-string.c (ctf_strptr_validate): ... this new function.
* ctf-impl.h (struct ctf_names): Extirpate.
(struct ctf_lookup.ctl_hash): Now a ctf_dynhash_t.
(struct ctf_dict): All ctf_names_t fields are now ctf_dynhash_t.
(ctf_name_table): Now returns a ctf_dynhash_t.
(ctf_lookup_by_rawhash): Remove.
(ctf_hash_create): Likewise.
(ctf_hash_insert_type): Likewise.
(ctf_hash_define_type): Likewise.
(ctf_hash_lookup_type): Likewise.
(ctf_hash_size): Likewise.
(ctf_hash_destroy): Likewise.
(ctf_dynhash_create_sized): New.
(ctf_dynhash_insert_type): New.
(ctf_dynhash_lookup_type): New.
(ctf_strptr_validate): New.
* ctf-lookup.c (ctf_lookup_by_name_internal): Adapt.
* ctf-open.c (init_types): Adapt.
(ctf_set_ctl_hashes): Adapt.
(ctf_dict_close): Adapt.
* ctf-serialize.c (ctf_serialize): Adapt.
* ctf-types.c (ctf_lookup_by_rawhash): Remove.
2023-12-19 01:47:48 +08:00
|
|
|
const char *str;
|
|
|
|
int err;
|
2019-04-24 05:12:16 +08:00
|
|
|
|
|
|
|
if (type == 0)
|
|
|
|
return EINVAL;
|
|
|
|
|
libctf: remove static/dynamic name lookup distinction
libctf internally maintains a set of hash tables for type name lookups,
one for each valid C type namespace (struct, union, enum, and everything
else).
Or, rather, it maintains *two* sets of hash tables: one, a ctf_hash *,
is meant for lookups in ctf_(buf)open()ed dicts with fixed content; the
other, a ctf_dynhash *, is meant for lookups in ctf_create()d dicts.
This distinction was somewhat valuable in the far pre-binutils past when
two different hashtable implementations were used (one expanding, the
other fixed-size), but those days are long gone: the hash table
implementations are almost identical, both wrappers around the libiberty
hashtab. The ctf_dynhash has many more capabilities than the ctf_hash
(iteration, deletion, etc etc) and has no downsides other than starting
at a fixed, arbitrary small size.
That limitation is easy to lift (via a new ctf_dynhash_create_sized()),
following which we can throw away nearly all the ctf_hash
implementation, and all the code to choose between readable and writable
hashtabs; the few convenience functions that are still useful (for
insertion of name -> type mappings) can also be generalized a bit so
that the extra string verification they do is potentially available to
other string lookups as well.
(libctf still has two hashtable implementations, ctf_dynhash, above,
and ctf_dynset, which is a key-only hashtab that can avoid a great many
malloc()s, used for high-volume applications in the deduplicator.)
libctf/
* ctf-create.c (ctf_create): Eliminate ctn_writable.
(ctf_dtd_insert): Likewise.
(ctf_dtd_delete): Likewise.
(ctf_rollback): Likewise.
(ctf_name_table): Eliminate ctf_names_t.
* ctf-hash.c (ctf_dynhash_create): Comment update.
Reimplement in terms of...
(ctf_dynhash_create_sized): ... this new function.
(ctf_hash_create): Remove.
(ctf_hash_size): Remove.
(ctf_hash_define_type): Remove.
(ctf_hash_destroy): Remove.
(ctf_hash_lookup_type): Rename to...
(ctf_dynhash_lookup_type): ... this.
(ctf_hash_insert_type): Rename to...
(ctf_dynhash_insert_type): ... this, moving validation to...
* ctf-string.c (ctf_strptr_validate): ... this new function.
* ctf-impl.h (struct ctf_names): Extirpate.
(struct ctf_lookup.ctl_hash): Now a ctf_dynhash_t.
(struct ctf_dict): All ctf_names_t fields are now ctf_dynhash_t.
(ctf_name_table): Now returns a ctf_dynhash_t.
(ctf_lookup_by_rawhash): Remove.
(ctf_hash_create): Likewise.
(ctf_hash_insert_type): Likewise.
(ctf_hash_define_type): Likewise.
(ctf_hash_lookup_type): Likewise.
(ctf_hash_size): Likewise.
(ctf_hash_destroy): Likewise.
(ctf_dynhash_create_sized): New.
(ctf_dynhash_insert_type): New.
(ctf_dynhash_lookup_type): New.
(ctf_strptr_validate): New.
* ctf-lookup.c (ctf_lookup_by_name_internal): Adapt.
* ctf-open.c (init_types): Adapt.
(ctf_set_ctl_hashes): Adapt.
(ctf_dict_close): Adapt.
* ctf-serialize.c (ctf_serialize): Adapt.
* ctf-types.c (ctf_lookup_by_rawhash): Remove.
2023-12-19 01:47:48 +08:00
|
|
|
if ((str = ctf_strptr_validate (fp, name)) == NULL)
|
|
|
|
return ctf_errno (fp);
|
2019-04-24 05:12:16 +08:00
|
|
|
|
|
|
|
if (str[0] == '\0')
|
|
|
|
return 0; /* Just ignore empty strings on behalf of caller. */
|
|
|
|
|
libctf: remove static/dynamic name lookup distinction
libctf internally maintains a set of hash tables for type name lookups,
one for each valid C type namespace (struct, union, enum, and everything
else).
Or, rather, it maintains *two* sets of hash tables: one, a ctf_hash *,
is meant for lookups in ctf_(buf)open()ed dicts with fixed content; the
other, a ctf_dynhash *, is meant for lookups in ctf_create()d dicts.
This distinction was somewhat valuable in the far pre-binutils past when
two different hashtable implementations were used (one expanding, the
other fixed-size), but those days are long gone: the hash table
implementations are almost identical, both wrappers around the libiberty
hashtab. The ctf_dynhash has many more capabilities than the ctf_hash
(iteration, deletion, etc etc) and has no downsides other than starting
at a fixed, arbitrary small size.
That limitation is easy to lift (via a new ctf_dynhash_create_sized()),
following which we can throw away nearly all the ctf_hash
implementation, and all the code to choose between readable and writable
hashtabs; the few convenience functions that are still useful (for
insertion of name -> type mappings) can also be generalized a bit so
that the extra string verification they do is potentially available to
other string lookups as well.
(libctf still has two hashtable implementations, ctf_dynhash, above,
and ctf_dynset, which is a key-only hashtab that can avoid a great many
malloc()s, used for high-volume applications in the deduplicator.)
libctf/
* ctf-create.c (ctf_create): Eliminate ctn_writable.
(ctf_dtd_insert): Likewise.
(ctf_dtd_delete): Likewise.
(ctf_rollback): Likewise.
(ctf_name_table): Eliminate ctf_names_t.
* ctf-hash.c (ctf_dynhash_create): Comment update.
Reimplement in terms of...
(ctf_dynhash_create_sized): ... this new function.
(ctf_hash_create): Remove.
(ctf_hash_size): Remove.
(ctf_hash_define_type): Remove.
(ctf_hash_destroy): Remove.
(ctf_hash_lookup_type): Rename to...
(ctf_dynhash_lookup_type): ... this.
(ctf_hash_insert_type): Rename to...
(ctf_dynhash_insert_type): ... this, moving validation to...
* ctf-string.c (ctf_strptr_validate): ... this new function.
* ctf-impl.h (struct ctf_names): Extirpate.
(struct ctf_lookup.ctl_hash): Now a ctf_dynhash_t.
(struct ctf_dict): All ctf_names_t fields are now ctf_dynhash_t.
(ctf_name_table): Now returns a ctf_dynhash_t.
(ctf_lookup_by_rawhash): Remove.
(ctf_hash_create): Likewise.
(ctf_hash_insert_type): Likewise.
(ctf_hash_define_type): Likewise.
(ctf_hash_lookup_type): Likewise.
(ctf_hash_size): Likewise.
(ctf_hash_destroy): Likewise.
(ctf_dynhash_create_sized): New.
(ctf_dynhash_insert_type): New.
(ctf_dynhash_lookup_type): New.
(ctf_strptr_validate): New.
* ctf-lookup.c (ctf_lookup_by_name_internal): Adapt.
* ctf-open.c (init_types): Adapt.
(ctf_set_ctl_hashes): Adapt.
(ctf_dict_close): Adapt.
* ctf-serialize.c (ctf_serialize): Adapt.
* ctf-types.c (ctf_lookup_by_rawhash): Remove.
2023-12-19 01:47:48 +08:00
|
|
|
if ((err = ctf_dynhash_insert (hp, (char *) str,
|
|
|
|
(void *) (ptrdiff_t) type)) == 0)
|
2019-04-24 05:12:16 +08:00
|
|
|
return 0;
|
|
|
|
|
libctf: remove static/dynamic name lookup distinction
libctf internally maintains a set of hash tables for type name lookups,
one for each valid C type namespace (struct, union, enum, and everything
else).
Or, rather, it maintains *two* sets of hash tables: one, a ctf_hash *,
is meant for lookups in ctf_(buf)open()ed dicts with fixed content; the
other, a ctf_dynhash *, is meant for lookups in ctf_create()d dicts.
This distinction was somewhat valuable in the far pre-binutils past when
two different hashtable implementations were used (one expanding, the
other fixed-size), but those days are long gone: the hash table
implementations are almost identical, both wrappers around the libiberty
hashtab. The ctf_dynhash has many more capabilities than the ctf_hash
(iteration, deletion, etc etc) and has no downsides other than starting
at a fixed, arbitrary small size.
That limitation is easy to lift (via a new ctf_dynhash_create_sized()),
following which we can throw away nearly all the ctf_hash
implementation, and all the code to choose between readable and writable
hashtabs; the few convenience functions that are still useful (for
insertion of name -> type mappings) can also be generalized a bit so
that the extra string verification they do is potentially available to
other string lookups as well.
(libctf still has two hashtable implementations, ctf_dynhash, above,
and ctf_dynset, which is a key-only hashtab that can avoid a great many
malloc()s, used for high-volume applications in the deduplicator.)
libctf/
* ctf-create.c (ctf_create): Eliminate ctn_writable.
(ctf_dtd_insert): Likewise.
(ctf_dtd_delete): Likewise.
(ctf_rollback): Likewise.
(ctf_name_table): Eliminate ctf_names_t.
* ctf-hash.c (ctf_dynhash_create): Comment update.
Reimplement in terms of...
(ctf_dynhash_create_sized): ... this new function.
(ctf_hash_create): Remove.
(ctf_hash_size): Remove.
(ctf_hash_define_type): Remove.
(ctf_hash_destroy): Remove.
(ctf_hash_lookup_type): Rename to...
(ctf_dynhash_lookup_type): ... this.
(ctf_hash_insert_type): Rename to...
(ctf_dynhash_insert_type): ... this, moving validation to...
* ctf-string.c (ctf_strptr_validate): ... this new function.
* ctf-impl.h (struct ctf_names): Extirpate.
(struct ctf_lookup.ctl_hash): Now a ctf_dynhash_t.
(struct ctf_dict): All ctf_names_t fields are now ctf_dynhash_t.
(ctf_name_table): Now returns a ctf_dynhash_t.
(ctf_lookup_by_rawhash): Remove.
(ctf_hash_create): Likewise.
(ctf_hash_insert_type): Likewise.
(ctf_hash_define_type): Likewise.
(ctf_hash_lookup_type): Likewise.
(ctf_hash_size): Likewise.
(ctf_hash_destroy): Likewise.
(ctf_dynhash_create_sized): New.
(ctf_dynhash_insert_type): New.
(ctf_dynhash_lookup_type): New.
(ctf_strptr_validate): New.
* ctf-lookup.c (ctf_lookup_by_name_internal): Adapt.
* ctf-open.c (init_types): Adapt.
(ctf_set_ctl_hashes): Adapt.
(ctf_dict_close): Adapt.
* ctf-serialize.c (ctf_serialize): Adapt.
* ctf-types.c (ctf_lookup_by_rawhash): Remove.
2023-12-19 01:47:48 +08:00
|
|
|
return err;
|
2019-04-24 05:12:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ctf_id_t
|
libctf: remove static/dynamic name lookup distinction
libctf internally maintains a set of hash tables for type name lookups,
one for each valid C type namespace (struct, union, enum, and everything
else).
Or, rather, it maintains *two* sets of hash tables: one, a ctf_hash *,
is meant for lookups in ctf_(buf)open()ed dicts with fixed content; the
other, a ctf_dynhash *, is meant for lookups in ctf_create()d dicts.
This distinction was somewhat valuable in the far pre-binutils past when
two different hashtable implementations were used (one expanding, the
other fixed-size), but those days are long gone: the hash table
implementations are almost identical, both wrappers around the libiberty
hashtab. The ctf_dynhash has many more capabilities than the ctf_hash
(iteration, deletion, etc etc) and has no downsides other than starting
at a fixed, arbitrary small size.
That limitation is easy to lift (via a new ctf_dynhash_create_sized()),
following which we can throw away nearly all the ctf_hash
implementation, and all the code to choose between readable and writable
hashtabs; the few convenience functions that are still useful (for
insertion of name -> type mappings) can also be generalized a bit so
that the extra string verification they do is potentially available to
other string lookups as well.
(libctf still has two hashtable implementations, ctf_dynhash, above,
and ctf_dynset, which is a key-only hashtab that can avoid a great many
malloc()s, used for high-volume applications in the deduplicator.)
libctf/
* ctf-create.c (ctf_create): Eliminate ctn_writable.
(ctf_dtd_insert): Likewise.
(ctf_dtd_delete): Likewise.
(ctf_rollback): Likewise.
(ctf_name_table): Eliminate ctf_names_t.
* ctf-hash.c (ctf_dynhash_create): Comment update.
Reimplement in terms of...
(ctf_dynhash_create_sized): ... this new function.
(ctf_hash_create): Remove.
(ctf_hash_size): Remove.
(ctf_hash_define_type): Remove.
(ctf_hash_destroy): Remove.
(ctf_hash_lookup_type): Rename to...
(ctf_dynhash_lookup_type): ... this.
(ctf_hash_insert_type): Rename to...
(ctf_dynhash_insert_type): ... this, moving validation to...
* ctf-string.c (ctf_strptr_validate): ... this new function.
* ctf-impl.h (struct ctf_names): Extirpate.
(struct ctf_lookup.ctl_hash): Now a ctf_dynhash_t.
(struct ctf_dict): All ctf_names_t fields are now ctf_dynhash_t.
(ctf_name_table): Now returns a ctf_dynhash_t.
(ctf_lookup_by_rawhash): Remove.
(ctf_hash_create): Likewise.
(ctf_hash_insert_type): Likewise.
(ctf_hash_define_type): Likewise.
(ctf_hash_lookup_type): Likewise.
(ctf_hash_size): Likewise.
(ctf_hash_destroy): Likewise.
(ctf_dynhash_create_sized): New.
(ctf_dynhash_insert_type): New.
(ctf_dynhash_lookup_type): New.
(ctf_strptr_validate): New.
* ctf-lookup.c (ctf_lookup_by_name_internal): Adapt.
* ctf-open.c (init_types): Adapt.
(ctf_set_ctl_hashes): Adapt.
(ctf_dict_close): Adapt.
* ctf-serialize.c (ctf_serialize): Adapt.
* ctf-types.c (ctf_lookup_by_rawhash): Remove.
2023-12-19 01:47:48 +08:00
|
|
|
ctf_dynhash_lookup_type (ctf_dynhash_t *hp, const char *key)
|
2019-04-24 05:12:16 +08:00
|
|
|
{
|
libctf: remove static/dynamic name lookup distinction
libctf internally maintains a set of hash tables for type name lookups,
one for each valid C type namespace (struct, union, enum, and everything
else).
Or, rather, it maintains *two* sets of hash tables: one, a ctf_hash *,
is meant for lookups in ctf_(buf)open()ed dicts with fixed content; the
other, a ctf_dynhash *, is meant for lookups in ctf_create()d dicts.
This distinction was somewhat valuable in the far pre-binutils past when
two different hashtable implementations were used (one expanding, the
other fixed-size), but those days are long gone: the hash table
implementations are almost identical, both wrappers around the libiberty
hashtab. The ctf_dynhash has many more capabilities than the ctf_hash
(iteration, deletion, etc etc) and has no downsides other than starting
at a fixed, arbitrary small size.
That limitation is easy to lift (via a new ctf_dynhash_create_sized()),
following which we can throw away nearly all the ctf_hash
implementation, and all the code to choose between readable and writable
hashtabs; the few convenience functions that are still useful (for
insertion of name -> type mappings) can also be generalized a bit so
that the extra string verification they do is potentially available to
other string lookups as well.
(libctf still has two hashtable implementations, ctf_dynhash, above,
and ctf_dynset, which is a key-only hashtab that can avoid a great many
malloc()s, used for high-volume applications in the deduplicator.)
libctf/
* ctf-create.c (ctf_create): Eliminate ctn_writable.
(ctf_dtd_insert): Likewise.
(ctf_dtd_delete): Likewise.
(ctf_rollback): Likewise.
(ctf_name_table): Eliminate ctf_names_t.
* ctf-hash.c (ctf_dynhash_create): Comment update.
Reimplement in terms of...
(ctf_dynhash_create_sized): ... this new function.
(ctf_hash_create): Remove.
(ctf_hash_size): Remove.
(ctf_hash_define_type): Remove.
(ctf_hash_destroy): Remove.
(ctf_hash_lookup_type): Rename to...
(ctf_dynhash_lookup_type): ... this.
(ctf_hash_insert_type): Rename to...
(ctf_dynhash_insert_type): ... this, moving validation to...
* ctf-string.c (ctf_strptr_validate): ... this new function.
* ctf-impl.h (struct ctf_names): Extirpate.
(struct ctf_lookup.ctl_hash): Now a ctf_dynhash_t.
(struct ctf_dict): All ctf_names_t fields are now ctf_dynhash_t.
(ctf_name_table): Now returns a ctf_dynhash_t.
(ctf_lookup_by_rawhash): Remove.
(ctf_hash_create): Likewise.
(ctf_hash_insert_type): Likewise.
(ctf_hash_define_type): Likewise.
(ctf_hash_lookup_type): Likewise.
(ctf_hash_size): Likewise.
(ctf_hash_destroy): Likewise.
(ctf_dynhash_create_sized): New.
(ctf_dynhash_insert_type): New.
(ctf_dynhash_lookup_type): New.
(ctf_strptr_validate): New.
* ctf-lookup.c (ctf_lookup_by_name_internal): Adapt.
* ctf-open.c (init_types): Adapt.
(ctf_set_ctl_hashes): Adapt.
(ctf_dict_close): Adapt.
* ctf-serialize.c (ctf_serialize): Adapt.
* ctf-types.c (ctf_lookup_by_rawhash): Remove.
2023-12-19 01:47:48 +08:00
|
|
|
void *value;
|
2019-04-24 05:12:16 +08:00
|
|
|
|
libctf: remove static/dynamic name lookup distinction
libctf internally maintains a set of hash tables for type name lookups,
one for each valid C type namespace (struct, union, enum, and everything
else).
Or, rather, it maintains *two* sets of hash tables: one, a ctf_hash *,
is meant for lookups in ctf_(buf)open()ed dicts with fixed content; the
other, a ctf_dynhash *, is meant for lookups in ctf_create()d dicts.
This distinction was somewhat valuable in the far pre-binutils past when
two different hashtable implementations were used (one expanding, the
other fixed-size), but those days are long gone: the hash table
implementations are almost identical, both wrappers around the libiberty
hashtab. The ctf_dynhash has many more capabilities than the ctf_hash
(iteration, deletion, etc etc) and has no downsides other than starting
at a fixed, arbitrary small size.
That limitation is easy to lift (via a new ctf_dynhash_create_sized()),
following which we can throw away nearly all the ctf_hash
implementation, and all the code to choose between readable and writable
hashtabs; the few convenience functions that are still useful (for
insertion of name -> type mappings) can also be generalized a bit so
that the extra string verification they do is potentially available to
other string lookups as well.
(libctf still has two hashtable implementations, ctf_dynhash, above,
and ctf_dynset, which is a key-only hashtab that can avoid a great many
malloc()s, used for high-volume applications in the deduplicator.)
libctf/
* ctf-create.c (ctf_create): Eliminate ctn_writable.
(ctf_dtd_insert): Likewise.
(ctf_dtd_delete): Likewise.
(ctf_rollback): Likewise.
(ctf_name_table): Eliminate ctf_names_t.
* ctf-hash.c (ctf_dynhash_create): Comment update.
Reimplement in terms of...
(ctf_dynhash_create_sized): ... this new function.
(ctf_hash_create): Remove.
(ctf_hash_size): Remove.
(ctf_hash_define_type): Remove.
(ctf_hash_destroy): Remove.
(ctf_hash_lookup_type): Rename to...
(ctf_dynhash_lookup_type): ... this.
(ctf_hash_insert_type): Rename to...
(ctf_dynhash_insert_type): ... this, moving validation to...
* ctf-string.c (ctf_strptr_validate): ... this new function.
* ctf-impl.h (struct ctf_names): Extirpate.
(struct ctf_lookup.ctl_hash): Now a ctf_dynhash_t.
(struct ctf_dict): All ctf_names_t fields are now ctf_dynhash_t.
(ctf_name_table): Now returns a ctf_dynhash_t.
(ctf_lookup_by_rawhash): Remove.
(ctf_hash_create): Likewise.
(ctf_hash_insert_type): Likewise.
(ctf_hash_define_type): Likewise.
(ctf_hash_lookup_type): Likewise.
(ctf_hash_size): Likewise.
(ctf_hash_destroy): Likewise.
(ctf_dynhash_create_sized): New.
(ctf_dynhash_insert_type): New.
(ctf_dynhash_lookup_type): New.
(ctf_strptr_validate): New.
* ctf-lookup.c (ctf_lookup_by_name_internal): Adapt.
* ctf-open.c (init_types): Adapt.
(ctf_set_ctl_hashes): Adapt.
(ctf_dict_close): Adapt.
* ctf-serialize.c (ctf_serialize): Adapt.
* ctf-types.c (ctf_lookup_by_rawhash): Remove.
2023-12-19 01:47:48 +08:00
|
|
|
if (ctf_dynhash_lookup_kv (hp, key, NULL, &value))
|
|
|
|
return (ctf_id_t) (uintptr_t) value;
|
2019-04-24 05:12:16 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|