Changeset 0ca7286 in mainline for uspace/srv/ns/task.c


Ignore:
Timestamp:
2012-07-21T11:19:27Z (12 years ago)
Author:
Adam Hraska <adam.hraska+hos@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2732c94
Parents:
1c1da4b
Message:

Added resizing to user space (single-threaded) hash_table. Resizes in a way to mitigate effects of bad hash functions. Change of interface affected many files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/ns/task.c

    r1c1da4b r0ca7286  
    4343#include "ns.h"
    4444
    45 #define TASK_HASH_TABLE_CHAINS  256
    46 #define P2I_HASH_TABLE_CHAINS   256
    4745
    4846/* TODO:
     
    7169 *
    7270 */
    73 static hash_index_t task_hash(unsigned long key[])
    74 {
    75         assert(key);
    76         return (LOWER32(key[0]) % TASK_HASH_TABLE_CHAINS);
     71static size_t task_key_hash(unsigned long key[])
     72{
     73        size_t hash = 17;
     74        hash = 37 * hash + key[1];
     75        hash = 37 * hash + key[0];
     76        return hash;
     77}
     78
     79static size_t task_hash(const link_t *item)
     80{
     81        hashed_task_t *ht = hash_table_get_instance(item, hashed_task_t, link);
     82
     83        unsigned long key[] = {
     84                LOWER32(ht->id),
     85                UPPER32(ht->id)
     86        };
     87       
     88        return task_key_hash(key);
    7789}
    7890
     
    8698 *
    8799 */
    88 static int task_compare(unsigned long key[], hash_count_t keys, link_t *item)
     100static bool task_match(unsigned long key[], size_t keys, const link_t *item)
    89101{
    90102        assert(key);
    91         assert(keys <= 2);
     103        assert(keys == 2);
    92104        assert(item);
    93105       
    94106        hashed_task_t *ht = hash_table_get_instance(item, hashed_task_t, link);
    95107       
    96         if (keys == 2)
    97                 return ((LOWER32(key[1]) == UPPER32(ht->id))
    98                     && (LOWER32(key[0]) == LOWER32(ht->id)));
    99         else
    100                 return (LOWER32(key[0]) == LOWER32(ht->id));
     108        return (key[0] == LOWER32(ht->id))
     109                && (key[1] == UPPER32(ht->id));
    101110}
    102111
     
    113122
    114123/** Operations for task hash table. */
    115 static hash_table_operations_t task_hash_table_ops = {
     124static hash_table_ops_t task_hash_table_ops = {
    116125        .hash = task_hash,
    117         .compare = task_compare,
     126        .key_hash = task_key_hash,
     127        .match = task_match,
     128        .equal = 0,
    118129        .remove_callback = task_remove
    119130};
     
    135146 *
    136147 */
    137 static hash_index_t p2i_hash(unsigned long key[])
     148static size_t p2i_key_hash(unsigned long key[])
    138149{
    139150        assert(key);
    140         return (key[0] % TASK_HASH_TABLE_CHAINS);
     151        return key[0];
     152}
     153
     154static size_t p2i_hash(const link_t *item)
     155{
     156        p2i_entry_t *entry = hash_table_get_instance(item, p2i_entry_t, link);
     157        unsigned long key = entry->in_phone_hash;
     158        return p2i_key_hash(&key);
    141159}
    142160
     
    150168 *
    151169 */
    152 static int p2i_compare(unsigned long key[], hash_count_t keys, link_t *item)
     170static bool p2i_match(unsigned long key[], size_t keys, const link_t *item)
    153171{
    154172        assert(key);
     
    173191
    174192/** Operations for task hash table. */
    175 static hash_table_operations_t p2i_ops = {
     193static hash_table_ops_t p2i_ops = {
    176194        .hash = p2i_hash,
    177         .compare = p2i_compare,
     195        .key_hash = p2i_key_hash,
     196        .match = p2i_match,
     197        .equal = 0,
    178198        .remove_callback = p2i_remove
    179199};
     
    193213int task_init(void)
    194214{
    195         if (!hash_table_create(&task_hash_table, TASK_HASH_TABLE_CHAINS,
    196             2, &task_hash_table_ops)) {
     215        if (!hash_table_create(&task_hash_table, 0, 2, &task_hash_table_ops)) {
    197216                printf(NAME ": No memory available for tasks\n");
    198217                return ENOMEM;
    199218        }
    200219       
    201         if (!hash_table_create(&phone_to_id, P2I_HASH_TABLE_CHAINS,
    202             1, &p2i_ops)) {
     220        if (!hash_table_create(&phone_to_id, 0, 1, &p2i_ops)) {
    203221                printf(NAME ": No memory available for tasks\n");
    204222                return ENOMEM;
     
    293311int ns_task_id_intro(ipc_call_t *call)
    294312{
    295         unsigned long keys[2];
    296313       
    297314        task_id_t id = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call));
    298         keys[0] = call->in_phone_hash;
     315
     316        unsigned long keys[] = { call->in_phone_hash };
    299317       
    300318        link_t *link = hash_table_find(&phone_to_id, keys);
     
    317335        entry->in_phone_hash = call->in_phone_hash;
    318336        entry->id = id;
    319         hash_table_insert(&phone_to_id, keys, &entry->link);
     337        hash_table_insert(&phone_to_id, &entry->link);
    320338       
    321339        /*
    322340         * Insert into the main table.
    323341         */
    324        
    325         keys[0] = LOWER32(id);
    326         keys[1] = UPPER32(id);
    327342       
    328343        link_initialize(&ht->link);
     
    331346        ht->have_rval = false;
    332347        ht->retval = -1;
    333         hash_table_insert(&task_hash_table, keys, &ht->link);
     348        hash_table_insert(&task_hash_table, &ht->link);
    334349       
    335350        return EOK;
Note: See TracChangeset for help on using the changeset viewer.