自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

[[390486]]

一、問題

為啥內(nèi)核有的變量沒有初始化就敢直接使用?

二、分析

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

其實大家仔細(xì)看一下,5765行是一個宏,

到內(nèi)核源碼去找該宏的定義: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又是一個宏, 繼續(xù)跟蹤該宏,有兩處定義

  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沒有定義【可以檢索一下內(nèi)核,找不到該宏的定義】,所以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 對應(yīng) 粉絲截圖的nid, __state 對應(yīng) N_ONLINE

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

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

可見,nid被定義了。

三、宏定義的注意點(diǎn)

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

1. 只占用編譯時間

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

宏展開因為在預(yù)處理階段發(fā)生,不會分配內(nèi)存。

2. 宏替換發(fā)生時機(jī)

編譯c源程序的過程:

  1. 預(yù)處理
  2. 編譯
  3. 匯編
  4. 連接

宏替換發(fā)生在編譯預(yù)處理階段。

3. 預(yù)處理包括哪些工作

預(yù)處理產(chǎn)生編譯器的輸出,實現(xiàn)功能如下

1)文件包含

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

2)條件編譯

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

3)宏展開

將對宏的調(diào)用展開成相對應(yīng)的宏定義

關(guān)于宏定義還有很多其他的知識點(diǎn),本文暫不深入展開。

四、如何快速展開復(fù)雜的宏定義?

Linux內(nèi)核中通常有很多宏定義,非常的復(fù)雜,對于初學(xué)者來說,經(jīng)常會一頭霧水,那如何快速理解宏定義呢?

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

第一步

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

文件如下: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的含義是,編譯預(yù)處理該文件,但是不去生成匯編代碼,只把文件中的宏定義以及包含的頭文件替代,并不會去檢查語法正確與否。

結(jié)果如下:

  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.  } 

由結(jié)果可知, nid是被賦值為0的。

五、練習(xí)

我們來做一個練習(xí),展開一下內(nèi)核的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 } 

編譯與處理結(jié)果如下:

  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); 

函數(shù)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()最終被替換后的代碼,你學(xué)會了嗎?

六、16個經(jīng)典宏定義小例子

數(shù)值相關(guān)的宏定義

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)) /*兩數(shù)取最大數(shù)*/ 
  2. #define MIN(x, y)   (((x) < (y)) ? (x) : (y)) /*兩數(shù)取最小數(shù)*/ 

3 BCD碼;

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

4 字符相關(guān)的宏定義

字符范圍判斷

  1. /*字符是否在某個區(qū)間范圍內(nèi)*/ 
  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.    /*十進(jìn)制內(nèi)字符*/ 
  7. #define isdigit(c)           in_range(c, '0''9'
  8. /*十六進(jìn)制內(nèi)字符*/ 
  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相關(guān)的宏定義

  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字節(jié)與高2字節(jié)內(nèi)容交換 */  
  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字節(jié)逆序*/ 
  13. #define LONGSWAP(x) ((LLSB(x) << 24) \     
  14.      |(LNLSB(x) << 16) \ 
  15.      |(LNMSB(x) << 8) \ 
  16.      |(LMSB(x))) 

6 bit相關(guān)的宏定義

  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. /* 字節(jié)串中某BIT值*/ 
  8. #define BIT_OF_BYTES(x, y) (BITS(x[(y)/8], (y)%8)) 
  9. /* 字節(jié)串中設(shè)置某BIT為0 */       
  10. #define SETBITSTO0_OF_BYTES(x, y) (x[(y)/8]) &= (~(1 << ((y)%8))) 
  11. /* 字節(jié)串中設(shè)置某BIT為1 */   
  12. #define SETBITSTO1_OF_BYTES(x, y) (x[(y)/8]) |= (1 << ((y)%8))  

7 數(shù)組與結(jié)構(gòu)體相關(guān)的宏定義

  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)二進(jìn)制后三位為000,&(~(align - 1)) = &(~7),就是去掉余數(shù),使其能被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. /*頁面對齊相關(guān)的宏,一頁為4096字節(jié)*/ 
  10. #define PAGE_SIZE         4096 
  11. #define PAGE_MASK         (~(PAGE_SIZE-1)) 
  12. #define PAGE_ALIGN(addr) -(((addr)+PAGE_SIZE-1) & PAGE_MASK) 

9 防止頭文件被重復(fù)包含

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

10 得到指定地址上的一個字節(jié)或者一個字

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

11 得到一個field在結(jié)構(gòu)體中的偏移量

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

12 得到一個結(jié)構(gòu)體中field所占用的字節(jié)數(shù)

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

13 得到一個變量的地址

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

14 將一個字母轉(zhuǎn)換成大寫

  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 返回數(shù)組元素的個數(shù)

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

 

責(zé)任編輯:姜華 來源: 一口Linux
相關(guān)推薦

2012-04-09 13:43:12

Java

2017-09-18 09:03:36

線程安全單例

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#靜態(tài)變量初始化

2009-08-26 18:28:44

C#數(shù)組

2010-01-22 13:16:05

VB.NET初始化數(shù)組

2023-05-08 15:55:16

MySQL數(shù)據(jù)庫

2019-11-04 13:50:36

Java數(shù)組編程語言

2009-09-08 09:48:34

LINQ初始化數(shù)組

2009-11-11 15:29:15

ADO初始化

2011-06-17 15:29:44

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

2021-03-12 10:30:11

SpringMVC流程初始化

2010-07-28 10:22:33

FlexApplica

2022-07-06 10:37:45

SpringServlet初始化

2020-12-03 09:50:52

容器IoC流程

2023-12-04 10:57:52

函數(shù)C++

2024-01-15 06:34:09

Gin鏡像容器

2009-08-31 10:30:29

C#變量初始化
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號