English 中文(简体)
• 如何使用弹nel器粉碎机?
原标题:How to use the kernel hashtable API?

https://web.archive.org/web/10304024805/https://treaties.cltpub.com/book/hardnel_and_creative/978838558802/app04lnewlkerh-hrnos 我的第一个问题是:为什么我们的结构必须在其内部有<条码>struct h_list? 如果我们通过<代码>struct h_list重新进入我们的建筑。 我们的序号应在<条码>中,而不是相反。 读第一教授后,试图写以下法典:

DECLARE_HASHTABLE(nodes_hash, 3)

hash_init(nodes_hash);

struct h_node{
    int data;
    char name[MAX_NAME_SIZE]; /*The key is the name of lua state*/
    struct hlist_node node;
};

struct h_node a = {
    .data = 3,
    .name = "foo",
    .node = 0   
};

struct h_node b = {
    .data = 7,
    .name = "bar",
    .node = 0   
};    

hash_add(nodes_hash,&a.node, "foo");
hash_add(nodes_hash,&b.node, "bar");

But this does not even compile. What I m doing wrong? I need that the key be the same name present in the struct h_node. So I would like that my hash table be like this:

“entergraph

PS:在我的桌上,永远不会发生碰撞(如果永远不会发生碰撞),因此钥匙可以是struct h_node上的名称。

最佳回答

为什么我们的结构必须具备<条码> 结构 h_list/code>。 内部? 如果我们通过<代码>struct h_list重新进入我们的建筑。 我们的序号应在<条码>中,而不是相反。

这是因为海藻如何在含水层舱实施。 表格只是一系列固定尺寸的<条码> 构造清单_head。 每一个国家都是一个桶子,是相关名单的负责人。 该表仅包含一个链接清单:struct hlist_node,别无其他。 它实际上并不“储存”整个用户界定的构件,它只是每个要素的<代码>struct hlist_node领域的一个要点。

When you add an element to the hashtable, a bucket is selected, and a pointer to the struct hlist_node field of your struct is inserted in the bucket list. When you later retrieve an element (for example through hash_for_each()), the container_of() macro is used to get your real structure back, knowing its type and the name of the struct member of type struct hlist_node inside your user defined structure.

这一点可以从源代码看出。 例如,hash_for_each(> 我们:

  1. hash_for_each(姓名,bkt, obj, member) 确实:

     for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);
                     (bkt)++)
             hlist_for_each_entry(obj, &name[bkt], member)
    
  2. hlist_for_each_entry() does:

     for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);
          pos;                           
          pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
    
  3. hlist_enter_safe(。 确实:

     ({ typeof(ptr) ____ptr = (ptr); 
        ____ptr ? hlist_entry(____ptr, type, member) : NULL; 
     })
    
  4. 最后,hlist_enter(>use>container_of(),以便获得真正的结构:

     #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
    

我需要把钥匙放在<代码>struct h_node上。

这在本土是不可能的。 短链氯化石蜡罐头冷却器只处理 in化钥匙。 <<<><<>>><<<>>t>>>>>>>>>><<<0>>>>>>>><<<0>>>t>>>>>><<<0>>>>><0>>>>>><0>>>>>><0>>>>>>>>>>><0>>>>>>>>>>>><0>>>>>>>>>><0>>>>>>>>><0>>>>>><0><0>>>>>>>>><0>>>>>>>><0>>>>>>><0>>>>>>>>>>>><0>>>>>>><0>>>><0>>>>>>>>>>>>>>>><0>>>>>>>>>>>>><0>>>>><0>>>>>>>>>>>>>><0>>

短链氯化石蜡罐头透镜非常有限,毫无疑问,not<>em> /em>采用了你用其他方案拟订语言使用的同样类型的 has。 它不能把扼杀作为钥匙,而且它有固定的规模。

如果你想使用扼杀,你必须洗刷这些扼杀装置,以产生未签名的愤怒。 为此,您可使用xxhash() 或写自己的职务。 <>xxhash(>>>功能相对较新,似乎尚未用于舱面代码,因此,我认为你的舱面极有可能配置,而你却无法提供。

无论如何,必须认识到,如果散列函数将different strings转化为同一 关键,或者如果hash_add(<>,则最终在薄阵列中选择相同的指数,以插入这些成分,那么这两个要素将放在同一薄子上。 因此,在检索任何要素(例如hash_for_each_possible(<>/code>)时,请need考虑到这一点,并正确核对其 :

Working example

这里是一个完整的工作例子,以显示对油轮 has进行检测的罐车库的基本用途,但也应处理最新的5.7号。 请注意,在这种例子中,我只是为了简单起见,在模块栏目上分配变量:_in。 这意味着,除了在《守则》内之外,我无法在《守则》中从任何地方对“可支配(<>>/代码”进行查询。 如果你想要一个能够持有后来由不同职能取用的内容的全球平台,那么你就必须积极使用<代码>kmalloc()。

// SPDX-License-Identifier: GPL-3.0
#include <linux/hashtable.h> // hashtable API
#include <linux/module.h>    // module_{init,exit}, MODULE_*
#include <linux/string.h>    // strcpy, strcmp
#include <linux/types.h>     // u32 etc.

#define MAX 32

struct h_node {
    int data;
    char name[MAX];
    struct hlist_node node;
};

DECLARE_HASHTABLE(tbl, 3);

// Just to demonstrate the behavior when two keys are equal.
static u32 myhash(const char *s) {
    u32 key = 0;
    char c;

    while ((c = *s++))
        key += c;

    return key;
}

static int __init myhashtable_init(void)
{
    struct h_node a, b, *cur;
    u32 key_a, key_b;
    unsigned bkt;

    pr_info("myhashtable: module loaded
");

    a.data = 3;
    strcpy(a.name, "foo");

    b.data = 7;
    strcpy(b.name, "oof");

    /* Calculate key for each element.
     * Since the above hash function only sums the characters, we will
     * end up having two identical keys here.
     */
    key_a = myhash(a.name);
    key_b = myhash(b.name);

    pr_info("myhashtable: key_a = %u, key_b = %u
", key_a, key_b);

    // Initialize the hashtable.
    hash_init(tbl);

    // Insert the elements.
    hash_add(tbl, &a.node, key_a);
    hash_add(tbl, &b.node, key_b);

    // List all elements in the table.
    hash_for_each(tbl, bkt, cur, node) {
        pr_info("myhashtable: element: data = %d, name = %s
",
            cur->data, cur->name);
    }

    // Get the element with name = "foo".
    hash_for_each_possible(tbl, cur, node, key_a) {
        pr_info("myhashtable: match for key %u: data = %d, name = %s
",
            key_a, cur->data, cur->name);

        // Check the name.
        if (!strcmp(cur->name, "foo")) {
            pr_info("myhashtable: element named "foo" found!
");
            break;
        }
    }

    // Remove elements.
    hash_del(&a.node);
    hash_del(&b.node);

    return 0;
}

static void __exit myhashtable_exit(void)
{
    // Do nothing (needed to be able to unload the module).
    pr_info("myhashtable: module unloaded
");
}


module_init(myhashtable_init);
module_exit(myhashtable_exit);
MODULE_VERSION("0.1");
MODULE_DESCRIPTION("Silly kernel hashtable API example module.");
MODULE_AUTHOR("Marco Bonelli");
MODULE_LICENSE("GPL");

www.un.org/Depts/DGACM/index_spanish.htm

[ 3174.567029] myhashtable: key_a = 324, key_b = 324
[ 3174.567030] myhashtable: element: data = 7, name = oof
[ 3174.567031] myhashtable: element: data = 3, name = foo
[ 3174.567032] myhashtable: match for key 324: data = 7, name = oof
[ 3174.567033] myhashtable: match for key 324: data = 3, name = foo
[ 3174.567033] myhashtable: element named "foo" found!
问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...