RBTree on Linux

Linux内核之rbtree(红黑树) red-black tree(RB树)是一种平衡二叉树,它主要用于存储或者说索引可排序的键值对数据。RB树(红黑树)与radix树和hash表都不同。radix树是一种比较适合用于存储稀疏的数据集而且将用一个大整数进行插入,删除,查找的操作基础。而hash表并不是以某种排序顺序进行存储,而且必须指定大小和hash函数。     RB树与AVL树很相似,但是比AVL树有更好的插入和删除最坏情况的时 阅读详情

linux下内核中本来就有rbtree的实现。在做语义分析的时候,完全可以将所有的符号表组织成一个红黑树。

 

以下复制于linux内核中的rbtree.h以及rbtree.c

 

rbtree.h

 

#ifndef    _LINUX_RBTREE_H
#define    _LINUX_RBTREE_H

#include <linux/kernel.h>
#include <linux/stddef.h>

struct rb_node
{
    unsigned long  rb_parent_color;
#define    RB_RED        0
#define    RB_BLACK    1
    struct rb_node *rb_right;
    struct rb_node *rb_left;
} __attribute__((aligned(sizeof(long))));
    /* The alignment might seem pointless, but allegedly CRIS needs it */

struct rb_root
{
    struct rb_node *rb_node;
};


#define rb_parent(r)   ((struct rb_node *)((r)->rb_parent_color & ~3))
#define rb_color(r)   ((r)->rb_parent_color & 1)
#define rb_is_red(r)   (!rb_color(r))
#define rb_is_black(r) rb_color(r)
#define rb_set_red(r)  do { (r)->rb_parent_color &= ~1; } while (0)
#define rb_set_black(r)  do { (r)->rb_parent_color |= 1; } while (0)

static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
{
    rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
}
static inline void rb_set_color(struct rb_node *rb, int color)
{
    rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
}

#define RB_ROOT    (struct rb_root) { NULL, }
#define    rb_entry(ptr, type, member) container_of(ptr, type, member)

#define RB_EMPTY_ROOT(root)    ((root)->rb_node == NULL)
#define RB_EMPTY_NODE(node)    (rb_parent(node) == node)
#define RB_CLEAR_NODE(node)    (rb_set_parent(node, node))

extern void rb_insert_color(struct rb_node *, struct rb_root *);
extern void rb_erase(struct rb_node *, struct rb_root *);

/* Find logical next and previous nodes in a tree */
extern struct rb_node *rb_next(const struct rb_node *);
extern struct rb_node *rb_prev(const struct rb_node *);
extern struct rb_node *rb_first(const struct rb_root *);
extern struct rb_node *rb_last(const struct rb_root *);

/* Fast replacement of a single node without remove/rebalance/add/rebalance */
extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
                struct rb_root *root);

static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
                struct rb_node ** rb_link)
{
    node->rb_parent_color = (unsigned long )parent;
    node->rb_left = node->rb_right = NULL;

    *rb_link = node;
}

#endif    /* _LINUX_RBTREE_H */

==============================================================

rbtree.c

 

/*
  Red Black Trees
  (C) 1999  Andrea Arcangeli <andrea@suse.de>
  (C) 2002  David Woodhouse <dwmw2@infradead.org>
 
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  linux/lib/rbtree.c
*/

#include <linux/rbtree.h>
#include <linux/module.h>

static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
{
    struct rb_node *right = node->rb_right;
    struct rb_node *parent = rb_parent(node);

    if ((node->rb_right = right->rb_left))
        rb_set_parent(right->rb_left, node);
    right->rb_left = node;

    rb_set_parent(right, parent);

    if (parent)
    {
        if (node == parent->rb_left)
            parent->rb_left = right;
        else
            parent->rb_right = right;
    }
    else
        root->rb_node = right;
    rb_set_parent(node, right);
}

static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
{
    struct rb_node *left = node->rb_left;
    struct rb_node *parent = rb_parent(node);

    if ((node->rb_left = left->rb_right))
        rb_set_parent(left->rb_right, node);
    left->rb_right = node;

    rb_set_parent(left, parent);

    if (parent)
    {
        if (node == parent->rb_right)
            parent->rb_right = left;
        else
            parent->rb_left = left;
    }
    else
        root->rb_node = left;
    rb_set_parent(node, left);
}

void rb_insert_color(struct rb_node *node, struct rb_root *root)
{
    struct rb_node *parent, *gparent;

    while ((parent = rb_parent(node)) && rb_is_red(parent))
    {
        gparent = rb_parent(parent);

        if (parent == gparent->rb_left)
        {
            {
                register struct rb_node *uncle = gparent->rb_right;
                if (uncle && rb_is_red(uncle))
                {
                    rb_set_black(uncle);
                    rb_set_black(parent);
                    rb_set_red(gparent);
                    node = gparent;
                    continue;
                }
            }

            if (parent->rb_right == node)
            {
                register struct rb_node *tmp;
                __rb_rotate_left(parent, root);
                tmp = parent;
                parent = node;
                node = tmp;
            }

            rb_set_black(parent);
            rb_set_red(gparent);
            __rb_rotate_right(gparent, root);
        } else {
            {
                register struct rb_node *uncle = gparent->rb_left;
                if (uncle && rb_is_red(uncle))
                {
                    rb_set_black(uncle);
                    rb_set_black(parent);
                    rb_set_red(gparent);
                    node = gparent;
                    continue;
                }
            }

            if (parent->rb_left == node)
            {
                register struct rb_node *tmp;
                __rb_rotate_right(parent, root);
                tmp = parent;
                parent = node;
                node = tmp;
            }

            rb_set_black(parent);
            rb_set_red(gparent);
            __rb_rotate_left(gparent, root);
        }
    }

    rb_set_black(root->rb_node);
}
EXPORT_SYMBOL(rb_insert_color);

static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
                 struct rb_root *root)
{
    struct rb_node *other;

    while ((!node || rb_is_black(node)) && node != root->rb_node)
    {
        if (parent->rb_left == node)
        {
            other = parent->rb_right;
            if (rb_is_red(other))
            {
                rb_set_black(other);
                rb_set_red(parent);
                __rb_rotate_left(parent, root);
                other = parent->rb_right;
            }
            if ((!other->rb_left || rb_is_black(other->rb_left)) &&
                (!other->rb_right || rb_is_black(other->rb_right)))
            {
                rb_set_red(other);
                node = parent;
                parent = rb_parent(node);
            }
            else
            {
                if (!other->rb_right || rb_is_black(other->rb_right))
                {
                    rb_set_black(other->rb_left);
                    rb_set_red(other);
                    __rb_rotate_right(other, root);
                    other = parent->rb_right;
                }
                rb_set_color(other, rb_color(parent));
                rb_set_black(parent);
                rb_set_black(other->rb_right);
                __rb_rotate_left(parent, root);
                node = root->rb_node;
                break;
            }
        }
        else
        {
            other = parent->rb_left;
            if (rb_is_red(other))
            {
                rb_set_black(other);
                rb_set_red(parent);
                __rb_rotate_right(parent, root);
                other = parent->rb_left;
            }
            if ((!other->rb_left || rb_is_black(other->rb_left)) &&
                (!other->rb_right || rb_is_black(other->rb_right)))
            {
                rb_set_red(other);
                node = parent;
                parent = rb_parent(node);
            }
            else
            {
                if (!other->rb_left || rb_is_black(other->rb_left))
                {
                    rb_set_black(other->rb_right);
                    rb_set_red(other);
                    __rb_rotate_left(other, root);
                    other = parent->rb_left;
                }
                rb_set_color(other, rb_color(parent));
                rb_set_black(parent);
                rb_set_black(other->rb_left);
                __rb_rotate_right(parent, root);
                node = root->rb_node;
                break;
            }
        }
    }
    if (node)
        rb_set_black(node);
}

void rb_erase(struct rb_node *node, struct rb_root *root)
{
    struct rb_node *child, *parent;
    int color;

    if (!node->rb_left)
        child = node->rb_right;
    else if (!node->rb_right)
        child = node->rb_left;
    else
    {
        struct rb_node *old = node, *left;

        node = node->rb_right;
        while ((left = node->rb_left) != NULL)
            node = left;
        child = node->rb_right;
        parent = rb_parent(node);
        color = rb_color(node);

        if (child)
            rb_set_parent(child, parent);
        if (parent == old) {
            parent->rb_right = child;
            parent = node;
        } else
            parent->rb_left = child;

        node->rb_parent_color = old->rb_parent_color;
        node->rb_right = old->rb_right;
        node->rb_left = old->rb_left;

        if (rb_parent(old))
        {
            if (rb_parent(old)->rb_left == old)
                rb_parent(old)->rb_left = node;
            else
                rb_parent(old)->rb_right = node;
        } else
            root->rb_node = node;

        rb_set_parent(old->rb_left, node);
        if (old->rb_right)
            rb_set_parent(old->rb_right, node);
        goto color;
    }

    parent = rb_parent(node);
    color = rb_color(node);

    if (child)
        rb_set_parent(child, parent);
    if (parent)
    {
        if (parent->rb_left == node)
            parent->rb_left = child;
        else
            parent->rb_right = child;
    }
    else
        root->rb_node = child;

 color:
    if (color == RB_BLACK)
        __rb_erase_color(child, parent, root);
}
EXPORT_SYMBOL(rb_erase);

/*
 * This function returns the first node (in sort order) of the tree.
 */
struct rb_node *rb_first(const struct rb_root *root)
{
    struct rb_node    *n;

    n = root->rb_node;
    if (!n)
        return NULL;
    while (n->rb_left)
        n = n->rb_left;
    return n;
}
EXPORT_SYMBOL(rb_first);

struct rb_node *rb_last(const struct rb_root *root)
{
    struct rb_node    *n;

    n = root->rb_node;
    if (!n)
        return NULL;
    while (n->rb_right)
        n = n->rb_right;
    return n;
}
EXPORT_SYMBOL(rb_last);

struct rb_node *rb_next(const struct rb_node *node)
{
    struct rb_node *parent;

    if (rb_parent(node) == node)
        return NULL;

    /* If we have a right-hand child, go down and then left as far
       as we can. */
    if (node->rb_right) {
        node = node->rb_right;
        while (node->rb_left)
            node=node->rb_left;
        return (struct rb_node *)node;
    }

    /* No right-hand children.  Everything down and left is
       smaller than us, so any 'next' node must be in the general
       direction of our parent. Go up the tree; any time the
       ancestor is a right-hand child of its parent, keep going
       up. First time it's a left-hand child of its parent, said
       parent is our 'next' node. */
    while ((parent = rb_parent(node)) && node == parent->rb_right)
        node = parent;

    return parent;
}
EXPORT_SYMBOL(rb_next);

struct rb_node *rb_prev(const struct rb_node *node)
{
    struct rb_node *parent;

    if (rb_parent(node) == node)
        return NULL;

    /* If we have a left-hand child, go down and then right as far
       as we can. */
    if (node->rb_left) {
        node = node->rb_left;
        while (node->rb_right)
            node=node->rb_right;
        return (struct rb_node *)node;
    }

    /* No left-hand children. Go up till we find an ancestor which
       is a right-hand child of its parent */
    while ((parent = rb_parent(node)) && node == parent->rb_left)
        node = parent;

    return parent;
}
EXPORT_SYMBOL(rb_prev);

void rb_replace_node(struct rb_node *victim, struct rb_node *new,
             struct rb_root *root)
{
    struct rb_node *parent = rb_parent(victim);

    /* Set the surrounding nodes to point to the replacement */
    if (parent) {
        if (victim == parent->rb_left)
            parent->rb_left = new;
        else
            parent->rb_right = new;
    } else {
        root->rb_node = new;
    }
    if (victim->rb_left)
        rb_set_parent(victim->rb_left, new);
    if (victim->rb_right)
        rb_set_parent(victim->rb_right, new);

    /* Copy the pointers/colour from the victim to the replacement */
    *new = *victim;
}
EXPORT_SYMBOL(rb_replace_node);

[linux,kernel,rbtree] rb_node 结构体分析 本文主要介绍的是分析 linux 内核中红黑树 rbtree 中的 rb_node 的实现技巧 阅读详情

相关推荐

Linux内核三大核心数据结构:list、rbtree与kfifo工程解析

链表、红黑树和环形缓冲区是操作系统底层开发中最基础且高频使用的数据结构。链表以容器化设计实现零开销泛型管理,支持多链表挂载与类型无关遍历;红黑树提供稳定O(log n)时间复杂度的有序查找与范围查询,广泛应用于内存管理与调度系统;环形缓冲区(kfifo)则通过原子索引与2的幂对齐实现无锁生产者-消费者通信,保障中断上下文下的实时性。这些结构共同构成嵌入式Linux驱动、内存子系统与I/O栈的骨架,其设计兼顾内存效率、并发安全与硬件适配能力。本文深入Linux 5.10+内核源码,结合list_head、kf

weixin_42611177的博客 184

Linux kernel rbtree

1.rbtree.h/* Red Black Trees (C) 1999 Andrea Arcangeli This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as publish

u010758403的专栏 927

Linux C 内核rbtree算法(支持用户态)

rbtree算法内核实现,支持用户态程序使用

一年一年又一年 476

linux内核分析之rbtree的使用

一、理论基础       红黑树是每个节点都带有颜色属性的二叉查找树,颜色为红色或黑色。在二叉查找树强制一般要求以外,对于任何有效的红黑树我们增加了如下的额外要求: 性质1. 节点是红色或黑色。 性质2. 根是黑色。 性质3. 所有叶子都是黑色(叶子是NIL节点)。 性质4. 每个红色节点的两个子节点都是黑色。(从每个叶子到根的所有路径上不能有两个连续的红色节点) 性质5. 从任一节点

Bloom and Grow 1万+

避坑指南:LAMMPS做GCMC模拟时,化学势mu和压力p到底怎么设?

本文详细解析了LAMMPS中巨正则蒙特卡洛(GCMC)模拟的关键参数设置,特别是化学势(mu)与压力(p)的正确计算方法。通过理论分析、实用公式和黄金法则,帮助用户避免常见误区,提升模拟精度和效率,适用于各类复杂体系的分子模拟研究。

weixin_42525289的博客 432

吉比特-笔试

1. 题一:打雷后下雨的概率是80%,而打闪电后下雨的概率是70%,那么即打雷又打闪电后下雨的概率是多少?   答:打雷后下雨和打闪电后下雨都是独立事件,P=1-又打雷又闪电但没下雨的概率= 1 - 0.2 *0.3 = 94%        题二:打雷后下雨的概率是80%,但是没打闪电后下雨的概率是20%,那么打雷了但没打闪电后下雨的概率是多少?答:P=0.8 *0.2 / ( 0.8 *0

naturism的专栏 2497

【Linux编程-数据结构】rbtree

Linux kernel rbtree 因编写内核模块时需要用到rbtree来记录异步request,研究分析了一下kernel rbtree的使用方法,记录于此。本文主要参考了内核文档rbtree.txt rbtree简介 Red-black trees(rbtree)是一种自平衡的二叉搜索树,用于存储可分类的key/value数据对。它不同于radix trees或者hash tables。 radix trees用于有效存储稀疏数组(使用长整型索引进行节点的插入、查询和删除),其索引值太大无法用

B.souls 617

基于LSTM语义分析的企业风险预测系统

在数字化时代,企业面临着前所未有的挑战和机遇,而风险管理作为确保企业稳定发展的关键一环,其重要性日益凸显。为了及时、准确地监测和分析企业相关的风险,并预测可能带来的风险,本文介绍了一种基于长短期记忆(LSTM)语义分析的企业风险预测系统。该系统利用深度学习技术,通过将收集到的企业数据进行预处理和分析,结合LSTM模型对文本进行深度语义分析,从而实现对潜在风险的预测和评估。系统设计方面,该系统包括数据预处理、LSTM模型构建与训练、风险预测以及可视化展示等模块。

qiufangtaowvnyyidtf的博客 1393

Linux内核红黑树(Red-Black Tree or rbtree)算法的实现

平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称 AVL 树。它或者是一棵空树,或者是具有下列性质的二叉树:它的左子树和右子树都是平衡二叉树,且左子树和右子树的深度之差的绝对值不超过1。若将二叉树上结点的平衡因子BF(BalanceFactor)定义为该结点的左子树的深度减去它的右子树的深度,则平衡二叉树上所有结点的平衡因子只可能是-1、0和1。(此段定义来自严蔚敏的《数据结构(C语言版)》) 红黑树是一种在插入或删除结点时都需要维持平衡的二叉查找树

择一事终一生 984

四路独立可控供电USB 2.0智能集线器设计

USB集线器是连接外设的基础接口设备,其供电管理能力直接影响系统可靠性与远程运维效率。传统HUB仅提供固定VBUS输出,缺乏端口级电源控制机制,难以实现软复位、故障隔离与功耗优化。本方案基于USB 2.0协议透明转发原理,通过硬件级电子负载开关(如MT9700)与独立MCU(ESP32-C3)协同,构建具备四路独立限流(680mA)、纳秒级通断响应、WiFi/MQTT远程管控能力的智能供电架构。技术价值体现在协议无侵入、控制域与数据域物理隔离、默认安全上电策略等工程实践要点,广泛适用于工业远程调试、实验室设

weixin_36289742的博客 195

从EXIF到测绘:解码大疆影像元数据的实战应用指南

本文深入解析了大疆无人机影像元数据的实战价值,重点阐述了如何利用EXIF和XMP中的GPS、RTK、姿态角及相机内参等关键信息,提升测绘、三维重建及精准农业等项目的成果精度与效率。通过自动化质检脚本与行业应用场景剖析,指导从业者将元数据从信息转化为生产力,实现从拍照到专业测绘的跨越。

dell8的博客 529

linux 中的rbtree算法

rbtree.h #ifndef    _LINUX_RBTREE_H#define    _LINUX_RBTREE_H#include #include struct rb_node{    unsigned long  rb_parent_color;#define    RB_RED        0#define    RB_BLACK  

stephanxu的专栏 6546

22、预条件技术:矩阵分解与多项式预条件器

本文系统介绍了预条件技术中的不完全分裂法、多项式预条件器和近似逆预条件器。内容涵盖矩阵分解的基本原理、不完全LU和Cholesky分解的实现方法、基于填充水平和阈值策略的改进技术,以及多项式和近似逆预条件器的数学基础与应用。通过算法流程图和表格对比,展示了各类预条件器的适用场景与优缺点,同时探讨了其在迭代求解中的关键作用和未来发展方向。

theta的博客 96

Linux内核中rbtree的使用

在结构体内嵌struct rb_node 定义根节点struct rb_root 把节点依据key加入红黑树 移除节点 遍历红黑树 #include #include #include #define NODES 20 struct test_node { u32 key; struct rb_node rb; u32 val; }; static

雜貨鋪 1393

【物联网技术】ESP8266 ESP-01/01s 固件下载烧录+STM32串口通信接线

ESP8266 ESP-01/01s 工作模式接线与MQTT固件下载烧录接线

嵌入式阿齐Archie 3万+

YOLOv11改进策略【SPPF】| AIFI : 基于Transformer的尺度内特征交互,在降低计算成本的同时提高模型的性能

本文记录的是基于AIFI模块的YOLOv11目标检测改进方法研究。AIFI是RT-DETR中高效混合编码器的一部分,利用其改进YOLOv11模型,

Limiiiing的博客 2241

rbtree(linux)

rbtree.h   #ifndef    _LINUX_RBTREE_H #define    _LINUX_RBTREE_H #include #include struct rb_node {     unsigned long  rb_parent_color; #define    RB_RED        0 #define    RB_BLA

yangzhengyi68的专栏 659

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Linux数据结构之rbtree

rbtree是linux内核红黑树实现。一般在struct中嵌套struct rb_node作为树节点: 1.2 初始化根 将struct rb_root赋值为RB_ROOT: 1.3 插入节点 先通过rb_link_node插入节点,再通过rb_insert_color重新平衡红黑树: 1.4 移除节点 通过rb_erase移除节点: 注:该API会重新平衡红黑树通过rb_entry可将节点转为具体类型:......

fs3296的博客 699

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Linux内核数据结构移植(list和rbtree)

简介list 介绍修改部分list.h 对外的接口使用示例 - 测试 list.h 中所有的list操作rbtree 介绍修改部分rbtree.h 对外接口使用示例 简介 主要移植了内核中的 list,rbtree。使得这2个数据结构在用户态程序中也能使用。同时用 cpputest 对移植后的代码进行了测试。(测试代码其实也是使用这2个数据结构的方法) 内核代码的如下文件:(内核版本 v3.2 ...

Stay Hungry, Stay Foolish 1113

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

rbtree使用

从linux内核源码中获取rbtree代码,应用层修改使用

zhanglong71的博客 198

ARM Linux Rbtree

2.node->rb_left node->rb_right,指向的指针都是4字节对齐的,所以rb_link_node(&de->subdir_node, parent, new);是判断parent->__rb_parent_color的低2bit。也就是节点的地址,是4字节对齐的,节点的颜色是保存在__rb_parent_color成员中。1.rb_red_parent表示获取红节点的父节点,红节点的__rb_parent_color就是其父节点的地址。

c18976636326的博客 202

linux-rbtree

linux的

coldsnow33的专栏 1790

Kernel数据结构移植(list和rbtree)

https://www.cnblogs.com/wang_yb/p/3818517.html   主要移植了内核中的 list,rbtree。使得这2个数据结构在用户态程序中也能使用。 同时用 cpputest 对移植后的代码进行了测试。(测试代码其实也是使用这2个数据结构的方法)   内核代码的如下文件:(内核版本 v3.2 debian 7.5源码) include/linux/l...

EminentBoy的博客 203

Linux内核的红黑树源码实现以及调用

红黑树可以说是程序员经常遇到的一种数据结构,不管是工作还是面试都会涉及,有时候还会让你写一段红黑树代码。 本文主要是讲Linux中的红黑树,关于红黑树定义参考wiki:https://en.wikipedia.org/wiki/Red%E2%80%93black_tree 其中《算法导论》中的定义最为清晰: 红黑树图示: Linux中的红黑树(rbtree) 下面...

深度Java 8060
上一篇: just a log
下一篇: Linux内核文档之rbtree.txt
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值