wifi6 Linux driver - Intel ax201 wifi driver - 5 - Linux kernel

struct_size

這個是 Linux kernel 4.18 之後才有的, 定義在 linux/overflow.h

--
程式:

cmd.len[0] = struct_size(pattern_cmd, patterns, wowlan->n_patterns);

pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);

--
這用法是在於 pattern_cmd 是一個 structure pointer.

struct iwl_wowlan_patterns_cmd_v1 *pattern_cmd;

structure 很單純, 就是有多少個 patterns: n_patterns 和 pattern pointer array
struct iwl_wowlan_patterns_cmd_v1 {
__le32 n_patterns;
struct iwl_wowlan_pattern_v1 patterns[];
} __packed; /* WOWLAN_PATTERN_ARRAY_API_S_VER_1 */

--
計算方法:


就是: (wowlan->n_patterns) * (*pattern_cmd->patterns) + sizeof(*pattern_cmd)

PS: C 運算子 -> 優先於 *


運算子優先權 (C 語言)
Percedence Table

運算子
Operator
說 明
Description
結合順序
Associativity
1()
[]
->
.
++ --
Function call
Array subscripting
Element selection (of struct or union) through pointer
Element selection (of struct or union) by reference
increment/decrement (suffix) (註1)
左至右
2!
~
++ --
+ -
*
&
(type)
sizeof
logic NOT
bitwise NOT
increment/decrement (prefix)
unary plus and minus
Indirection (dereference, right value)
Address-of (left value)
type cast
Size-of
右至左
3* / %Arithmetic multiplication, division, and remainder左至右
4+ -Arithmetic addition, subtraction左至右
5<< >>Bitwise left shift, right shift左至右
6< <=
> >=
Relational less than, not greater than
Relational greater than, not less than
左至右
7== !=Relational equal, not equal左至右
8&Bitwise AND左至右
9^Bitwise XOR左至右
10|Bitwise OR左至右
11&&Logical AND左至右
12||Logical OR左至右
13?:Ternary conditional右至左
14=
+= -=
*= /= %=
&= ^= |=
<<= >>=
Direct assignment
Assignment by sum, difference
Assignment by product, quotient, remainder
Assignment by bitwise AND, XOR, OR
Assignment by bitwise left shift, right shift
右至左
15,Comma左至右
註1: 這一條和 K&R C 書上所列不同, 這是 K&R C 原版的錯誤. 目前所有的 C/C++ 編譯器都還保留這個錯誤, 並且不會被修正. 請參考 Wiki: C (programming language) Syntax 的第 5 小段 (標題 "Character Set" 的前面小段). 或者 K&R 原版 第 3 頁. 摘錄如下:
"C, like any other language, has its blemishes. Some of the operators have the wrong precedence; some parts of the syntax could be better."



留言

熱門文章