A doubly linked list in Android

Source: Internet
Author: User

1. See the source code must understand the Android data structure. In the Init source code, the doubly linked list ListNode uses a lot, it only prev and next two pointers, no data members. This is the same as the Linux kernel List_head, which shows that Android is deeply affected by the Linux kernel. Originally to analyze this ListNode data structure.

One of the things to consider here is that the list operation is done through ListNode, but that's just a connection, and if we have a host structure on our hands, then of course we know where one of its listnode is, and thus call the List_add and List_del functions as arguments; , in turn, when we get one of the listnode structures along the list, how do we find the host structure? There is no pointer to its host structure in the LISTNODE structure. After all, what we really care about is the host structure, not the connectors. For this problem, we will address the example of list_head in the kernel. The page structure of the kernel contains list_head members, the problem is: Know the address of list_head, how to get the address of the page host? The following is a line of code taken from MM/PAGE_ALLOC.C:

page = Memlist_entry (curr, struct page, list);

The memlist_entry here translates a list_head pointer curr into the starting address of its host structure, which is a pointer to its host page structure. Readers may be confused about the implementation of Memlist_entry ().

#define Memlist_entry List_entry

And the list_entry definition is in include/linux/list.h.

135/**136 * List_entry getthe struct for this entry137 * @ptr: the &struct list_head pointer.138 * @type: the type of The struct is embedded in.139 * @member: The name of the list_struct within the struct.140 */141 #define LIST_ENTRY ( PTR, type, member) 142 ((Type *) ((char *) (PTR)-(unsigned Long) (& ((type *) 0)->member)))

So we should understand that Curr is the address of a page structure's internal component list, and what we need is the address of the page structure itself, so we subtract an offset from curr, that is, the amount of displacement of the component list within the page. So how does this displacement amount to be asked? & (struct page*) 0)->list represents the address of its constituent list when the structure page is exactly on address 0, which is the offset to be asked.

2. Test code

#include <stdio.h> #include <stddef.h>typedef struct _listnode{struct _listnode *prev; struct _listnode *next;} ListNode, #define Node_to_item (Node,container,member) (container*) (((char*) (node)-offsetof (Container,member))// Adds a node to the tail of the list doubly linked list, and the list always points to the head of the doubly linked list (this header contains only prev/next) void List_add_tail (ListNode *list,listnode *node) {list->p        rev->next=node;        node->prev=list->prev;        node->next=list; List->prev=node;}        Defines the host structure of a test typedef struct _node{int data; ListNode list;}        Node;int Main () {node n1,n2,n3,*n;        ListNode list,*p;        N1.data=1;        n2.data=2;        N3.data=3;        list.prev=&list;        list.next=&list;        List_add_tail (&list,&n1.list);        List_add_tail (&list,&n2.list);        List_add_tail (&list,&n3.list);                for (P=list.next;p!=&list;p=p->next) {N=node_to_item (p,node,list); printf ("%d\n", n-&Gt;data); } return 0;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.