转载:

http://blog.csdn.net/wuhzossibility/article/details/8079025

http://blog.chinaunix.net/uid-27717694-id-4286337.html

内核通知链

1.1. 概述

Linux内核中各个子系统相互依赖,当其中某个子系统状态发生改变时,就必须使用一定的机制告知使用其服务的其他子系统,以便其他子系统采取相应的措施。为满足这样的需求,内核实现了事件通知链机制(notificationchain)。

通知链只能用在各个子系统之间,而不能在内核和用户空间进行事件的通知。组成内核的核心系统代码均位于kernel目录下,通知链表位于 kernel/notifier.c中,对应的头文件为include/linux/notifier.h。通知链表机制并不复杂,实现它的代码只有区区 几百行。

事件通知链表是一个事件处理函数的列表,每个通知链都与某个或某些事件有关,当特定的事件发生时,就调用相应的事件通知链中的回调函数,进行相应的处理。

1.2.数据结构

如图 1中所示,Linux的网络子系统一共有3个通知链:表示ipv4地址发生变化时的inetaddr_chain;表示ipv6地址发生变化的inet6addr_chain;还有表示设备注册、状态变化的netdev_chain。

在这些链中都是一个个notifier_block结构:

  1. struct notifier_block {
  2. int (*notifier_call)(struct notifier_block *, unsigned long, void *);
  3. struct notifier_block *next;
  4. int priority;
  5. };

其中,

1.     notifier_call:当相应事件发生时应该调用的函数,由被通知方提供,如other_subsys_1;

2.     notifier_block *next:用于链接成链表的指针;

3.     priority:回调函数的优先级,一般默认为0。

内核代码中一般把通知链命名为xxx_chain, xxx_nofitier_chain这种形式的变量名。围绕核心数据结构notifier_block,内核定义了四种通知链类型:

1.  原子通知链( Atomic notifier chains ):通知链元素的回调函数(当事件发生时要执行的函数)在中断或原子操作上下文中运行,不允许阻塞。对应的链表头结构:

  1. struct atomic_notifier_head {
  2. spinlock_t lock;
  3. struct notifier_block *head;
  4. };

2.  可阻塞通知链( Blocking notifier chains ):通知链元素的回调函数在进程上下文中运行,允许阻塞。对应的链表头:

  1. struct blocking_notifier_head {
  2. struct rw_semaphore rwsem;
  3. struct notifier_block *head;
  4. };

3.     原始通知链( Raw notifierchains ):对通知链元素的回调函数没有任何限制,所有锁和保护机制都由调用者维护。对应的链表头:

网络子系统就是该类型,通过以下宏实现head的初始化

  1. static RAW_NOTIFIER_HEAD(netdev_chain);
  2. #define RAW_NOTIFIER_INIT(name) { \
  3. .head= NULL }
  4. #define RAW_NOTIFIER_HEAD(name) \ //调用他就好了
  5. struct raw_notifier_head name = \
  6. RAW_NOTIFIER_INIT(name)
  7. 即:
  8. struct raw_notifier_head netdev_chain = {
  9. .head = NULL;
  10. }

而其回调函数的注册,比如向netdev_chain的注册函数:register_netdevice_notifier。

  1. struct raw_notifier_head {
  2. struct notifier_block *head;
  3. };

4.  SRCU 通知链( SRCU notifier chains ):可阻塞通知链的一种变体。对应的链表头:

  1. struct srcu_notifier_head {
  2. struct mutex mutex;
  3. struct srcu_struct srcu;
  4. struct notifier_block *head;
  5. };

1.3.  运行机理

被通知一方(other_subsys_x)通过notifier_chain_register向特定的chain注册回调函数,并且一般而言特 定的子系统会用特定的notifier_chain_register包装函数来注册,比如路由子系统使用的是网络子系统 的:register_netdevice_notifier来注册他的notifier_block。

1.3.1.  向事件通知链注册的步骤

1. 申明struct notifier_block结构

2. 编写notifier_call函数

3. 调用特定的事件通知链的注册函数,将notifier_block注册到通知链中

如果内核组件需要处理够某个事件通知链上发出的事件通知,其就该在初始化时在该通知链上注册回调函数。

1.3.2.  通知子系统有事件发生

inet_subsys是通过notifier_call_chain来通知其他的子系统(other_subsys_x)的。

notifier_call_chain会按照通知链上各成员的优先级顺序执行回调函数(notifier_call_x);回调函数的执行现场在 notifier_call_chain进程地址空间;其返回值是NOTIFY_XXX的形式,在include/linux/notifier.h中:

  1. #define NOTIFY_DONE 0x0000 /* 对事件视而不见 */
  2. #define NOTIFY_OK 0x0001 /* 事件正确处理 */
  3. #define NOTIFY_STOP_MASK 0x8000 /*由notifier_call_chain检查,看继续调用回调函数,还是停止,_BAD和_STOP中包含该标志 */
  4. #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002) /*事件处理出错,不再继续调用回调函数 */
  5. /*
  6. *Clean way to return from the notifier and stop further calls.
  7. */
  8. #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK) /* 回调出错,不再继续调用该事件回调函数 */

notifier_call_chain捕获并返回最后一个事件处理函数的返回值;注意:notifier_call_chain可能同时被不同的cpu调用,故而调用者必须保证互斥。

1.3.3.  事件列表

对于网络子系统而言,其事件常以NETDEV_XXX命名;描述了网络设备状态(dev->flags)、传送队列状态 (dev->state)、设备注册状态(dev->reg_state),以及设备的硬件功能特性(dev->features):

include/linux/notifier.h中

  1. /* netdevice notifier chain */
  2. #define NETDEV_UP 0x0001 /* 激活一个网络设备 */
  3. #define NETDEV_DOWN 0x0002f /* 停止一个网络设备,所有对该设备的引用都应释放 */
  4. #define NETDEV_REBOOT 0x0003 /* 检查到网络设备接口硬件崩溃,硬件重启 */
  5. #define NETDEV_CHANGE 0x0004 /* 网络设备的数据包队列状态发生改变 */
  6. #define NETDEV_REGISTER 0x0005 /*一个网络设备事例注册到系统中,但尚未激活 */
  7. #define NETDEV_UNREGISTER 0x0006 /*网络设备驱动已卸载 */
  8. #define NETDEV_CHANGEMTU 0x0007 /*MTU发生了改变 */
  9. #define NETDEV_CHANGEADDR 0x0008 /*硬件地址发生了改变 */
  10. #define NETDEV_GOING_DOWN 0x0009 /*网络设备即将注销,有dev->close报告,通知相关子系统处理 */
  11. #define NETDEV_CHANGENAME 0x000A /*网络设备名改变 */
  12. #define NETDEV_FEAT_CHANGE 0x000B /*feature网络硬件功能改变 */
  13. #define NETDEV_BONDING_FAILOVER 0x000C /* */
  14. #define NETDEV_PRE_UP 0x000D /* */
  15. #define NETDEV_BONDING_OLDTYPE 0x000E /* */
  16. #define NETDEV_BONDING_NEWTYPE 0x000F /* */

1.4.  简单一例:

通过上面所述,notifier_chain机制只能在内核个子系统间使用,因此,这里使用3个模块:test_notifier_chain_0、 test_notifier_chain_1、test_notifier_chain_2;当 test_notifier_chain_2通过module_init初始化模块时发出事件TESTCHAIN_2_INIT;然后 test_notifier_chain_1作出相应的处理:打印 test_notifier_chain_2正在初始化。

  1. /* test_chain_0.c :0. 申明一个通知链;1. 向内核注册通知链;2. 定义事件; 3. 导出符号,因而必需最后退出*/
  2.  
  3. #include <linux/notifier.h>
  4. #include <linux/module.h>
  5. #include <linux/init.h>
  6. #include <linux/kernel.h> /* printk() */
  7. #include <linux/fs.h> /* everything() */
  8.  
  9. #define TESTCHAIN_INIT 0x52U
  10. static RAW_NOTIFIER_HEAD(test_chain);
  11.  
  12. /* define our own notifier_call_chain */
  13. static int call_test_notifiers(unsigned long val, void *v)
  14. {
  15. return raw_notifier_call_chain(&test_chain, val, v);
  16. }
  17. EXPORT_SYMBOL(call_test_notifiers);
  18.  
  19. /* define our own notifier_chain_register func */
  20. static int register_test_notifier(struct notifier_block *nb)
  21. {
  22. int err;
  23. err = raw_notifier_chain_register(&test_chain, nb);
  24.  
  25. if(err)
  26. goto out;
  27.  
  28. out:
  29. return err;
  30. }
  31.  
  32. EXPORT_SYMBOL(register_test_notifier);
  33.  
  34. static int __init test_chain_0_init(void)
  35. {
  36. printk(KERN_DEBUG "I'm in test_chain_0\n");
  37.  
  38. return ;
  39. }
  40.  
  41. static void __exit test_chain_0_exit(void)
  42. {
  43. printk(KERN_DEBUG "Goodbye to test_chain_0\n");
  44. // call_test_notifiers(TESTCHAIN_EXIT, (int *)NULL);
  45. }
  46.  
  47. MODULE_LICENSE("GPL v2");
  48. MODULE_AUTHOR("fishOnFly");
  49.  
  50. module_init(test_chain_0_init);
  51. module_exit(test_chain_0_exit);
  52.  
  53. /* test_chain_1.c :1. 定义回调函数;2. 定义notifier_block;3. 向chain_0注册notifier_block;*/
  54. #include <linux/notifier.h>
  55. #include <linux/module.h>
  56. #include <linux/init.h>
  57.  
  58. #include <linux/kernel.h> /* printk() */
  59. #include <linux/fs.h> /* everything() */
  60.  
  61. extern int register_test_notifier(struct notifier_block *nb);
  62. #define TESTCHAIN_INIT 0x52U
  63.  
  64. /* realize the notifier_call func */
  65. int test_init_event(struct notifier_block *nb, unsigned long event,
  66. void *v)
  67. {
  68. switch(event){
  69. case TESTCHAIN_INIT:
  70. printk(KERN_DEBUG "I got the chain event: test_chain_2 is on the way of init\n");
  71. break;
  72.  
  73. default:
  74. break;
  75. }
  76.  
  77. return NOTIFY_DONE;
  78. }
  79. /* define a notifier_block */
  80. static struct notifier_block test_init_notifier = {
  81. .notifier_call = test_init_event,
  82. };
  83. static int __init test_chain_1_init(void)
  84. {
  85. printk(KERN_DEBUG "I'm in test_chain_1\n");
  86. register_test_notifier(&test_init_notifier);<span style="white-space:pre"> </span>// chain_0提供的设施
  87. return ;
  88. }
  89.  
  90. static void __exit test_chain_1_exit(void)
  91. {
  92. printk(KERN_DEBUG "Goodbye to test_clain_l\n");
  93. }
  94.  
  95. MODULE_LICENSE("GPL");
  96. MODULE_AUTHOR("fishOnFly");
  97.  
  98. module_init(test_chain_1_init);
  99. module_exit(test_chain_1_exit);
  100.  
  101. /* test_chain_2.c:发出通知链事件*/
  102.  
  103. #include <linux/notifier.h>
  104. #include <linux/module.h>
  105. #include <linux/init.h>
  106. #include <linux/kernel.h> /* printk() */
  107. #include <linux/fs.h> /* everything() */
  108.  
  109. extern int call_test_notifiers(unsigned long val, void *v);
  110. #define TESTCHAIN_INIT 0x52U
  111.  
  112. static int __init test_chain_2_init(void)
  113. {
  114. printk(KERN_DEBUG "I'm in test_chain_2\n");
  115. call_test_notifiers(TESTCHAIN_INIT, "no_use");
  116.  
  117. return ;
  118. }
  119.  
  120. static void __exit test_chain_2_exit(void)
  121. {
  122. printk(KERN_DEBUG "Goodbye to test_chain_2\n");
  123. }
  124.  
  125. MODULE_LICENSE("GPL v2");
  126. MODULE_AUTHOR("fishOnFly");
  127.  
  128. module_init(test_chain_2_init);
  129. module_exit(test_chain_2_exit);
  130.  
  131. # Makefile
  132.  
  133. # Comment/uncomment the following line to disable/enable debugging
  134. # DEBUG = y
  135.  
  136. # Add your debugging flag (or not) to CFLAGS
  137. ifeq ($(DEBUG),y)
  138. DEBFLAGS = -O -g -DSCULL_DEBUG # "-O" is needed to expand inlines
  139. else
  140. DEBFLAGS = -O2
  141. endif
  142.  
  143. ifneq ($(KERNELRELEASE),)
  144. # call from kernel build system
  145.  
  146. obj-m := test_chain_0.o test_chain_1.o test_chain_2.o
  147.  
  148. else
  149.  
  150. KERNELDIR ?= /lib/modules/$(shell uname -r)/build
  151. PWD := $(shell pwd)
  152.  
  153. modules:
  154. $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
  155.  
  156. endif
  157.  
  158. clean:
  159. rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
  160.  
  161. depend .depend dep:
  162. $(CC) $(CFLAGS) -M *.c > .depend
  163.  
  164. ifeq (.depend,$(wildcard .depend))
  165. include .depend
  166. endif
  167.  
  168. [wang2@iwooing: notifier_chian]$ sudo insmod./test_chain_0.ko
  169. [wang2@iwooing: notifier_chian]$ sudo insmod./test_chain_1.ko
  170. [wang2@iwooing: notifier_chian]$ sudo insmod./test_chain_2.ko
  171.  
  172. [wang2@iwooing: notifier_chian]$ dmesg
  173.  
  174. [ 5950.112649] I'm in test_chain_0
  175. [ 5956.766610] I'm in test_chain_1
  176. [ 5962.570003] I'm in test_chain_2
  177. [ 5962.570008] I got the chain event: test_chain_2 is on the way of init
  178.  
  179. [ 6464.042975] Goodbye to test_chain_2
  180. [ 6466.368030] Goodbye to test_clain_l
  181. [ 6468.371479] Goodbye to test_chain_0

一、概述

内核许多子系统之间关联紧密,因此在一个子系统发生或者检测到的事件信息很可能对其他子系统来说也是有价值的。为了满足其他子系统对这些事件信息的需求,即在某个子系统内发生或检测到事件时,其他对此感兴趣的子系统也能知道事件的发生,内核提供了notification chain机制。
注意:notification chain适用于内核子系统之间的信息传递,不涉及用户态。

二、结构体

1. notifier_block
//action参数表示发生的事件类型,因为一个chain可能支持多个事件,此参数用来对事件进行区分
//void *data用来存放私有信息,其具体信息取决于特定的事件

  1. typedef int (*notifier_fn_t)(struct notifier_block *nb,unsigned long action, void *data);
  2.  
  3. struct notifier_block {
  4. notifier_fn_t notifier_call;//回调函数
  5. struct notifier_block __rcu *next; //用于同一个chain中的notifier_block的链接
  6. int priority;//表示notifier_call函数的优先级,在事件发生时先调用高优先级的回调函数。
  7. };

notifier_call返回值为int类型,可能的返回值(include/linux/notifier.h文件中定义了这些常值)

  1. //对该事件不感兴趣
  2. #define NOTIFY_DONE 0x0000
  3. //成功响应该事件
  4. #define NOTIFY_OK 0x0001
  5. //该回调函数返回后停止处理后续notifier block
  6. #define NOTIFY_STOP_MASK 0x8000
  7. //出错,回调函数返回后停止处理后续notifier block
  8. #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
  9. //成功响应事件,回调函数返回后停止处理后续notifier block
  10. #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)

2. 围绕核心数据结构notifier_block,内核定义了四种通知链类型:
1)原子通知链( Atomic notifier chains ):通知链元素的回调函数(当事件发生时要执行的函数)在中断或原子操作上下文中运行,不允许阻塞。对应的链表头结构:

  1. struct atomic_notifier_head {
  2. spinlock_t lock;
  3. struct notifier_block __rcu *head;
  4. };

2)可阻塞通知链( Blocking notifier chains ):通知链元素的回调函数在进程上下文中运行,允许阻塞。对应的链表头:

  1. struct blocking_notifier_head {
  2. struct rw_semaphore rwsem;
  3. struct notifier_block __rcu *head;
  4. };

3)原始通知链( Raw notifierchains ):对通知链元素的回调函数没有任何限制,所有锁和保护机制都由调用者维护。对应的链表头:

  1. struct raw_notifier_head {
  2. struct notifier_block __rcu *head;
  3. };

4)SRCU 通知链( SRCU notifier chains ):可阻塞通知链的一种变体。对应的链表头:

  1. struct srcu_notifier_head {
  2. struct mutex mutex;
  3. struct srcu_struct srcu;
  4. struct notifier_block __rcu *head;
  5. };

三、操作过程

被通知一方(other_subsys_x)通过notifier_chain_register向特定的chain注册回调函数,并且一般而言特定的子系统会用特定的notifier_chain_register包装函数来注册,比如路由子系统使用的是网络子系统的:register_netdevice_notifier来注册他的notifier_block。
1.向事件通知链注册的步骤
(1)申明struct notifier_block结构
(2)编写notifier_call函数
(3)调用特定的事件通知链的注册函数,通过notifier_chain_register()将notifier_block注册到通知链中。实际上notification chain就是一组函数列表。通常notification chain的名字的格式为xxx_chain、xxx_notifier_chain、 xxx_notifier_list,例如reboot_notifier_list。

  1. static int notifier_chain_register(struct notifier_block **nl,struct notifier_block *n)
  2. {
  3. while ((*nl) != NULL) {
  4. //判断优先权值, 优先权值越大位置越靠前
  5. if (n->priority > (*nl)->priority)
  6. break;
  7. nl = &((*nl)->next);
  8. }
  9. n->next = *nl;//将节点n链接到链表nl中的合适位置
  10. rcu_assign_pointer(*nl, n);
  11. return ;
  12. }

2.通知子系统有事件发生
如果内核组件需要处理够某个事件通知链上发出的事件通知,其就该在初始化时在该通知链上注册回调函数。
inet_subsys是通过notifier_call_chain来通知其他的子系统的。notifier_call_chain会按照通知链上各成员的优先级顺序执行回调函数(notifier_call_x);回调函数的执行现场在notifier_call_chain进程地址空间.

  1. static int __kprobes notifier_call_chain(struct notifier_block **nl,unsigned long val, void *v,
  2. int nr_to_call, int *nr_calls)
  3. {
  4. int ret = NOTIFY_DONE;
  5. struct notifier_block *nb, *next_nb;
  6. //取通知链中的notifier_block
  7. nb = rcu_dereference_raw(*nl);
  8.  
  9. while (nb && nr_to_call) {
  10. next_nb = rcu_dereference_raw(nb->next);
  11. //调用回调函数
  12. ret = nb->notifier_call(nb, val, v);
  13.  
  14. if (nr_calls)
  15. (*nr_calls)++;
  16.  
  17. if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
  18. break;
  19. nb = next_nb;
  20. nr_to_call--;
  21. }
  22. return ret;
  23. }

该函数将一个通知块结构从通知链表中拆除:
static int notifier_chain_unregister(struct notifier_block **nl,struct notifier_block *n)

四、实例

1. 定义通知链并初始化

  1. static struct srcu_notifier_head cpufreq_transition_notifier_list;
  2. srcu_init_notifier_head(&cpufreq_transition_notifier_list);
  3. void srcu_init_notifier_head(struct srcu_notifier_head *nh)
  4. {
  5. mutex_init(&nh->mutex);
  6. if (init_srcu_struct(&nh->srcu) < )
  7. BUG();
  8. nh->head = NULL;
  9. }

2. 定义结构体和回调函数

  1. static struct notifier_block __clk_cpufreq_notifier_block = {
  2. .notifier_call = __clk_cpufreq_notifier
  3. };
  4.  
  5. static int __clk_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
  6. {
  7. printk("dump cpu freq\n",);
  8. return ;
  9. }

2. 注册
调用封装函数cpufreq_register_notifier(&__clk_cpufreq_notifier_block,CPUFREQ_TRANSITION_NOTIFIER)注册

  1. int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
  2. {
  3. int ret;
  4.  
  5. WARN_ON(!init_cpufreq_transition_notifier_list_called);
  6.  
  7. switch (list) {
  8. case CPUFREQ_TRANSITION_NOTIFIER:
  9. //将我们定义的notifier_block结构注册到cpufreq_transition_notifier_list链中
  10. ret = srcu_notifier_chain_register(&cpufreq_transition_notifier_list, nb);
  11. break;
  12. case CPUFREQ_POLICY_NOTIFIER:
  13. ret = blocking_notifier_chain_register(
  14. &cpufreq_policy_notifier_list, nb);
  15. break;
  16. default:
  17. ret = -EINVAL;
  18. }
  19.  
  20. return ret;
  21. }

3.通知事件发生

  1. void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
  2. {
  3. struct cpufreq_policy *policy;
  4.  
  5. BUG_ON(irqs_disabled());
  6.  
  7. freqs->flags = cpufreq_driver->flags;
  8. pr_debug("notification %u of frequency transition to %u kHz\n",state, freqs->new);
  9.  
  10. policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
  11. switch (state) {
  12. case CPUFREQ_PRECHANGE:
  13. //......
  14. //向通知链进行事件通知,最终会调用notifier_call_chain()函数,最终执行回调函数
  15. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,CPUFREQ_PRECHANGE, freqs);
  16. adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
  17. break;
  18.  
  19. //.......
  20. }
  21. }

Linux内核基础--事件通知链(notifier chain)的更多相关文章

  1. Linux内核基础--事件通知链(notifier chain)good【转】

    转自:http://www.cnblogs.com/pengdonglin137/p/4075148.html 阅读目录(Content) 1.1. 概述 1.2.数据结构 1.3.  运行机理 1. ...

  2. Linux内核基础--事件通知链(notifier chain)【转】

    转自:http://blog.csdn.net/wuhzossibility/article/details/8079025 内核通知链 1.1. 概述 Linux内核中各个子系统相互依赖,当其中某个 ...

  3. linux kernel notifier chain(事件通知链)

    Linux内核中各个子系统相互依赖,当其中某个子系统状态发生改变时,就必须使用一定的机制告知使用其服务的其他子系统,以便其他子系统采取相应的措施.为满足这样的需求,内核实现了事件通知链机制(notif ...

  4. Linux内核基础优化

    Linux内核基础优化 net.ipv4.ip_forward = 1 #开启网络转发 net.ipv4.conf.default.rp_filter = 0 #开启代理功能 net.ipv4.con ...

  5. [Linux] 内核通知链 notifier

    Linux 内核中每个模块之间都是独立的,如果模块需要感知其他模块的事件,就需要用到内核通知链. 最典型的通知链应用就是 LCD 和 TP 之间,TP 需要根据 LCD 的亮灭来控制是否打开关闭触摸功 ...

  6. linux内核基础(系统调用,简明)

    内核基础(系统调用) 在说系统调用之前.先来说说内核是怎么和我们交互的.或者说是怎么和我们产生交集的. 首先,内核是用来控制硬件的仅仅有内核才干直接控制硬件,所以说内核非常重要,假设内核被控制那么电脑 ...

  7. Linux内核基础

            Linux系统运行的应用程序通过系统调用来与内核通信.应用程序通常调用库函数(比如C库函数)再有库函数通过系统调用界面,让内核带其完成各种不同的任务. 下面这张图显示的就是应用程序,内 ...

  8. Jquery | 基础 | 事件的链式写法

    $(".title").click(function () { $(this).addClass("curcol").next(".content&q ...

  9. Linux 内核热插拔事件产生

    一个热插拔事件是一个从内核到用户空间的通知, 在系统配置中有事情已经改变. 无论何 时一个 kobject 被创建或销毁就产生它们. 这样事件被产生, 例如, 当一个数字摄像头 使用一个 USB 线缆 ...

随机推荐

  1. CSS样式补充代码

    CSS符号属性: list-style-type:none; /*不编号*/ list-style-type:decimal; /*阿拉伯数字*/ list-style-type:lower-roma ...

  2. 设计模式之装饰模式(Decorator)

    装饰模式原理:给对象增加特性,这种特性是一种累加的效果 代码如下 #include <iostream> #include <string> #include <list ...

  3. 【BZOJ】【1047】【HAOI2007】理想的正方形

    DP/单调队列优化 一眼看上去就是DP 我想的naive的二维DP是酱紫滴: mx[i][j][k]表示以(i,j)为右下角的k*k的正方形区域内的最大值,mn[i][j][k]同理 mx[i][j] ...

  4. depthstencil buffer 不支持 msaa

    phyreengine dx11 MRT不支持 depth rendertarget 的msaa 他里面竟然只写着,// not supported yet !!!! 导致hdao 时开msaa的话, ...

  5. js添加事件、移除事件、阻止冒泡、阻止浏览器默认行为等写法(兼容IE/FF/CHROME)

    转自:http://blog.csdn.net/itchiang/article/details/7769341 添加事件   var addEvent = function( obj, type, ...

  6. HDOJ 1062 Text Reverse

    Text Reverse Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  7. 数据库表 copy

    db1为原数据库,db2为要导出到的数据库,fromtable 是要导出的表名 1.方法一:登录导出到的数据库,执行create table fromtable select * from db1.f ...

  8. Message,MessageQueue,Looper,Handler详解

    Message,MessageQueue,Looper,Handler详解   一.几个关键概念 1.MessageQueue:是一种数据结构,见名知义,就是一个消息队列,存放消息的地方.每一个线程最 ...

  9. Javascript scrollTop 20大洋

    花了20大洋,买了一个视频,这是读书笔记 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"&g ...

  10. (转)【移动开发】Android中三种超实用的滑屏方式汇总(ViewPager、ViewFlipper、ViewFlow)

    转自: http://smallwoniu.blog.51cto.com/3911954/1308959 现如今主流的Android应用中,都少不了左右滑动滚屏这项功能,(貌似现在好多人使用智能机都习 ...