contributed by < JulianATA
>
First of all, we need know the difference between macro and function.
Macro trade space for time, on the other hand, functions trade time for space.
The cost of function call is to branch to the function part every time, making it slower than Macro.
So, using Macros in Linux will save more time than functions.
Plot(togo):
Why not using inline functions?
Julian Fang
typeof()
will be replace by return the type of parameter.
instance(before compilation):
For instance(after compilation):
A useful trick I knew is using in macros, when you are not sure what your inputs actually are.
Refer to GNU [Referring to a Type with typeof
:https://gcc.gnu.org/onlinedocs/gcc/Typeof.html
typeof
is often useful in conjunction with statement expressions (see Statement Exprs). Here is how the two together can be used to define a safe “maximum” macro which operates on any arithmetic type and evaluates each of its arguments exactly once:
This is a macro I knew before, its utility is to find the memery starting address with only one member in the structure.
But before we take a deep look at it, we should know macro offset()
.
The macro offset()
can calculate the offset(偏移) of the member relative to structure starting memory address.
Then we bck to container_of()
, which contains two steps.
list.h
還定義一系列操作,為什麼呢?這些有什麼益處?list_empty
head->next == head
makes it easy to understand.list_del_init
list_del
, the memory space of the node won't be reset or free. list_del_init
will initial the deleted node.list.h
are for formal checking and manipulation of the list, making it easier and more safe when manipulate list.LIST_POISONING
這樣的設計有何意義?LIST_POISONING
is defined in [linux/poison.h](https://github.com/torvalds/linux/blob/master/include/linux/poison.h)
, commented as fallowed
in linux/list.h
When deleting node, it'll set node->prev
set as LIST_POISON2
, node->next
as LIST_POISON1
, rather than set to NULL.
When enter these two memory will cause page fault, making them cannot be traversal.
List poisoning I knew is not in linux but in mail system as introduced in [List poinsoning]: https://en.wikipedia.org/wiki/List_poisoning"
Do not need to consider head and tail as special condition, an equal treatment circular linked list can reduce complicated determine statement.
和
list_for_each` 的差異在哪?"safe" 在執行時期的影響為何?The difference between list_for_each
and list_for_each_safe
is n
.
As I know, n
is like a buffer of pos
.
Why we need a buffer of pos
?
If we delete or free pos
in our manipulation, we won't lose the memory address we need to traversal next.
@
符號,這有何意義?你能否應用在後續的程式開發呢?@
is used in doxygen assists developer modify program.
For example:
@return
is the return value.@
is represnet as function parameter.We often use symbols not frequently use developement .
tests/
目錄底下的 unit test 的作用為何?就軟體工程來說的精神為何?To check if all the subtle parts work well or not.
In software architecture, uni test
can ensure every modifictaion of code won't cause bug or break origin functions.
Another benefit is shortening the testing, developing, and debugging time.
Unit test is checking a single unit, which can be a function or a class, indicating that a huge software is integrate by small units.
tests/
目錄的 unit test 可如何持續精進和改善呢?Cprogramming
LinuxKernel