Changeset 5e801dc in mainline for uspace/lib


Ignore:
Timestamp:
2019-02-25T14:42:38Z (6 years ago)
Author:
GitHub <noreply@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a4e78743
Parents:
ee8d4d6
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2019-02-25 14:42:38)
git-committer:
GitHub <noreply@…> (2019-02-25 14:42:38)
Message:

Indicate and enforce constness of hash table key in certain functions (#158)

The assumption here is that modifying key in the hash/equal functions in something completely unexpected, and not something you would ever want to do intentionally, so it makes sense to disallow it entirely to get that extra level of checking.

Location:
uspace/lib
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/block/block.c

    ree8d4d6 r5e801dc  
    241241}
    242242
    243 static size_t cache_key_hash(void *key)
    244 {
    245         aoff64_t *lba = (aoff64_t *)key;
     243static size_t cache_key_hash(const void *key)
     244{
     245        const aoff64_t *lba = key;
    246246        return *lba;
    247247}
     
    253253}
    254254
    255 static bool cache_key_equal(void *key, const ht_link_t *item)
    256 {
    257         aoff64_t *lba = (aoff64_t *)key;
     255static bool cache_key_equal(const void *key, const ht_link_t *item)
     256{
     257        const aoff64_t *lba = key;
    258258        block_t *b = hash_table_get_inst(item, block_t, hash_link);
    259259        return b->lba == *lba;
  • uspace/lib/c/generic/adt/hash_table.c

    ree8d4d6 r5e801dc  
    245245 *
    246246 */
    247 ht_link_t *hash_table_find(const hash_table_t *h, void *key)
     247ht_link_t *hash_table_find(const hash_table_t *h, const void *key)
    248248{
    249249        assert(h && h->bucket);
     
    303303 * @param key  Array of keys that will be compared against items of
    304304 *             the hash table.
    305  * @param keys Number of keys in the 'key' array.
    306305 *
    307306 * @return Returns the number of removed items.
    308307 */
    309 size_t hash_table_remove(hash_table_t *h, void *key)
     308size_t hash_table_remove(hash_table_t *h, const void *key)
    310309{
    311310        assert(h && h->bucket);
  • uspace/lib/c/generic/async/ports.c

    ree8d4d6 r5e801dc  
    103103static hash_table_t interface_hash_table;
    104104
    105 static size_t interface_key_hash(void *key)
    106 {
    107         iface_t iface = *(iface_t *) key;
    108         return iface;
     105static size_t interface_key_hash(const void *key)
     106{
     107        const iface_t *iface = key;
     108        return *iface;
    109109}
    110110
     
    115115}
    116116
    117 static bool interface_key_equal(void *key, const ht_link_t *item)
    118 {
    119         iface_t iface = *(iface_t *) key;
     117static bool interface_key_equal(const void *key, const ht_link_t *item)
     118{
     119        const iface_t *iface = key;
    120120        interface_t *interface = hash_table_get_inst(item, interface_t, link);
    121         return iface == interface->iface;
     121        return *iface == interface->iface;
    122122}
    123123
     
    131131};
    132132
    133 static size_t port_key_hash(void *key)
    134 {
    135         port_id_t port_id = *(port_id_t *) key;
    136         return port_id;
     133static size_t port_key_hash(const void *key)
     134{
     135        const port_id_t *port_id = key;
     136        return *port_id;
    137137}
    138138
     
    143143}
    144144
    145 static bool port_key_equal(void *key, const ht_link_t *item)
    146 {
    147         port_id_t port_id = *(port_id_t *) key;
     145static bool port_key_equal(const void *key, const ht_link_t *item)
     146{
     147        const port_id_t *port_id = key;
    148148        port_t *port = hash_table_get_inst(item, port_t, link);
    149         return port_id == port->id;
     149        return *port_id == port->id;
    150150}
    151151
  • uspace/lib/c/generic/async/server.c

    ree8d4d6 r5e801dc  
    231231static sysarg_t notification_avail = 0;
    232232
    233 static size_t client_key_hash(void *key)
    234 {
    235         task_id_t in_task_id = *(task_id_t *) key;
    236         return in_task_id;
     233static size_t client_key_hash(const void *key)
     234{
     235        const task_id_t *in_task_id = key;
     236        return *in_task_id;
    237237}
    238238
     
    243243}
    244244
    245 static bool client_key_equal(void *key, const ht_link_t *item)
    246 {
    247         task_id_t in_task_id = *(task_id_t *) key;
     245static bool client_key_equal(const void *key, const ht_link_t *item)
     246{
     247        const task_id_t *in_task_id = key;
    248248        client_t *client = hash_table_get_inst(item, client_t, link);
    249         return in_task_id == client->in_task_id;
     249        return *in_task_id == client->in_task_id;
    250250}
    251251
     
    490490}
    491491
    492 static size_t notification_key_hash(void *key)
    493 {
    494         sysarg_t id = *(sysarg_t *) key;
    495         return id;
     492static size_t notification_key_hash(const void *key)
     493{
     494        const sysarg_t *id = key;
     495        return *id;
    496496}
    497497
     
    503503}
    504504
    505 static bool notification_key_equal(void *key, const ht_link_t *item)
    506 {
    507         sysarg_t id = *(sysarg_t *) key;
     505static bool notification_key_equal(const void *key, const ht_link_t *item)
     506{
     507        const sysarg_t *id = key;
    508508        notification_t *notification =
    509509            hash_table_get_inst(item, notification_t, htlink);
    510         return id == notification->imethod;
     510        return *id == notification->imethod;
    511511}
    512512
  • uspace/lib/c/include/adt/hash_table.h

    ree8d4d6 r5e801dc  
    5353
    5454        /** Returns the hash of the key. */
    55         size_t (*key_hash)(void *key);
     55        size_t (*key_hash)(const void *key);
    5656
    5757        /** True if the items are equal (have the same lookup keys). */
     
    5959
    6060        /** Returns true if the key is equal to the item's lookup key. */
    61         bool (*key_equal)(void *key, const ht_link_t *item);
     61        bool (*key_equal)(const void *key, const ht_link_t *item);
    6262
    6363        /** Hash table item removal callback.
     
    9494extern void hash_table_insert(hash_table_t *, ht_link_t *);
    9595extern bool hash_table_insert_unique(hash_table_t *, ht_link_t *);
    96 extern ht_link_t *hash_table_find(const hash_table_t *, void *);
     96extern ht_link_t *hash_table_find(const hash_table_t *, const void *);
    9797extern ht_link_t *hash_table_find_next(const hash_table_t *, ht_link_t *,
    9898    ht_link_t *);
    99 extern size_t hash_table_remove(hash_table_t *, void *);
     99extern size_t hash_table_remove(hash_table_t *, const void *);
    100100extern void hash_table_remove_item(hash_table_t *, ht_link_t *);
    101101extern void hash_table_apply(hash_table_t *, bool (*)(ht_link_t *, void *),
  • uspace/lib/ext4/src/ops.c

    ree8d4d6 r5e801dc  
    101101} node_key_t;
    102102
    103 static size_t open_nodes_key_hash(void *key_arg)
    104 {
    105         node_key_t *key = (node_key_t *)key_arg;
     103static size_t open_nodes_key_hash(const void *key_arg)
     104{
     105        const node_key_t *key = key_arg;
    106106        return hash_combine(key->service_id, key->index);
    107107}
     
    113113}
    114114
    115 static bool open_nodes_key_equal(void *key_arg, const ht_link_t *item)
    116 {
    117         node_key_t *key = (node_key_t *)key_arg;
     115static bool open_nodes_key_equal(const void *key_arg, const ht_link_t *item)
     116{
     117        const node_key_t *key = key_arg;
    118118        ext4_node_t *enode = hash_table_get_inst(item, ext4_node_t, link);
    119119
  • uspace/lib/nic/src/nic_addr_db.c

    ree8d4d6 r5e801dc  
    6262} addr_key_t;
    6363
    64 static bool nic_addr_key_equal(void *key_arg, const ht_link_t *item)
    65 {
    66         addr_key_t *key = (addr_key_t *)key_arg;
     64static bool nic_addr_key_equal(const void *key_arg, const ht_link_t *item)
     65{
     66        const addr_key_t *key = key_arg;
    6767        nic_addr_entry_t *entry = member_to_inst(item, nic_addr_entry_t, link);
    6868
     
    8181}
    8282
    83 static size_t nic_addr_key_hash(void *k)
    84 {
    85         addr_key_t *key = (addr_key_t *)k;
     83static size_t nic_addr_key_hash(const void *k)
     84{
     85        const addr_key_t *key = k;
    8686        return addr_hash(key->len, key->addr);
    8787}
  • uspace/lib/nic/src/nic_wol_virtues.c

    ree8d4d6 r5e801dc  
    4545 */
    4646
    47 static size_t nic_wv_key_hash(void *key)
    48 {
    49         return *(nic_wv_id_t *) key;
     47static size_t nic_wv_key_hash(const void *key)
     48{
     49        const nic_wv_id_t *k = key;
     50        return *k;
    5051}
    5152
     
    5657}
    5758
    58 static bool nic_wv_key_equal(void *key, const ht_link_t *item)
    59 {
    60         nic_wol_virtue_t *virtue = (nic_wol_virtue_t *) item;
    61         return (virtue->id == *(nic_wv_id_t *) key);
     59static bool nic_wv_key_equal(const void *key, const ht_link_t *item)
     60{
     61        const nic_wv_id_t *k = key;
     62        const nic_wol_virtue_t *virtue = (const nic_wol_virtue_t *) item;
     63        return (virtue->id == *k);
    6264}
    6365
Note: See TracChangeset for help on using the changeset viewer.