首页 > 系统相关 >linux内核链表

linux内核链表

时间:2024-01-19 18:55:07浏览次数:39  
标签:head list next 链表 add 内核 linux entry prev

linux内核的链表实现

定义链表节点和初始化

LIST_HEAD_INIT宏通过将next和prev都指向自身,来对节点进行初始化

LIST_HEAD宏定义一个struct list_head类型的节点,并使用LIST_HEAD_INIT宏进行初始化

点击查看代码
struct list_head {
	struct list_head *next, *prev;
};


#define LIST_HEAD_INIT(name)                                                   \
  { &(name), &(name) }

#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)

插入节点

linux内核实现了头插法和尾插法。
__list_add函数将待插入节点,插入到prev和next之间,而不管prev和next是什么
list_add和list_add_tail,参数列表一直,但使用__list_add的方式不一致。
list_add将头结点,作为prev。
list_add_tail将头结点的prev,作为prev。
因此list_add是头插法
list_add_tail是尾插法

点击查看代码
/*
 * Insert a new entry between two known consecutive entries.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
static inline void __list_add(struct list_head *new, struct list_head *prev,
                              struct list_head *next) {
  if (!__list_add_valid(new, prev, next))
    return;

  next->prev = new;
  new->next = next;
  new->prev = prev;
  WRITE_ONCE(prev->next, new);
}

/**
 * list_add - add a new entry
 * @new: new entry to be added
 * @head: list head to add it after
 *
 * Insert a new entry after the specified head.
 * This is good for implementing stacks.
 */
static inline void list_add(struct list_head *new, struct list_head *head) {
  __list_add(new, head, head->next);
  
/**
 * list_add_tail - add a new entry
 * @new: new entry to be added
 * @head: list head to add it before
 *
 * Insert a new entry before the specified head.
 * This is useful for implementing queues.
 */
static inline void list_add_tail(struct list_head *new,
                                 struct list_head *head) {
  __list_add(new, head->prev, head);
}
}

删除节点

点击查看代码

删除链表某个节点,使用list_del
__list_del负责将entry->next->prev 设置为 entrey -> prev。entry->prev->next 设置为 entry->next。entry自身的next指向LIST_POISON1,prev指向LIST_POISON2上。

/*
 * Delete a list entry by making the prev/next entries
 * point to each other.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
static inline void __list_del(struct list_head *prev, struct list_head *next) {
  next->prev = prev;
  WRITE_ONCE(prev->next, next);
}

/**
 * list_del - deletes entry from list.
 * @entry: the element to delete from the list.
 * Note: list_empty() on entry does not return true after this, the entry is
 * in an undefined state.
 */
static inline void __list_del_entry(struct list_head *entry) {
  if (!__list_del_entry_valid(entry))
    return;

  __list_del(entry->prev, entry->next);
}

static inline void list_del(struct list_head *entry) {
  __list_del_entry(entry);
  entry->next = LIST_POISON1;
  entry->prev = LIST_POISON2;
}

标签:head,list,next,链表,add,内核,linux,entry,prev
From: https://www.cnblogs.com/ryanos/p/17975351

相关文章

  • /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found问题解决
    有一个go实现的项目代码最近有更新,自己在开发环境上手动构建并运行都没有问题(构建和运行时相同环境,肯定没有问题^_^)。后面通过jenkins构建镜像也没有问题,运行时却报错 之前的版本在jenkins上构建也是成功的,后沟通得知jenkins集群版本最近有更新 那么,大概知道原因了,由于jenk......
  • nginx设置开机启动后配置没生效 | selinux不限制nginx
    1现象给nginx配置负载均衡后,使用/usr/sbin/nginx启动nginx的话,负载均衡有效;但是给nginx配置好了开机启动后,重启机器,nginx正常启动,但是负载均衡不生效了。kill了nginx进程,再使用/usr/sbin/nginx重新启动nginx,负载均衡又有效了。(PS:本人nginx是centos7下yum安装的) 先查询状态sys......
  • Linux中设置只允许特定IP登录你的SSH
    登录Linux服务的工具有putty,xshell等,若要限制特定的IP登录,可以使用一下方式:有几种方式,这里我使用的是其中一种:在此文件/etc/ssh/sshd_config中添加。添加步骤:echo 'AllowUsers [email protected]' >>/etc/ssh/sshd_config        注意:echo的用法搭配......
  • Linux-监控IP频繁登录服务器脚本------------------iptables脚本
    该脚本的作用是监控IP登录失败次数,如果某个IP的登录失败次数超过设定的最大次数,则阻止该IP的进一步登录尝试。通过iptables防火墙阻止连接,当一个IP尝试登录次数超过5次时,iptables会阻止来自该IP的所有连接。#!/bin/bashfunction secrity(){# 设置要监控的登录失败次数,超过......
  • Nessus 10.6 Auto Installer for RHEL 9/AlmaLinux 9/Rocky Linux 9 (updated Jan 202
    Nessus10.6AutoInstallerforRHEL9/AlmaLinux9/RockyLinux9(updatedJan2024)发布Nessus试用版自动化安装程序,支持macOSSonoma、RHEL9和Ubuntu22.04请访问原文链接:https://sysin.org/blog/nessus-auto-install-for-rhel-9/,查看最新版。原创作品,转载请保留出处......
  • Nexpose v6.6.233 for Linux & Windows - 漏洞扫描
    Nexposev6.6.233forLinux&Windows-漏洞扫描Rapid7VulnerabilityManagement,ReleaseJan17,2024请访问原文链接:https://sysin.org/blog/nexpose-6/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org您的本地漏洞扫描程序搜集通过实时覆盖整个网络,随......
  • SecureCRT & SecureFX 9.5 for macOS, Linux, Windows 下载 - 跨平台的多协议终端仿真
    SecureCRT&SecureFX9.5formacOS,Linux,Windows下载-跨平台的多协议终端仿真和文件传输请访问原文链接:SecureCRT&SecureFX9.5formacOS,Linux,Windows,查看最新版。原创作品,转载请保留出处。作者主页:sysin.orgSecureCRT客户端运行于Windows、Mac和Linux,将坚......
  • Linux中的fd
    在Linux中,fd代表文件描述符(filedescriptor)。文件描述符是一个非负整数,用于表示打开的文件、设备、网络连接等的引用。文件描述符提供了一种抽象,使得各种类型的输入/输出操作可以以统一的方式进行处理。通常情况下,Linux中的文件描述符有以下三种类型:标准输入(stdin)的文件描述符......
  • linux下怎么挂载u盘 Linux挂载U盘的方法
    2023-06-04首页 > 软件教程 Linux下怎么挂载U盘?1、查找U盘在Linux操作系统下,如果想要挂载U盘,首先需要找到U盘所在的设备。可以通过以下命令来查看:sudofdisk-l该命令会列出电脑中所有的储存设备,通过观察设备的大小来确定哪一个是U盘。2、创建挂载点为了能够挂载U盘,需......
  • 阿里云rds云数据恢复至自建数据库 (linux 服务器版本ubuntu22.04)
    一、准备1.安装mysql5.7注意:需要跟rds云数据库版本对应2.安装PerconaXtraBackup工具,将解压后的备份文件恢复到自建数据库的数据目录中3.下载需要还原的物理备份文件我的是.qp类型wget-c'https://****.bak.rds.aliyuncs.com/****_xb.qp?****'-Oins2......