0

someone help me please i am stucked at this pointed at line 75,96,133 i am getting error warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] when at line 96 i replaced int with long then it resolved the issue but for other two it is still same someone please explain me what i am doing wrong here?

#include <linux/module.h>
#include <linux/list.h>
#include <linux/slab.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("WaSi");
int init(void);//initialization
int hashFunc(int);//Hash function
void addAllNodes(int[]);//Add all keyword nodes
void showAllNodes(void);//Print all keyword nodes
int search(int);//Query node address based on keywords

//Hash table length and hash remainder
#define N 8
//Keyword array length
#define LENGTH 20
//Node structure
struct hlist_data {
    int key;
    struct hlist_node hNode;
};
struct hlist_head* head;

//Initialize, apply for memory
int init(void) {
    int index;
    head = (struct hlist_head*)kmalloc(sizeof(struct hlist_head) * N, GFP_KERNEL);
    if (!head) return 0;
    printk("\nStart initializing the hash table!\n");
    for (index = 0; index < N; index++) {
        INIT_HLIST_HEAD(&head[index]);
    }
    return 1;
}

//The simplest hash function-remainder
int hashFunc(int key) {
    return key % N;
}

//Insert all keyword nodes
void addAllNodes(int array[]) {
    int index;
    int i;
    int hashAddr;//Address;
    struct hlist_data* hd;

    for (index = 0, i = 1; index < LENGTH; index++) {
        //Apply for memory for keyword nodes
        hd = (struct hlist_data*)kmalloc(sizeof(struct hlist_data), GFP_KERNEL);
        INIT_HLIST_NODE(&(hd->hNode));
        hd->key = array[index];
        printk("Add node%d-----%d!\n", i++, array[index]);

        //Keyword gets the hash address according  the hash function
        hashAddr = hashFunc(array[index]);
        hlist_add_head(&(hd->hNode), &(head[hashAddr]));//Head insertion method
    }
}

// Traverse and print all nodes
void showAllNodes(void) {
    int index;
    struct hlist_data* hd;
    struct hlist_node* pos;

    printk("\ntraversal print!\n");
    //Loop array
    for (index = 0; index < N; index++) {
        printk("%d\n", index);
        //Circular singly linked list
        hlist_for_each(pos, &(head[index])) {
            //Find out the first address of the linked list node pointed  by pos
            hd = hlist_entry(pos, struct hlist_data, hNode);
            **printk("%d(%d)->", hd->key, int pos);**
        }
        printk("NULL");
        printk("\n");
    }
}

//Find the node address based on keywords
int search(int searchKey) {
    int hashAddr;
    struct hlist_data* hd;
    struct hlist_node* pos;

    printk("finding %d!\n", searchKey);

    //Find the address
    hashAddr = hashFunc(searchKey);
    printk("According  the hash function  obtain the address=%d!\n", hashAddr);

    hlist_for_each(pos, &(head[hashAddr])) {
        hd = hlist_entry(pos, struct hlist_data, hNode);
        **if (hd->key == searchKey) return (long)pos;**
    }
    return -1;
}

static int __init hlist_init(void) {
    int keys[LENGTH] = { 1,2,3,4,5,6,7,16,18,20,31,44,47,51,55,60,66,69,72,73 };// A set of keywords
    int addr;//Used when searching
    int searchKey;

    //Initialization
    if (!init()) return -1;

    //Insert the node as a hash function according  the simple remainder
    addAllNodes(keys);

    //Print all nodes
    showAllNodes();

    //Give keywords
    searchKey = 19;
    addr = search(searchKey);
    if (addr == -1) printk("Search failed\n\n");
    else printk("Find the node address with key %d is %d\n\n", searchKey, addr);

    return 0;
}

static void __exit hlist_exit(void) {
    int index;
    int i;
    struct hlist_data* hd;
    struct hlist_node* pos, * n;
    for (index = 0, i = 1; index < N; index++) {
        //The traversal here needs  be safe
        hlist_for_each_safe(pos, n, &(head[index])) {
            hd = hlist_entry(pos, struct hlist_data, hNode);
            **printk("Delete node%d-----%d, the address is%d\n", i++, hd->key, int pos);**
            hlist_del(pos);
            kfree(hd);
        }
    }
    kfree(head);

    printk("Deletion is complete!\n");
}

module_init(hlist_init);
module_exit(hlist_exit);

0 Answers0