Libevent源码分析(一):最小堆
Libevent中的timeout事件是使用最小堆来管理维护的.代码位于<minheap-internal.h>.
看函数命名和代码风格应该是一个C++程序员,函数名都挺好懂的,只是下面这个结构体变量命名比较坑....
typedef struct min_heap
{
struct event** p;
unsigned n, a;//n队列元素的多少,a代表队列空间的大小.
} min_heap_t;
注释是我加的,这命名,n啊a啊的,鬼知道啥意思....必须吐槽一下.
先来说说什么是最小堆:
1.堆是一个二叉树
2.最小堆:父节点的值总是小于等于子节点
如下图:

上图圆圈旁边的数字代表其在数组中的下标.堆一般是用数组来存储的,也就是说实际存储结构是连续的,只是逻辑上是一棵树的结构.这样做的好处是很容易找到堆顶的元素,对Libevent来说,很容易就可以找到距当前时间最近的timeout事件.
现在想想看我们要插入一个元素,我们要怎么移动数组中元素的位置,使其逻辑上仍然是一个小堆?结合下图很容易看出来:
1.假设我们要插入的元素为6大于其父节点的值2.则把元素放在数组相应的index上,插入完成.

2.假设我们要插入的为2小于其父节点的值3.则交换该节点与其父节点的值.对于下图来说,交换完毕后插入就算完成了.那要是交换完后发现index=2的元素还小于其父节点index=0的呢?就又得在一次交换,如此循环,直到达到根节点或是其不大于父节点.


到了这里我们看看libevent里的实现代码就很清楚了
int min_heap_push(min_heap_t* s, struct event* e)
{
if (min_heap_reserve(s, s->n + ))
return -;
min_heap_shift_up_(s, s->n++, e);
return ;
} //分配队列大小.n代表队列元素个数多少.
int min_heap_reserve(min_heap_t* s, unsigned n)
{
if (s->a < n) //队列大小不足元素个数,重新分配空间.
{
struct event** p;
unsigned a = s->a ? s->a * : ; //初始分配8个指针大小空间,否则原空间大小翻倍.
if (a < n)
a = n; //翻倍后空间依旧不足,则分配n.
if (!(p = (struct event**)mm_realloc(s->p, a * sizeof *p))) //重新分配内存
return -;
s->p = p; //重新赋值队列地址及大小.
s->a = a; //
}
return ;
} void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e)
{
unsigned parent = (hole_index - ) / ;
while (hole_index && min_heap_elem_greater(s->p[parent], e)) //比父节点小或是到达根节点.则交换位置.循环.
{
(s->p[hole_index] = s->p[parent])->ev_timeout_pos.min_heap_idx = hole_index;
hole_index = parent;
parent = (hole_index - ) / ;
}
(s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
}
实际上作者写了一个比较通用的函数min_heap_shift_up(),与之相对应的还有min_heap_shift_down()
void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e)
{
unsigned min_child = * (hole_index + );
while (min_child <= s->n)
{
//找出较小子节点
min_child -= min_child == s->n || min_heap_elem_greater(s->p[min_child], s->p[min_child - ]);
//比子节点小正常.不需要再交换位置,跳出循环.
if (!(min_heap_elem_greater(e, s->p[min_child])))
break;
//比子节点大,要交换位置
(s->p[hole_index] = s->p[min_child])->ev_timeout_pos.min_heap_idx = hole_index;
hole_index = min_child;
min_child = * (hole_index + );
}
(s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
}
这里的hole_index是我们要填入某个值的下标,e是要填入的值.还是画图比较好理解:

当这个值(下标为hole_Index=1)比其父节点1(index = 0)小时,要向上移动调整.
当这个值(下标为hole_Index=1)比其最小子节点6(index = 3)还大时,要向下移动调整.
Libevent里是这么使用他们的:
int min_heap_erase(min_heap_t* s, struct event* e)
{
if (- != e->ev_timeout_pos.min_heap_idx)
{
struct event *last = s->p[--s->n];//把最后一个值作为要填入hole_index的值
unsigned parent = (e->ev_timeout_pos.min_heap_idx - ) / ;
/* we replace e with the last element in the heap. We might need to
shift it upward if it is less than its parent, or downward if it is
greater than one or both its children. Since the children are known
to be less than the parent, it can't need to shift both up and
down. */
if (e->ev_timeout_pos.min_heap_idx > && min_heap_elem_greater(s->p[parent], last))
min_heap_shift_up_(s, e->ev_timeout_pos.min_heap_idx, last);
else
min_heap_shift_down_(s, e->ev_timeout_pos.min_heap_idx, last);
e->ev_timeout_pos.min_heap_idx = -;
return ;
}
return -;
}
struct event* min_heap_pop(min_heap_t* s)
{
if (s->n)
{
struct event* e = *s->p;
min_heap_shift_down_(s, 0u, s->p[--s->n]);
e->ev_timeout_pos.min_heap_idx = -;
return e;
}
return ;
}
最后总结一下,由于堆这种结构在逻辑上的这种二叉树的关系,其插入也好,删除也好,就是一个与父节点或是子节点比较然后调整位置,这一过程循环往复直到达到边界条件的过程.记住这一点,就不难写出代码了.
二叉树节点i:父节点为(i-1)/2.子节点为2i+1,2(i+1)。
Libevent源码分析(一):最小堆的更多相关文章
- 【转】libevent源码分析
libevent源码分析 转自:http://www.cnblogs.com/hustcat/archive/2010/08/31/1814022.html 这两天没事,看了一下Memcached和l ...
- Libevent源码分析 (1) hello-world
Libevent源码分析 (1) hello-world ⑨月份接触了久闻大名的libevent,当时想读读源码,可是由于事情比较多一直没有时间,现在手头的东西基本告一段落了,我准备读读libeven ...
- libevent源码分析
这两天没事,看了一下Memcached和libevent的源码,做个小总结. 1.入门 1.1.概述Libevent是一个用于开发可扩展性网络服务器的基于事件驱动(event-driven)模型的网络 ...
- libevent源码分析二--timeout事件响应
libevent不仅支持io事件,同时还支持timeout事件与signal事件,这篇文件将分析libevent是如何组织timeout事件以及如何响应timeout事件. 1. min_heap ...
- Libevent源码分析系列【转】
转自:https://www.cnblogs.com/zxiner/p/6919021.html 1.使用libevent库 源码那么多,该怎么分析从哪分析呢?一个好的方法就是先用起来,会用了 ...
- Libevent源码分析系列
1.使用libevent库 源码那么多,该怎么分析从哪分析呢?一个好的方法就是先用起来,会用了,然后去看底层相应的源码,这样比较有条理,自上向下掌握.下面用libevent库写个程序,每隔1秒 ...
- Libevent源码分析—event_base_dispatch()
我们知道libevent是一个Reactor模式的事件驱动的网络库. 到目前为止,我们已经看了核心的event和event_base结构体的源码,看了初始化这两个结构体的源码,看了注册event的 ...
- Libevent源码分析—event_init()
下面开始看初始化event_base结构的相关函数.相关源码位于event.c event_init() 首先调用event_init()初始化event_base结构体 struct event_b ...
- Libevent源码分析—event, event_base
event和event_base是libevent的两个核心结构体,分别是反应堆模式中的Event和Reactor.源码分别位于event.h和event-internal.h中 1.event: s ...
随机推荐
- 记 tower.im 的一次重构
原文in here: http://outofmemory.cn/wr?u=http%3A%2F%2Fblog.mycolorway.com%2F2013%2F05%2F01%2Ftower-refa ...
- 读取word文件.选择了TextParse
待续! 代码还没分离出来.. 分离后会上传上来 不支持wps 文件 . ]]>
- h.264 FMO
在H.264之前的标准中,比如H.263,其比特流中的数据是按照一个宏块接一个宏块的方式排列的,一旦发生丢包,很多相邻宏块信息都会丢失,很难进行错误隐藏处理.在H.264中加入了一项新特性:把宏块在比 ...
- -_-#【Mac】命令
切换cd $HOMEcd ~ 查看ls 查看系统显示:defaults write com.apple.finder AppleShowAllFiles -bool true隐藏:defaults w ...
- Sum Root to Leaf Numbers——LeetCode
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 尚学堂 JAVA Day1 概念总结
1.什么是计算机语言?一些符号,这些符号按照计算机硬件结构可以理解的方式排列组合,方便人与计算机,计算机与计算机之间进行信息的交换. 2.什么是机器语言?就是简单的二进制0和1的组合.该语言是计算机硬 ...
- php开启错误提示
1.在php.ini文件里加上下面两句 display_errors = Onerror_reporting = E_ALL | E_STRICT 2.在Apache的 httpd.conf文件里加上 ...
- eclipse 错误: 找不到或无法加载主类
在src文件夹上移除Source Folder,再点右键-Build Path-Use as Source Folder,重新进行编译,一切正常了.
- Android---优化下载让网络访问更高效(三)
批处理传输和连接 每次启动一个连接---跟传输的数据大小无关---在使用典型的3G无线信号时,就会潜在的导致无线信号消耗近20秒的电量. 如果一个应用程序每隔20秒ping一次服务器,只是告知该应用程 ...
- Android BaseAdapter Gallery 画廊视图 (左右拖动图片列表拖至中间时图片放大显示)
画廊视图使用Gallery表示,能够按水平方向显示内容,并且可以手指直接拖动图片和移动,一般用来浏览图片,,被选中的选项位于中间,并且可以响应事件显示信息.在使用画廊视图时,首先在屏幕上添加Galle ...