FreeBSD kernel 笔记(5)——分配内存

FreeBSD kernel编程分配内存可以参考这两篇文档:MALLOC(9)CONTIGMALLOC(9)。需要注意以下几点:

(1)在中断上下文中使用malloc系列分配内存函数时,要使用M_NOWAIT标记;

(2)contigmalloc有一个boundary参数:

If the given value “boundary” is non-zero, then the set of physical pages cannot cross any physical address boundary that is a multiple of that value.

举个例子,如果boundary设置为1M,则实际分配的物理内存页面可以位于0~1M1M~2M,而不能位于1.9M~2.1M

C语言中的XOR运算符

Stackoverflow上一个回答很好地解释了XOR运算符的作用:

If you know how XOR works, and you know that ^ is XOR in C, then this should be pretty simple. You should know that XOR will flip bits where 1 is set, bits 2 and 5 of 0b00100100 are set, therefore it will flip those bits.

From an “during the test” standpoint, let’s say you need to prove this to yourself, you really don’t need to know the initial value of star to answer the question, If you know how ^ works then just throw anything in there:

 00100100
^10101010  (star's made up value)
---------
 10001110  (star's new value)

 bit position: | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0  
   |---|---|---|---|---|---|---|---
 star's new v: | 1 | 0 | 0 | 0 | 1 | 1 | 1 | 0
   |---|---|---|---|---|---|---|---
 star's old v: | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0

总结一下,就是第二个操作数用来控制第一个操作数:第二个操作数中bit1会导致第一个操作数中相应的bit发生反转,而bit0则会让第一个操作数中相应的bit不变。