Notes
main
main
  • Introduction
  • linuxKernel
    • tips
    • make_help
    • old linux
      • compile_linux0.11
      • TestEnvironment
      • load_setup
      • get_hard_data
    • list
    • plist
    • fifo
    • idr
    • xarray
    • rbtree
    • maple_tree
    • syscall
    • bitmap
    • page
    • page_flags
    • page_size
    • page mapcount
    • page refcount
    • folio
    • slub
      • proc_slabinfo
      • slub_theory
      • kmalloc_kfree
      • kmem_cache
      • slab_alloc
      • slab_free
      • proc_meminfo_SReclaimable_SReclaimable
    • vmalloc
    • brk
    • mmap
    • mremap
    • mprotect
    • madvise
    • read
    • write
    • shmem
    • huge_page
    • page_fault
    • rmap
    • lru
    • multi-gen-LRU
    • page_reclaim
    • page_cache
    • page_table
    • rcu
    • kvm
    • aarch64_boot
    • tracing_system
    • cache_coherence_and_memory_consistency
    • cpu_speculates
    • mmap_lock
    • per-vma_lock
    • cgroup
    • symbol
    • campact
    • page_ext
    • mempool
    • kernelstack
    • filesystem
    • io_stack
    • workingset
    • ioremap
    • sched_period
  • linuxDebug
    • openocd_openjtag
    • i2c_tools
    • objdump
    • addr2line
    • gdb_useage
    • debug_linux_kernel_via_gdb
    • debug_linux_module_via_gdb
    • early_boot
    • sequentially_execute
    • dynamic_debug
    • research_linuxKernel_by_patch
    • tracefs
    • ebpf
    • bpftrace
    • perf
    • flame_graph
    • crash
    • ASAN_HWASAN_MTE_check_mem_bug
    • page_owner
    • vmtouch
    • fio
    • benchmark
  • linuxSystem
    • common
      • system_version
      • procfs
      • proc_sys_vm
      • cmd_ps
      • makefile
      • file_descriptor
      • psi
      • ulimit
      • top
      • delay_accounting
    • ubuntu
      • custom_kernel
      • get_cmd_src
      • record_ssh_info
      • log
      • run_custom_script
      • repo
      • cockpit
      • nfs
      • tftp
      • misc
    • fedora
      • system_upgrade
      • custom_kernel
      • lvextend
      • yt-dlp
      • jellyfin
  • linuxDriver
    • i2c_peripherals_driver
    • spi_peripherals_driver
    • gpio_subsystem
    • IRQ_driver
    • blockIO_unblockIO_async
    • linux_own_driver
    • misc_device
    • input_device
    • timer
    • atomic_spinlock_semaphore_mutex
    • lcd
    • touch_screen
    • debugfs
    • v4l2
    • mmap
  • hardware
    • paging_mmu_pt
    • iommu
  • process_thread_scheduler
    • scheduler01
    • scheduler02
    • scheduler03
    • scheduler04
    • scheduler05
    • scheduler06
  • memory_management
    • mm1
    • mm2
    • mm3
    • mm4
    • mm5
  • input_output_filesystem
    • io_fs_01
    • io_fs_02
    • io_fs_03
    • io_fs_04
  • lock_and_lockup_detector
    • general_lock
    • hung_task
    • softLockup_hardLockup
    • crash_experiment
  • MIT_6.S081
    • 6.S081_Operating_System_Engineering
    • Schedule.md
    • Class
      • Overview
      • Administrivia
    • Labs
      • Tools
      • Guidance
      • startup
      • syscall
      • page_table
      • Calling_Convention
      • traps
    • xv6
      • xv6
    • References.md
  • qemu
    • qemu_buildroot
    • qemu_busybox.md
    • Serial.md
    • demo_mini2440
      • 0_compilation_error_summary
      • 1_compilation_steps
      • 2_operation_mode
      • 3_transplant_tools_libraries
      • 4_tools_use
      • reference_website
  • tools
    • getKernelSourceCodeList
    • nat
    • shell
    • translating
    • YouCompleteMe
    • cscope
    • global
    • vscode
    • vim
    • binary
    • markdown
    • draw
    • git
    • tig
    • tmux
    • mail_client
    • download_patchset_from_LKML
    • minicom
    • clash
  • other
    • interview
    • interview_c_base
    • know_dontknow
    • Stop-Ask-Questions-The-Stupid-Ways
    • How-To-Ask-Questions-The-Smart-Way
    • docker
    • buildroot
    • rv32_to_rv64
Powered by GitBook
On this page

Was this helpful?

  1. linuxKernel

list

linux kernel有链表、队列、映射、二叉树等内建数据结构,并且封装一系列API给linux kernel开发人员使用,本章讲解链表。

base linux-2.6.34

简介

链表有单向链表、双向链表、环形链表,linux kernel 标准链表就是采用环形双向链表形式实现

链表节点 定义如下:

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

如何使用 linux kernel 链表?一般将链表节点内嵌在对象结构内,如下:

struct test {
    int index;
    struct list_head list;
};

如何定义链表头 head ?如下:

LIST_HEAD(head);

如何将链表节点添加链表头?

void list_add(struct list_head *new, struct list_head *head)

如何从链表头删除链表节点?

void list_del(struct list_head *entry)

如何得到链表节点内嵌对象结构的内容?

struct test *pos;

list_for_each_entry(pos, head, list) {
    /* content */
}

实践

#include <linux/list.h>
#include <linux/slab.h>

struct test {                       // 定义对象结构体
    int index;
    struct list_head list;
};

void list()
{
    LIST_HEAD(tlist);               // 定义链表头,名为tlist
    struct test *ta, *tb, *tc, *tt; // 定义对象

    ta = kmalloc(sizeof(*ta), GFP_KERNEL); // 动态分配对象
    ta->index = 1;                         // 初始化对象内容
    INIT_LIST_HEAD(&ta->list);             // 初始化对象链表节点

    tb = kmalloc(sizeof(*tb), GFP_KERNEL);
    tb->index = 2;
    INIT_LIST_HEAD(&tb->list);

    tc = kmalloc(sizeof(*tc), GFP_KERNEL);
    tc->index = 3;
    INIT_LIST_HEAD(&tc->list);

    list_add(&ta->list, &tlist);           // 将链表节点添加到链表头
    list_add(&tb->list, &tlist);
    list_add(&tc->list, &tlist);

    list_del(&tb->list);                   // 从链表头删除链表节点
    list_add(&tb->list, &tlist);

    // 从链表头tlist开始,遍历每一个链表节点list,并且得到对象指针保存到tt中
    list_for_each_entry(tt, &tlist, list) {
        pr_debug("%s: index %d\n", __func__, tt->index);
    }

    if(list_empty(&tlist)) // 判断链表是否为空
        pr_debug("%s: empty\n", __func__);
    else
        pr_debug("%s: not empty\n", __func__);

    kfree(ta);             // 释放对象
    kfree(tb);
    kfree(tc);
}
Previousget_hard_dataNextplist

Last updated 3 years ago

Was this helpful?