Correct way to join two double linked list

后端 未结 1 460
眼角桃花
眼角桃花 2020-12-03 22:32

In the Linux kernel source, the list_splice is implemented with __list_splice:

static inline void __list_splice(const struct list_h         


        
相关标签:
1条回答
  • 2020-12-03 22:57

    The double linked list API in the Linux kernel is implemented as an abstraction of circular list. In that simple scheme the HEAD node does not contain any payload (data) and used explicitly to keep starting point of the list. Due to such design it's really simple to a) check if the list is empty, and b) debug list because unused nodes have been assigned to the so called POISON — magic number specific only to the list pointers in the entire kernel.

    1) non-initialized list

    +-------------+
    |    HEAD     |
    | prev | next |
    |POISON POISON|
    +-------------+
    

    2) empty list

    +----------+-----------+
    |          |           |
    |          |           |
    |   +------v------+    |
    |   |    HEAD     |    |
    +---+ prev | next +----+
        | HEAD   HEAD |
        +-------------+
    

    3) list with one element

    +--------------+--------------+
    |              |              |
    |              |              |
    |       +------v------+       |
    |       |    HEAD     |       |
    |   +---+ prev | next +--+    |
    |   |   |ITEM1   ITEM1|  |    |
    |   |   +-------------+  |    |
    |   +--------------------+    |
    |              |              |
    |       +------v------+       |
    |       |    ITEM1    |       |
    +-------+ prev | next +-------+
            |    DATA1    |
            +-------------+
    

    4) two items in the list

          +----------+
          |          |
          |          |
          |   +------v------+
          |   |    HEAD     |
       +------+ prev | next +----+
       |  |   |ITEM2   ITEM1|    |
       |  |   +-------------+    |
    +----------------------------+
    |  |  |          |
    |  |  |   +------v------+
    |  |  |   |    ITEM1    |
    |  |  +---+ prev | next +----+
    |  |  |   |    DATA1    |    |
    |  |  |   +-------------+    |
    |  +-------------------------+
    |     |          |
    |     |   +------v------+
    |     |   |    ITEM2    |
    +---------+ prev | next +----+
          |   |    DATA2    |    |
          |   +-------------+    |
          |                      |
          +----------------------+
    

    In the lock less algorithm there is a guarantee only for next pointer to be consistent. The guarantee wasn't always the case. The commit 2f073848c3cc introduces it.

    0 讨论(0)
提交回复
热议问题