Linux Äں˵ÄÖ÷ÌåÊÇÒÔ GNU µÄ C ÓïÑÔ±àдµÄ£¬ GNU Ϊ´ËÌṩÁ˱àÒ빤¾ßgcc ¡£
Ò»¡¢ inline º¯Êý´óÁ¿µÄʹÓãº
Gcc ´Ó C++ ÓïÑÔÖÐÎüÊÕÁË¡° inline ¡±ºÍ¡° const ¡±¡£Æäʵ£¬ GNU µÄ C ºÍ C++ ÊǺÏΪһÌåµÄ£¬ gcc ¼ÈÊÇ C ±àÒëÓÖÊÇ C++ ±àÒ룬ËùÒÔ´Ó C++ ÖÐÎüÊÕһЩ¶«Î÷µ½ C ÖÐÊǺÜ×ÔÈ»µÄ¡£´Ó¹¦ÄÜÉÏ˵£¬ inline º¯ÊýµÄʹÓÃÓë #define ºê¶¨ÒåÏàËÆ£¬µ«¸üÓÐÏà¶ÔµÄ¶ÀÁ¢ÐÔ£¬Ò²¸ü°²È«¡£Ê¹Óà inline º¯ÊýÒ²ÓÐÀûÓÚ³ÌÐòµ÷ÊÔ¡£Èç¹û±àÒëʱ²»¼ÓÓÅ»¯£¬ÔòÕâЩ inline º¯Êý¾ÍÊÇÆÕͨµÄ£¬¶ÀÁ¢µÄº¯Êý£¬¸ü±ãÓÚµ÷ÊÔ¡£µ÷ÊÔºÃÒÔºó£¬ÔÙ²ÉÓÃÓÅ»¯ÖØбàÒëÒ»´Î£¬ÕâЩ inline º¯Êý¾ÍÏñºê²Ù×÷Ò»ÑùÈÚÈëÁËÒýÓô¦µÄ´úÂëÖУ¬ÓÐÀûÓÚÌá¸ßÔËÐÐЧÂÊ¡£ÓÉÓÚ inline º¯ÊýµÄ´óÁ¿Ê¹Óã¬Ï൱һ²¿·Ö´úÂë´Ó .c ÎļþÒÆÈëÁË .h ÎļþÖС£
¶þ¡¢Ææ¹ÖµÄºê²Ù×÷¶¨Ò壺
Linux Äں˴úÂëÖÐʹÓÃÁË´óÁ¿µÄ inline º¯Êý£¬µ«ÕⲢδÏû³ý¶Ôºê²Ù×÷µÄʹÓã¬ÄÚºËÖÐÈÔÓÐÐí¶àºê²Ù×÷¶¨Òå¡£²¢³£¶ÔÄں˴úÂëÖÐһЩºê²Ù×÷¶¨Ò巽ʽ¸Ðµ½ÃԻ󲻽⣬ÏÈ¿´Ò»¸öʵÀý£¬È¡×Ô fs/proc/kcore.c:
163#define DUMP_WRITE(add,nr) do {memcpy(bufp,addr,nr);buf +=nr;} while(0)
Õâ¸öÑ»·ÌåÖ»Ö´ÐÐÒ»´Î£¬ÎªÊ²Ã´ÒªÕâÑùͨ¹ýÒ»¸ö do-while Ñ»·À´¶¨ÒåÄØ£¿Ê×ÏÈÄܲ»Äܶ¨Òå³ÉÈçÏÂʽÑù£º
163#define DUMP_WRITE(add,nr) memcpy(bufp,addr,nr);buf +=nr;
²»ÐС£Èç¹ûÓÐÒ»¶Î³ÌÐòÔÚÒ»¸ö if Óï¾äÖÐÒýÓÃÕâ¸öºê²Ù×÷¾Í»á³öÎÊÌ⣺
if (add)
DUMP_WRITE(addr,nr);
Else
Do_something_else();
¾¹ýÔ¤´¦ÀíÒÔºó£¬Õâ¶Î´úÂë¾Í»á±ä³ÉÕâÑù£º
if (add)
memcpy(bufp,addr,nr);buf +=nr;
Else
Do_something_else();
±àÒëÕâ¶Î´úÂë gcc »áʧ°Ü£¬²¢±¨Óï·¨³ö´í¡£ÒòΪ gcc ÈÏΪ if Óï¾äÔÚ memcpy() ÒÔºó¾Í½áÊøÁË£¬È»ºóÈ´ÓÖÅöµ½ÁËÒ»¸ö else ¡£Èç¹û°Ñ DUMP_WRITE(addr,nr) ºÍ Do_something_else() »»Ò»ÏÂλÖ㬱àÒëµ¹ÊÇ¿ÉÒÔͨ¹ý£¬µ«ÎÊÌâÈ´¸üÑÏÖØÁË£¬ÒòΪ²»¹ÜÌõ¼þÂú×ãÓë·ñ bufp+=nr ¶¼»áµÃµ½Ö´ÐС£ÂíÉÏ»áÏëµ½ÒªÔÚ¶¨ÒåÖмÓÉÏ»¨À¨ºÅ£¬³ÉΪÕâÑù£º
163#define DUMP_WRITE(add,nr) {memcpy(bufp,addr,nr);buf +=nr;}
¿ÉÊÇ£¬ÉÏÃæÄǶγÌÐòÊÇͨ²»¹ý±àÒ룬ÒòΪ¾¹ýÔ¤´¦Àíºó¾Í±ä³ÉÕâÑù£º
if (add)
{memcpy(bufp,addr,nr);buf +=nr;};
Else
Do_something_else();
ͬÑù£¬ gcc ÔÚÅöµ½ else Ç°ÃæµÄ¡° ; ¡±Ê±¾ÍÈÏΪ if Óï¾äÒѾ½áÊøÁË£¬Òò¶øºóÃæµÄ else ²»ÔÚ if Óï¾äÖС£Ïà±È֮ϣ¬²ÉÓà do-while µÄ¶¨ÒåÔÚÈκÎÇé¿ö϶¼Ã»ÓÐÎÊÌâ¡£
Èý¡¢¶ÓÁеÄʹÓãº
ÄÚºËÖдóÁ¿µØʹÓÃ×ŶÓÁкͶÓÁвÙ×÷¡£
Èç¹ûÎÒÃÇÓÐÒ»ÖÖÊý¾Ý½á¹¹ foo £¬²¢ÇÒÐèҪά³ÖÒ»¸öÕâÖÖÊý¾Ý½á¹¹µÄË«Á´¶ÓÁУ¬×î¼òµ¥µÄ¡¢Ò²ÊÇ×î³£Óõİ취¾ÍÊÇÔÚÕâ¸öÊý¾Ý½á¹¹µÄÀàÐͶ¨ÒåÖмÓÈëÁ½¸öÖ¸Õ룬ÀýÈ磺
typedef struct foo
{
struct foo *prev;
struct foo *next;
¡¡
}foo_t;
È»ºóΪÕâÖÖÊý¾Ý½á¹¹Ð´Ò»Ì×ÓÃÓÚ¸÷ÖÖ¶ÓÁвÙ×÷µÄ×Ó³ÌÐò¡£ÓÉÓÚÓÃÀ´Î¬³Ö¶ÓÁеÄÕâÁ½¸öÖ¸ÕëµÄÀàÐÍÊǹ̶¨µÄ£¨¶¼ÊÇÖ¸Ïò foo Êý¾Ý½á¹¹£©£¬ÕâЩ×Ó³ÌÐò²»ÄÜÓÃÓÚÆäËüÊý¾Ý½á¹¹µÄ¶ÓÁвÙ×÷¡£»»ÑÔÖ®£¬ÐèҪά³Ö¶àÉÙÖÖÊý¾Ý½á¹¹µÄ¶ÓÁУ¬¾ÍµÃÓжàÉÙÌ׵ĶÓÁвÙ×÷×Ó³ÌÐò¡£¶ÔÓÚʹÓöÓÁнÏÉÙµÄÓ¦ÓóÌÐò»òÐí²»ÊǸö´óÎÊÌ⣬µ«¶ÔÓÚʹÓôóÁ¿¶ÓÁеÄÄں˾ͳÉÎÊÌâÁË¡£ËùÒÔ£¬ Linux ÄÚºËÖвÉÓÃÁËÒ»Ì×ÄÜÓõġ¢Ò»°ãµÄ¡¢¿ÉÒÔÓõ½¸÷ÖÖ²»Í¬Êý¾Ý½á¹¹µÄ¶ÓÁвÙ×÷¡£Îª´Ë£¬´úÂëµÄ×÷ÕßÃÇ°ÑÖ¸Õë prev ºÍ next ´Ó¾ßÌåµÄ¡°ËÞÖ÷¡±Êý¾Ý½á¹¹ÖгéÏó³öÀ´³ÉΪһÖÖÊý¾Ý½á¹¹ list_head, ÕâÖÖÊý¾Ý½á¹¹¼È¿ÉÒÔ¡°¼ÄËÞ¡±ÔÚ¾ßÌåµÄËÞÖ÷½á¹¹ÄÚ²¿£¬³ÉΪ¸ÃÊý¾Ý½á¹¹µÄÒ»¸ö¡°Á¬½Ó¼þ¡± ; Ò²¿ÉÒÔ¶ÀÁ¢´æÔÚ¶ø³ÉΪһ¸ö¶ÓÁеÄÍ·¡£Õâ¸öÊý¾Ý½á¹¹¶¨ÒåÔÚ include/linux/list.h ÖС£
16 struct list_head {
17 struct list_head *next, *prev;
18 };
Èç¹ûÐèҪijÖÖÊý¾Ý½á¹¹µÄ¶ÓÁУ¬¾ÍÔÚÕâÖֽṹÄÚ²¿·ÅÉÏÒ»¸ö list_head Êý¾Ý½á¹¹¡£ÒÔÓÃÓÚÄÚ´æÒ³Ãæ¹ÜÀíµÄ page Êý¾Ý½á¹¹ÎªÀý£¬Æ䶨ÒåΪ£º£¨¼û include/linux/mm.h £©
134 typedef struct page {
135 struct list_head list;
¡¡
138 struct page *next_hash;
¡¡
141 struct list_head lru;
¡¡
148 }mem_map_t;
¿É¼û£¬ÔÚ page Êý¾Ý½á¹¹ÖмÄËÞÁËÁ½¸ö list_head ½á¹¹£¬»òÕß˵ÓÐÁ½¸ö¶ÓÁвÙ×÷µÄÁ¬½Ó¼þ£¬ËùÒÔ page ½á¹¹¿ÉÒÔͬʱ´æÔÚÓÚÁ½¸öË«Á´¶ÓÁÐÖС£´ËÍ⣬½á¹¹Öл¹Óиöµ¥Á´Ö¸Õë next_hash, ÓÃÀ´Î¬³ÖÒ»¸öµ¥Á´µÄÔÓ´Õ¶ÓÁС£
¶ÔÓÚËÞÖ÷Êý¾Ý½á¹¹ÄÚ²¿ÁËÿ¸ö list_head Êý¾Ý½á¹¹¶¼Òª¼ÓÒÔ³õʼ»¯£¬¿ÉÒÔͨ¹ýÒ»¸öºê²Ù×÷ INIT_LIST_HEAD ½øÐУ¬Òª½«Ò»¸ö page ½á¹¹Í¨¹ýÆä¡°¶ÓÁÐÍ·¡±Á´È루ÓÐʱºòҲ˵¡°¹ÒÈ롱£©Ò»¸ö¶ÓÁÐʱ£¬¿ÉÒÔʹÓà list_add() ¡£´Ó¶ÓÁÐÖÐÍÑÁ´Óà list_del() ¡£
µ«ÕâÀï´æÔÚÒ»¸öÎÊÌ⣺¶ÓÁвÙ×÷¶¼ÊÇͨ¹ý list_head ½øÐеģ¬µ«ÄDz»¹ýÊǸöÁ¬½Ó¼þ£¬Èç¹ûÎÒÃÇÊÖÉÏÓиöËÞÖ÷½á¹¹£¬Äǵ±È»¾ÍÖªµÀÁËËüµÄij¸ö list_head ÔÚÄÇÀ´Ó¶øÒÔ´ËΪ²ÎÊýµ÷Óà list_add() »ò list_del(); ¿ÉÊÇ£¬·´¹ýÀ´£¬µ±ÎÒÃÇ˳×ÅÒ»¸ö¶ÓÁÐÈ¡µÃÆäÖÐÒ»Ïî list_head ½á¹¹Ê±£¬ÓÖÔõÑùÕÒµ½ÆäËÞÖ÷½á¹¹ÄØ£¿ÔÚ list_head ½á¹¹Öв¢Ã»ÓÐÖ¸ÏòËÞÖ÷½á¹¹µÄÖ¸Õëѽ¡£±Ï¾¹£¬ÎÒÃÇÕæÕý¹ØÐĵÄÊÇËÞÖ÷½á¹¹£¬¶ø²»ÊÇÁ¬½Ó¼þ¡£
ÏÂÃæͨ¹ýÒ»¸öʵÀýÀ´¿´Õâ¸öÎÊÌâÊÇÈçºÎ½â¾öµÄ¡£ÏÂÃæÊÇÈ¡×Ô mm/page_alloc.c ÖеÄÒ»ÐдúÂ룺
[rmqueue()]
188 page = memlist_entry(curr,struct page,list);
ÕâÀïµÄ memlist_entry() ½«Ò»¸ö list_head Ö¸Õë curr »»Ëã³ÉÆäËÞÖ÷½á¹¹µÄÆðʼµØÖ·£¬Ò²¾ÍÊÇÈ¡µÃÖ¸ÏòÆäËÞÖ÷½á¹¹µÄÖ¸Õë¡£ÄÇ memlist_entry() ÊÇÈçºÎʵÏÖµÄÄØ£¿ÒòΪÆäµ÷ÓòÎÊý page ÊǸöÀàÐÍ£¬¶ø²»ÊǾßÌåµÄÊý¾Ý¡£Èç¹û¿´Ò»Ïº¯Êý rmqueue() µÄÕû¸ö´úÂ룬¾Í¿ÉÒÔ·¢ÏÖÔÚÄÇÀï list ¾¹ÊÇÎÞ¶¨ÒåµÄ¡£
ÊÂʵÉÏ£¬ÔÚͬһÎļþÖн« memlist_entry ¶¨Òå³É list_entry £¬ËùÒÔʵ¼ÊÒýÓõÄÊÇ list_entry():
48 #define memlist_entry list_entry
¶ø list_entry µÄ¶¨ÒåÔòÔÚ include/linux/list.h ÖУº
135/**
136 * list_entry : get the struct for this entry
137 * @ptr: the &struct list_head pointer
138 * @type: the type of the struct this is embedded in
139 * @member: the name of the list_struct within the struct
140 */
141 #define list_entry(ptr, type, member) \
142 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
½«Ç°ÃæµÄ 188 ÐÐÓë´Ë¶ÔÕÕ£¬¾Í¿ÉÒÔ¿´³öÆäÖеİÂÃØ£º¾¹ý C Ô¤´¦ÀíµÄÎÄ×ÖÌæ»»£¬ÕâÒ»ÐеÄÄÚÈݾͳÉΪ£º
page=((struct page*) ((char )(curr)-(unsigned long)(&((struct page*)0)->list)));
ÕâÀïµÄ curr ÊÇÒ»¸ö page ½á¹¹ÄÚ²¿µÄ³É·Ö list µÄµØÖ·£¬¶øÎÒÃÇËùÐèÒªµÄÈ´ÊÇÄǸö page ½á¹¹±¾ÉíµÄµØÖ·£¬ËùÒÔÒª´ÓµØÖ· curr ¼õÈ¥Ò»¸öλÒÆÁ¿£¬¼´³É·Ö list ÔÚ page ÄÚ²¿µÄλÒÆÁ¿£¬²ÅÄÜ´ïµ½ÒªÇó¡£ÄÇô£¬Õâ¸öλÒÆÁ¿µ½µ×ÊǶàÉÙÄØ£¿£¦ ((struct page*)0)->list ¾Í±íʾµ±½á¹¹ page ÕýºÃÔÚµØÖ· 0 ÉÏʱÆä³É·Ö list µÄµØÖ·£¬Õâ¾ÍÊÇλÒÆ¡£Í¬ÑùµÀÀí£¬Èç¹ûÊÇÔÚ page ½á¹¹µÄ lru ¶ÓÁÐÀÔò´«ÏÂÀ´µÄ member Ϊ lru £¬Ò»ÑùÄÜËã³öËÞÖ÷½á¹¹µÄµØÖ·¡£
¿É¼û£¬ÕâÒ»ÌײÙ×÷¼ÈÆÕ±éÊÊÓã¬ÓÖ±£³ÖÁ˽ϸßЧÂÊ¡£µ«ÊÇ£¬¶ÔÓÚÔĶÁ´úÂëµÄÈËÈ´ÊÇÓиöȱµã£¬ÄǾÍÊǹâ´Ó´úÂëÖв»ÈÝÒ׿´³öÒ»¸ö list_head µÄËÞÖ÷½á¹¹ÊÇʲô£¬¶øÒÔÇ°Ö»Òª¿´Ò»Ï next µÄÀàÐ;ÍÖªµÀÁË¡£
cernllyy ÓÚ 2009-09-28 18:17:08·¢±í:
ллÀ²¡£¡£¡£¡£
weihuan ÓÚ 2008-03-16 12:16:35·¢±í:
ºÇºÇ лл
huangqimin4 ÓÚ 2008-03-01 14:51:55·¢±í:
ÊÇµÄ ³ýÁËÍ·Îļþ
weifan135157 ÓÚ 2008-02-28 17:34:46·¢±í:
linux Cº¯ÊýºÍC ÓïÑÔÖеĺ¯ÊýÊÇͨÓõÄÂð£¿
dzj ÓÚ 2008-02-24 00:06:36·¢±í:
ÁìÎò
Çز٠ÓÚ 2008-02-20 00:55:53·¢±í:
ÄÜ×ö³ÉÊÓƵ¸üºÃ
liuwench3385ao ÓÚ 2008-02-18 13:19:49·¢±í:
ºÃµÄ£¬Ð»Ð»£¡