成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

為啥變量沒初始化就用了?那是宏定義啊!

系統 Linux
有粉絲提問為啥內核有的變量沒有初始化就敢直接使用?本篇就這一問題給大家做詳細的介紹。

[[390486]]

一、問題

為啥內核有的變量沒有初始化就敢直接使用?

二、分析

看上圖,其中的5747行的變量nid的確沒有定義,就直接使用了,這么做沒有問題嗎?

其實大家仔細看一下,5765行是一個宏,

到內核源碼去找該宏的定義:linux-3.14\include\linux\Nodemask.h

  1. #define for_each_online_node(node) for_each_node_state(node, N_ONLINE) 

其中的for_each_node_state又是一個宏, 繼續跟蹤該宏,有兩處定義

  1. 408 #if MAX_NUMNODES > 1 
  2. …… 
  3. 429 #define for_each_node_state(__node, __state) \ 
  4. 430  for_each_node_mask((__node), node_states[__state]) 
  5. …… 
  6. 450 #else  
  7. …… 
  8. 470 #define for_each_node_state(node, __state) \ 
  9. 471  for ( (node) = 0; (node) == 0; (node) = 1) 
  10. …… 
  11. 481 #endif 

究竟是哪一個定義,由條件#if MAX_NUMNODES > 1 來決定,

  1. #ifdef CONFIG_NODES_SHIFT 
  2. #define NODES_SHIFT     CONFIG_NODES_SHIFT 
  3. #else 
  4. #define NODES_SHIFT     0 
  5. #endif 
  6.  
  7. #define MAX_NUMNODES    (1 << NODES_SHIFT) 

因為CONFIG_NODES_SHIFT沒有定義【可以檢索一下內核,找不到該宏的定義】,所以NODES_SHIFT 為0

所以 MAX_NUMNODES 為1;

所以 for_each_node_state 定義如下:

  1. 470 #define for_each_node_state(node, __state) \ 
  2. 471  for ( (node) = 0; (node) == 0; (node) = 1) 

而此處的 node 對應 粉絲截圖的nid, __state 對應 N_ONLINE

所以5765行代碼,可以展開為

  1. for ( (nid) = 0; (nid) == 0; (nid) = 1) 

可見,nid被定義了。

三、宏定義的注意點

宏定義是一個給定名稱的代碼片段,當我們使用這個名稱的時候,預處理器會自動將其替換為宏定義的內容。宏定義有兩種,一種是object-like宏定義,在使用的時候相當于一個數據對象;另一種是function-like,在使用的時候就像調用函數那樣。

1. 只占用編譯時間

宏展開會使源程序變長,但是宏展開發生在編譯過程中,不占運行時間,只占編譯時間。

宏展開因為在預處理階段發生,不會分配內存。

2. 宏替換發生時機

編譯c源程序的過程:

  1. 預處理
  2. 編譯
  3. 匯編
  4. 連接

宏替換發生在編譯預處理階段。

3. 預處理包括哪些工作

預處理產生編譯器的輸出,實現功能如下

1)文件包含

把#include中包含的內容拓展為文件的正文,即找到.h文件,同時展開到#include所在處

2)條件編譯

根據#if和#ifdef等編譯命令,將源程序文件中的部分包含進來,部分排除,排除在外的一般轉換為空行

3)宏展開

將對宏的調用展開成相對應的宏定義

關于宏定義還有很多其他的知識點,本文暫不深入展開。

四、如何快速展開復雜的宏定義?

Linux內核中通常有很多宏定義,非常的復雜,對于初學者來說,經常會一頭霧水,那如何快速理解宏定義呢?

一口君教你一個非常方便的方法,讓你直接看透宏定義, 我們以上述代碼為例:

第一步

  1. 將要展開的宏先拷貝到c文件中,然后把所有用到的宏定義都拷貝到該文件中;
  2. 內核中很多的宏都是嵌套的,把嵌套的宏定義都一起拷貝到文件中;
  3. 此外內核很多的宏會由條件編譯決定,從而導致有多種定義方式, 如果不確定,就把條件編譯一起拷貝過來,
  • 如該例所示,MAX_NUMNODES 就被嵌套了多級, 最終宏CONFIG_NODES_SHIFT在內核中沒有檢索到,所以該宏沒有定義。

文件如下:123.c

  1. 1  
  2.  2  
  3.  3 #ifdef CONFIG_NODES_SHIFT 
  4.  4 #define NODES_SHIFT     CONFIG_NODES_SHIFT 
  5.  5 #else 
  6.  6 #define NODES_SHIFT     0 
  7.  7 #endif 
  8.  8  
  9.  9  
  10. 10                                                                                                                   
  11. 11 #define MAX_NUMNODES    (1 << NODES_SHIFT) 
  12. 12  
  13. 13  
  14. 14  
  15. 15  
  16. 16 #if MAX_NUMNODES > 1 
  17. 17 #define for_each_node_state(__node, __state) \ 
  18. 18         for_each_node_mask((__node), node_states[__state]) 
  19. 19 #else 
  20. 20 #define for_each_node_state(node, __state) \ 
  21. 21         for ( (node) = 0; (node) == 0; (node) = 1) 
  22. 22 #endif 
  23. 23  
  24. 24  
  25. 25  
  26. 26  
  27. 27 #define for_each_online_node(node) for_each_node_state(node, N_ONLINE) 
  28. 28  
  29. 29  
  30. 30 static int __build_all_zonelists(void *data) 
  31. 31 {    
  32. 32     int nid; 
  33. 33     int cpu; 
  34. 34     pg_data_t *self = data; 
  35. 35  
  36. 36      
  37. 37  
  38. 38     for_each_online_node(nid) { 
  39. 39         pg_data_t *pgdat = NODE_DATA(nid); 
  40. 40  
  41. 41         build_zonelists(pgdat); 
  42. 42         build_zonelist_cache(pgdat); 
  43. 43     } 
  44. 44 } 

第二步

使用以下命令,展開宏定義,

  1. gcc -E  

-E的含義是,編譯預處理該文件,但是不去生成匯編代碼,只把文件中的宏定義以及包含的頭文件替代,并不會去檢查語法正確與否。

結果如下:

  1. peng@ubuntu:~/test$ gcc 123.c -E 
  2. # 1 "123.c" 
  3. # 1 "<built-in>" 
  4. # 1 "<command-line>" 
  5. # 1 "/usr/include/stdc-predef.h" 1 3 4 
  6. # 1 "<command-line>" 2 
  7. # 1 "123.c" 
  8. # 28 "123.c" 
  9. static int __build_all_zonelists(void *data) 
  10.  int nid; 
  11.  int cpu; 
  12.  pg_data_t *self = data; 
  13.  
  14.  for ( (nid) = 0; (nid) == 0; (nid) = 1) { 
  15.   pg_data_t *pgdat = NODE_DATA(nid); 
  16.  
  17.   build_zonelists(pgdat); 
  18.   build_zonelist_cache(pgdat); 
  19.  } 

由結果可知, nid是被賦值為0的。

五、練習

我們來做一個練習,展開一下內核的waite_event()這個宏

拷貝用到所有宏定義到c文件中。

wait.c

  1. 1  
  2.  2 #define ___wait_event(wq, condition, state, exclusive, ret, cmd)    \ 
  3.  3     ({                                  \ 
  4.  4      __label__ __out;                       \ 
  5.  5      wait_queue_t __wait;                       \ 
  6.  6      long __ret = ret;                      \ 
  7.  7      \ 
  8.  8      INIT_LIST_HEAD(&__wait.task_list);             \ 
  9.  9      if (exclusive)                         \ 
  10. 10      __wait.flags = WQ_FLAG_EXCLUSIVE;          \ 
  11. 11      else
  12. 12      {\ 
  13. 13      /* code */                         \ 
  14. 14      __wait.flags = 0;                  \ 
  15. 15      \ 
  16. 16      for (;;) {                         \ 
  17. 17      long __int = prepare_to_wait_event(&wq, &__wait, state);\ 
  18. 18      \ 
  19. 19      if (condition)                     \    
  20. 20      break;                     \ 
  21. 21      \ 
  22. 22      if (___wait_is_interruptible(state) && __int) {        \ 
  23. 23      __ret = __int;                 \ 
  24. 24          if (exclusive) {               \ 
  25. 25              abort_exclusive_wait(&wq, &__wait, \ 
  26. 26                      state, NULL);  \ 
  27. 27                  goto __out;                \ 
  28. 28          }                      \ 
  29. 29      break;                     \ 
  30. 30      }                          \ 
  31. 31      \ 
  32. 32          cmd;                           \ 
  33. 33      }                              \ 
  34. 34      finish_wait(&wq, &__wait);                 \ 
  35. 35          __out: __ret;                              \ 
  36. 36      })\ 
  37. 37     }\ 
  38. 38  
  39. 39  
  40. 40  
  41. 41  
  42. 42 #define TASK_UNINTERRUPTIBLE    2 
  43. 43  
  44. 44  
  45. 45 #define __wait_event(wq, condition)                 \ 
  46. 46     (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0,  \ 
  47. 47             schedule())\ 
  48. 48 \ 
  49. 49 \ 
  50. 50 wait_event(wq, condition)                   \ 
  51. 51         do {                                    \ 
  52. 52             if (condition)                          \ 
  53. 53                 break;                          \ 
  54. 54                     __wait_event(wq, condition);                    \ 
  55. 55         } while (0)\ 
  56. 56  
  57. 57  
  58. 58  
  59. 59 test() 
  60. 60 { 
  61. 62     wait_event(wq,flag == 0); 
  62. 64 } 

編譯與處理結果如下:

  1.  root@ubuntu:/home/peng/test# gcc wait.c -E 
  2. # 1 "wait.c" 
  3. # 1 "<built-in>" 
  4. # 1 "<command-line>" 
  5. # 1 "/usr/include/stdc-predef.h" 1 3 4 
  6. # 1 "<command-line>" 2 
  7. # 1 "wait.c" 
  8. # 71 "wait.c" 
  9. test() 
  10.  do { if (flag == 0) break; (void)({ __label__ __out; wait_queue_t __wait; long __ret = 0; INIT_LIST_HEAD(&__wait.task_list); if (0) __wait.flags = WQ_FLAG_EXCLUSIVE; else { __wait.flags = 0; for (;;) { long __int = prepare_to_wait_event(&wq, &__wait, 2); if (flag == 0) break; if (___wait_is_interruptible(2) && __int) { __ret = __int; if (0) { abort_exclusive_wait(&wq, &__wait, 2, NULL); goto __out; } break; } schedule(); } finish_wait(&wq, &__wait); __out: __ret; }) }; } while (0); 

函數test()整理如下:

  1. test(){ 
  2.  do {  
  3.   if (flag == 0)  
  4.   break;  
  5.   (void)( 
  6.   { 
  7.    __label__ __out;  
  8.    wait_queue_t __wait;  
  9.    long __ret = 0;  
  10.    INIT_LIST_HEAD(&__wait.task_list);  
  11.    if (0) __wait.flags = WQ_FLAG_EXCLUSIVE;  
  12.    else { 
  13.     __wait.flags = 0;  
  14.  
  15.    for (;;)  
  16.    {  
  17.     long __int = prepare_to_wait_event(&wq, &__wait, 2);  
  18.     if (flag == 0)  
  19.      break;  
  20.     if (___wait_is_interruptible(2) && __int)  
  21.     {  
  22.      __ret = __int; 
  23.      if (0)  
  24.      {  
  25.       abort_exclusive_wait(&wq, &__wait, 2, NULL);  
  26.       goto __out;  
  27.      }  
  28.      break;  
  29.     }  
  30.     schedule();  
  31.    }  
  32.    finish_wait(&wq, &__wait); 
  33. __out:  
  34.  __ret;  
  35.    })  
  36.   };  
  37.  }while (0); 

這就是wait_event()最終被替換后的代碼,你學會了嗎?

六、16個經典宏定義小例子

數值相關的宏定義

1 閏年的判斷 ,年份可以整除4并且不能整除100,或者可以整除400,則為閏年;

  1. #define IS_LEAP_YEAR(y) (((((y) % 4) == 0) && (((y) % 100) != 0))  \ 
  2.          || (((y) % 400) == 0))/*判斷是否是閏年*/ 

2 **MAX 與 MIN ** ;

  1. #define MAX(x, y)   (((x) < (y)) ? (y) : (x)) /*兩數取最大數*/ 
  2. #define MIN(x, y)   (((x) < (y)) ? (x) : (y)) /*兩數取最小數*/ 

3 BCD碼;

  1. #define BCD2HEX(x) (((x) >> 4) * 10 + ((x) & 0x0F))       /*BCD碼轉數值, 20H -> 20*/ 
  2. #define HEX2BCD(x) (((x) % 10) + ((((x) / 10) % 10) << 4))  /*數值轉BCD碼, 20 -> 20H*/ 

4 字符相關的宏定義

字符范圍判斷

  1. /*字符是否在某個區間范圍內*/ 
  2. #define in_range(c, lo, up)  ((uint8)c >= lo && (uint8)c <= up)   
  3.  
  4.  
  5. #define isprint(c)           in_range(c, 0x20, 0x7f) 
  6.    /*十進制內字符*/ 
  7. #define isdigit(c)           in_range(c, '0''9'
  8. /*十六進制內字符*/ 
  9. #define isxdigit(c)          (isdigit(c) || in_range(c, 'a''f') || in_range(c, 'A''F')) 
  10. /*是否是小寫*/ 
  11. #define islower(c)           in_range(c, 'a''z')  
  12. /*是否是空格*/ 
  13. #define isspace(c)           (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v'
  14. /*是否為ASCII碼*/ 
  15. #define isascii(c)          ((unsigned) (c) <= 0177)  

5 byte相關的宏定義

  1. #define MSB(x) (((x) >> 8) & 0xff) /* x占2byte(如short)2byte的高地址的1byte */ 
  2. #define LSB(x) ((x) & 0xff) /* x占2byte(如short)2byte的低地址的1byte*/ 
  3.  
  4. #define MSW(x) (((x) >> 16) & 0xffff) /* x占4byte(如int)  4byte的高地址的2byte */ 
  5. #define LSW(x) ((x) & 0xffff)         
  6. #define WORDSWAP(x) (MSW(x) | (LSW(x) << 16)) /* x占4byte(如int) 低2字節與高2字節內容交換 */  
  7.  
  8. #define LLSB(x) ((x) & 0xff) /*x占4byte(如int) 取低地址1byte*/      
  9. #define LNLSB(x) (((x) >> 8) & 0xff) 
  10. #define LNMSB(x) (((x) >> 16) & 0xff) 
  11. #define LMSB(x)  (((x) >> 24) & 0xff) 
  12. /*x占4byte(如int) 4字節逆序*/ 
  13. #define LONGSWAP(x) ((LLSB(x) << 24) \     
  14.      |(LNLSB(x) << 16) \ 
  15.      |(LNMSB(x) << 8) \ 
  16.      |(LMSB(x))) 

6 bit相關的宏定義

  1. /* 判斷某位是否為1 */ 
  2. #define BIT_IS_1(x,y) (((x)>>(y))&0x01u)    
  3.  
  4. #define SETBITS(x,y,n) (x) = (n) ? ((x)|(1 << (y))) : ((x) &(~(1 << (y)))); 
  5. /* 給某位置反 */ 
  6. #define BIT_INVERSE(x,y)    ((x)=(x)^(1<<(y)))         
  7. /* 字節串中某BIT值*/ 
  8. #define BIT_OF_BYTES(x, y) (BITS(x[(y)/8], (y)%8)) 
  9. /* 字節串中設置某BIT為0 */       
  10. #define SETBITSTO0_OF_BYTES(x, y) (x[(y)/8]) &= (~(1 << ((y)%8))) 
  11. /* 字節串中設置某BIT為1 */   
  12. #define SETBITSTO1_OF_BYTES(x, y) (x[(y)/8]) |= (1 << ((y)%8))  

7 數組與結構體相關的宏定義

  1. /* number of elements in an array */ 
  2. #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))         
  3. /* byte offset of member in structure*/ 
  4. #define MOFFSET(structure, member) ((int) &(((structure *) 0) -> member))    
  5. /* size of a member of a structure */ 
  6. #define MEMBER_SIZE(structure, member) (sizeof (((structure *) 0) -> member))  

8 對齊的宏定義

  1. /*向上對齊,~(align - 1)為對齊掩碼,例如align=8,~(align - 1) = ~7, 
  2. (~7)二進制后三位為000,&(~(align - 1)) = &(~7),就是去掉余數,使其能被8整除*/ 
  3. #define ALIGN_UP(x, align)  (((int) (x) + (align - 1)) & ~(align - 1))  
  4. /*向下對齊*/ 
  5. #define ALIGN_DOWN(x, align)    ((int)(x) & ~(align - 1)) 
  6. /*是否對齊*/ 
  7. #define ALIGNED(x, align)   (((int)(x) & (align - 1)) == 0) 
  8.  
  9. /*頁面對齊相關的宏,一頁為4096字節*/ 
  10. #define PAGE_SIZE         4096 
  11. #define PAGE_MASK         (~(PAGE_SIZE-1)) 
  12. #define PAGE_ALIGN(addr) -(((addr)+PAGE_SIZE-1) & PAGE_MASK) 

9 防止頭文件被重復包含

  1. #ifndef BODY_H   //保證只有未包含該頭文件才會將其內容展開 
  2.  
  3. #define BODY_H 
  4.  
  5. //頭文件內容 
  6.  
  7. #endif 

10 得到指定地址上的一個字節或者一個字

  1. #define MEM_B(x) (*(byte*)(x))    //得到x表示的地址上的一個字節 
  2. #define MEM_W(x) (*(word*)(x))    //得到x表示的地址上的一個字 

11 得到一個field在結構體中的偏移量

  1. #define OFFSET(type,field) (size_t)&((type*)0->field) 

12 得到一個結構體中field所占用的字節數

  1. #define FSIZ(type,field) sizeof(((type*)0)->field) 

13 得到一個變量的地址

  1. #defien B_PTR(var) ((byte*)(void*)&(var))   //得到字節寬度的地址 
  2. #define W_PTR(var) ((word*)(void*)&(var))   //得到字寬度的地址 

14 將一個字母轉換成大寫

  1. #define UPCASE(c) (((c) >= "a" && (c) <= "z") ? ((c) - 0x20) : (c) ) 

15 防止溢出

  1. #define INC_SAT(val) (val = ((val) +1 > (val)) ? (val) + 1 : (val)) 

16 返回數組元素的個數

  1. #define ARR_SIZE(a) (sizeof( (a) ) / sizeof(a[0]) ) ) 

 

責任編輯:姜華 來源: 一口Linux
相關推薦

2017-09-18 09:03:36

線程安全單例

2012-04-09 13:43:12

Java

2010-02-24 15:41:19

Linux Light

2023-11-12 23:08:17

C++初始化

2009-08-31 10:38:34

C#變量初始化

2009-08-31 09:47:22

C#靜態變量初始化

2009-08-26 18:28:44

C#數組

2023-05-08 15:55:16

MySQL數據庫

2010-01-22 13:16:05

VB.NET初始化數組

2019-11-04 13:50:36

Java數組編程語言

2009-09-08 09:48:34

LINQ初始化數組

2009-11-11 15:29:15

ADO初始化

2010-07-28 10:22:33

FlexApplica

2021-03-12 10:30:11

SpringMVC流程初始化

2022-07-06 10:37:45

SpringServlet初始化

2020-12-03 09:50:52

容器IoC流程

2011-06-17 15:29:44

C#對象初始化器集合初始化器

2024-01-15 06:34:09

Gin鏡像容器

2023-12-04 10:57:52

函數C++

2021-04-07 08:03:51

js舉起Hoisting初始化
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 91精品国产91久久久久福利 | 免费黄色av网站 | 欧美日韩中文字幕在线播放 | www国产成人 | 超碰超碰| 久久99视频免费观看 | 欧美成人免费电影 | 国产a区 | 久久久免费少妇高潮毛片 | 99爱在线视频 | 综合久久99 | 日日夜夜操天天干 | 精品视频久久久久久 | 在线国产视频 | 亚洲三级在线观看 | 国产毛片久久久久久久久春天 | 一色桃子av一区二区 | 巨大荫蒂视频欧美另类大 | 欧美日韩国产一区二区三区 | 国产一级毛片精品完整视频版 | 久久99精品久久久久久噜噜 | 精品国产免费人成在线观看 | 美女视频一区二区 | 午夜久久久久 | 欧美精品综合在线 | 日本视频一区二区 | 日韩国产精品一区二区三区 | 欧美日韩综合视频 | 国产成人精品福利 | 国产精品自拍视频网站 | 国产精品高潮呻吟久久aⅴ码 | 日日操视频 | 伊人无码高清 | 国产成人99久久亚洲综合精品 | 欧美精品一区二区三区在线播放 | 538在线精品| 综合另类 | 午夜在线小视频 | 亚洲国产精品一区二区www | 国产精品视频 | 欧洲在线视频 |