源碼摘抄
#ifdef UNIV_DEBUG/** Debug assertion. Does nothing unless UNIV_DEBUG is defined. */#define ut_ad(EXPR) ut_a(EXPR)/** Debug statement. Does nothing unless UNIV_DEBUG is defined. */#define ut_d(EXPR) do {EXPR;} while (0)#else/** Debug assertion. Does nothing unless UNIV_DEBUG is defined. */#define ut_ad(EXPR)/** Debug statement. Does nothing unless UNIV_DEBUG is defined. */#define ut_d(EXPR)#endif
上述代碼涉及到define 宏定義用法 如下:
define function(x) do{} while()
我們常見的一種是 define N 100 定義常量 。
看上面用法:
#define ut_d(EXPR) do {EXPR;} while (0)
while (0) 為false,只執(zhí)行一次 do 。如果while里值大于0,那么條件為真,將一直執(zhí)行 do ,除非在里面添加break 跳出。
參考這篇文章do{...}while(0)的意義和用法看到這篇文章真是茅塞頓開。
再回到源碼。 ifdef univ_DEBUG 如果執(zhí)行了debug 將執(zhí)行下面的 else 執(zhí)行下面的 endif 。 ifdef ..... else ...... endif 結(jié)構(gòu)
下面摘抄一段 MySQL 源碼中調(diào)用的 lock0lock.c :: lock_clust_rec_cons_read_sees()函數(shù)
/*********************************************************************//**Checks that a record is seen in a consistent read.@return TRUE if sees, or FALSE if an earlier version of the recordshould be retrieved */UNIV_INTERNiboollock_clust_rec_cons_read_sees(/*==========================*/ const rec_t* rec, /*!< in: user record which should be read or passed over by a read cursor */ dict_index_t* index, /*!< in: clustered index */ const ulint* offsets,/*!< in: rec_get_offsets(rec, index) */ read_view_t* view) /*!< in: consistent read view */{ trx_id_t trx_id; ut_ad(dict_index_is_clust(index)); // 只在debug模式下才會執(zhí)行一次 ut_ad(page_rec_is_user_rec(rec)); // 只在debug模式下才會執(zhí)行一次 ut_ad(rec_offs_validate(rec, index, offsets)); //只在debug模式下才會執(zhí)行一次 /* NOTE that we call this function while holding the search system latch. To obey the latching order we must NOT reserve the kernel mutex here! */ trx_id = row_get_rec_trx_id(rec, index, offsets); return(read_view_sees_trx_id(view, trx_id));}
mysql好多ut_ad()這種斷言調(diào)用,這次終于搞明白了。
新聞熱點(diǎn)
疑難解答
圖片精選