Changeset 5e801dc in mainline for kernel


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:
kernel
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • kernel/genarch/src/mm/page_ht.c

    ree8d4d6 r5e801dc  
    5454
    5555static size_t ht_hash(const ht_link_t *);
    56 static size_t ht_key_hash(void *);
    57 static bool ht_key_equal(void *, const ht_link_t *);
     56static size_t ht_key_hash(const void *);
     57static bool ht_key_equal(const void *, const ht_link_t *);
    5858static void ht_remove_callback(ht_link_t *);
    5959
     
    109109
    110110/** Return the hash of the key. */
    111 size_t ht_key_hash(void *arg)
    112 {
    113         uintptr_t *key = (uintptr_t *) arg;
     111size_t ht_key_hash(const void *arg)
     112{
     113        const uintptr_t *key = arg;
    114114        size_t hash = 0;
    115115        hash = hash_combine(hash, key[KEY_AS]);
     
    119119
    120120/** Return true if the key is equal to the item's lookup key. */
    121 bool ht_key_equal(void *arg, const ht_link_t *item)
    122 {
    123         uintptr_t *key = (uintptr_t *) arg;
     121bool ht_key_equal(const void *arg, const ht_link_t *item)
     122{
     123        const uintptr_t *key = arg;
    124124        pte_t *pte = hash_table_get_inst(item, pte_t, link);
    125125        return (key[KEY_AS] == (uintptr_t) pte->as) &&
  • kernel/generic/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 *),
  • kernel/generic/src/adt/hash_table.c

    ree8d4d6 r5e801dc  
    5151#include <adt/hash_table.h>
    5252#include <adt/list.h>
     53#include <assert.h>
    5354#include <stdlib.h>
    54 #include <assert.h>
    5555#include <str.h>
    5656
     
    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);
     
    306306 * @return Returns the number of removed items.
    307307 */
    308 size_t hash_table_remove(hash_table_t *h, void *key)
     308size_t hash_table_remove(hash_table_t *h, const void *key)
    309309{
    310310        assert(h && h->bucket);
  • kernel/generic/src/cap/cap.c

    ree8d4d6 r5e801dc  
    101101}
    102102
    103 static size_t caps_key_hash(void *key)
    104 {
    105         cap_handle_t *handle = (cap_handle_t *) key;
     103static size_t caps_key_hash(const void *key)
     104{
     105        const cap_handle_t *handle = key;
    106106        return hash_mix(cap_handle_raw(*handle));
    107107}
    108108
    109 static bool caps_key_equal(void *key, const ht_link_t *item)
    110 {
    111         cap_handle_t *handle = (cap_handle_t *) key;
     109static bool caps_key_equal(const void *key, const ht_link_t *item)
     110{
     111        const cap_handle_t *handle = key;
    112112        cap_t *cap = hash_table_get_inst(item, cap_t, caps_link);
    113113        return *handle == cap->handle;
  • kernel/generic/src/ddi/irq.c

    ree8d4d6 r5e801dc  
    7373
    7474static size_t irq_ht_hash(const ht_link_t *);
    75 static size_t irq_ht_key_hash(void *);
     75static size_t irq_ht_key_hash(const void *);
    7676static bool irq_ht_equal(const ht_link_t *, const ht_link_t *);
    77 static bool irq_ht_key_equal(void *, const ht_link_t *);
     77static bool irq_ht_key_equal(const void *, const ht_link_t *);
    7878
    7979static hash_table_ops_t irq_ht_ops = {
     
    208208
    209209/** Return the hash of the key. */
    210 size_t irq_ht_key_hash(void *key)
    211 {
    212         inr_t *inr = (inr_t *) key;
     210size_t irq_ht_key_hash(const void *key)
     211{
     212        const inr_t *inr = key;
    213213        return hash_mix(*inr);
    214214}
     
    223223
    224224/** Return true if the key is equal to the item's lookup key. */
    225 bool irq_ht_key_equal(void *key, const ht_link_t *item)
    226 {
    227         inr_t *inr = (inr_t *) key;
     225bool irq_ht_key_equal(const void *key, const ht_link_t *item)
     226{
     227        const inr_t *inr = key;
    228228        irq_t *irq = hash_table_get_inst(item, irq_t, link);
    229229        return irq->inr == *inr;
  • kernel/generic/src/lib/ra.c

    ree8d4d6 r5e801dc  
    6767
    6868/** Return the hash of the key */
    69 static size_t used_key_hash(void *key)
    70 {
    71         uintptr_t *base = (uintptr_t *) key;
     69static size_t used_key_hash(const void *key)
     70{
     71        const uintptr_t *base = key;
    7272        return hash_mix(*base);
    7373}
    7474
    7575/** Return true if the key is equal to the item's lookup key */
    76 static bool used_key_equal(void *key, const ht_link_t *item)
    77 {
    78         uintptr_t *base = (uintptr_t *) key;
     76static bool used_key_equal(const void *key, const ht_link_t *item)
     77{
     78        const uintptr_t *base = key;
    7979        ra_segment_t *seg = hash_table_get_inst(item, ra_segment_t, uh_link);
    8080        return seg->base == *base;
Note: See TracChangeset for help on using the changeset viewer.