Issue using kernel linked list in user space

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I have a linked list like this http://pastebin.com/bwF3jLb6 . The problem i am facing is i have initialized the list head and
 then i am doing malloc for that struct object. What i need is i want to make struct abc_st *r as head of linked list and other nodes after it.

typedef struct abc {
char client_id[18];
    char mac[18];
    st_list_head list;
}abc_st;

abc_st* check_fields (abc_st *ptr)
{
    char cmd[500];
    MYSQL_RES *result;
    MYSQL_ROW row;
    int num_rows;
    abc_st *r=NULL,*temp=NULL;
    INIT_LIST_HEAD(&r->list);   // i have intialized the head here

    sprintf(cmd, "SELECT * FROM ABCD_TABLE WHERE MAC = %.4x", ptr->mac);
        /* Running the sql query to check for fields with value in database */
    if (mysql_query(abc_db.db_handle, cmd)) {
        num_rows = -1;
        goto done;
    }
    result = mysql_store_result(abc_db.db_handle);
    if (result == NULL) {
        num_rows = -1;
        goto done;
    }
    num_rows = mysql_num_rows(result);

    while ((row = mysql_fetch_row(result)))
    {
        r= (abc_st *)malloc(sizeof(abc_st));
        memcpy(r->mac,row[1],strlen(row[1])+1);
        memcpy(r->client_id,row[0],strlen(row[0])+1);
How should i use list_add ?
        //list_add(struct list_head *new, struct list_head *head)  //prototype of list_add
       // list_add(&temp->list,&r->list);  //it will go wrong here, i want to make r(structure r as head and then add other strutcure objects)  
     //   r->link = NULL;
        //list_add(r, ptr);

    }
done:
    mysql_free_result(result);

    return r;

}

 #ifndef __LIST_H
 #define __LIST_H

struct list_head {
        struct list_head *next, *prev;
};
typedef struct list_head st_list_head;


#define INIT_LIST_HEAD(ptr) do { \
        (ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)

/*
 * __list_add: insert new between prev and next.
 */
static inline void __list_add(struct list_head *new,
                              struct list_head *prev,
                              struct list_head *next)
{
        next->prev = new;
        new->next = next;
        new->prev = prev;
        prev->next = new;
}

/**
 * list_add: add a new entry to the end of the list.
 */
static inline void list_add1(struct list_head *new, struct list_head *head)
{
        __list_add(new, head->prev, head);
}

/*
 *__list_del: making the prev/next entries point to each other.
 */
static inline void __list_del(struct list_head *prev, struct list_head *next)
{
        next->prev = prev;
        prev->next = next;
}

/**
 * list_del: deletes entry from list.
 */
static inline void list_del(struct list_head *entry)
{
        __list_del(entry->prev, entry->next);
        entry->next = (void *) 0;
        entry->prev = (void *) 0;
}

/**
 * list_empty: check is list is empty.
 */
static inline int list_empty(struct list_head *head)
{
        return head->next == head;
}


/**
 * list_entry: retrieve the container struct.
 */
#define list_entry(ptr, type, member) \
        ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

/**
 * list_for_each: traverse through the list.
 */
#define list_for_each(pos, head) \
        for (pos = (head)->next; pos != (head); \
                pos = pos->next)

/**
 * list_for_each_delete: traverse through the list with safe delete
 */
#define list_for_each_delete(pos, n, head) \
        for (pos = (head)->next, n = pos->next; pos != (head); \
                pos = n, n = pos->next)
#endif
_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux