libevent,一个非常好的c的网络库,最近开始学习并分析下,做个记录。源码选用的1.4版本。因为感觉这版的代码比较精简,也没有太多宏定义,个人感觉适合学习原理。
从哪里开始呢,我选择从一些最简单的基础的东西开始,由简入繁。
今天就带来libevent的最小堆生成,体会下libevent作者如何实现最小堆的。最小堆用在libevent的时间管理上,来计算是否超时。
最小堆:是一种经过排序的完全二叉树,其中任一非终端节点的数据值均不大于其左子节点和右子节点的值。
1.min_heap_shift_up_ 插入元素后向上调整
2.min_heap_shift_down_ 元素向下调整(删除元素)
3.代码注释
1 /* 2 * Copyright (c) 2006 Maxim Yegorushkin <maxim.yegorushkin@gmail.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 #ifndef _MIN_HEAP_H_ 28 #define _MIN_HEAP_H_ 29 30 #include "event.h" 31 #include "evutil.h" 32 33 //最小堆,是一种经过排序的完全二叉树,其中任一非终端节点的数据值均不大于其左子节点和右子节点的值。 34 typedef struct min_heap 35 { 36 //动态分配内存用来保存指向*event的指针 37 struct event** p; 38 //n为元素个数,a为个数容量 39 unsigned n, a; 40 } min_heap_t; 41 42 static inline void min_heap_ctor(min_heap_t* s); 43 static inline void min_heap_dtor(min_heap_t* s); 44 static inline void min_heap_elem_init(struct event* e); 45 static inline int min_heap_elem_greater(struct event *a, struct event *b); 46 static inline int min_heap_empty(min_heap_t* s); 47 static inline unsigned min_heap_size(min_heap_t* s); 48 static inline struct event* min_heap_top(min_heap_t* s); 49 static inline int min_heap_reserve(min_heap_t* s, unsigned n); 50 static inline int min_heap_push(min_heap_t* s, struct event* e); 51 static inline struct event* min_heap_pop(min_heap_t* s); 52 static inline int min_heap_erase(min_heap_t* s, struct event* e); 53 static inline void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e); 54 static inline void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e); 55 56 int min_heap_elem_greater(struct event *a, struct event *b) 57 { 58 return evutil_timercmp(&a->ev_timeout, &b->ev_timeout, >); 59 } 60 61 void min_heap_ctor(min_heap_t* s) { s->p = 0; s->n = 0; s->a = 0; } 62 void min_heap_dtor(min_heap_t* s) { if(s->p) free(s->p); } 63 void min_heap_elem_init(struct event