×ðÁú¿­Ê±¹ÙÍøµÇ¼

LinuxÄں˻ù´¡Æª¡ª¡ªcontainer_ofÔ­ÀíºÍÏÖʵӦÓÃ

LinuxÄÚºËÖо­³£¿É¼ûcontainer_ofµÄÉíÓ°£¬ËüÔÚÏÖʵÇý¶¯µÄ±àдÖÐÒ²ÊÇÆÕ±éÓ¦Óá£

container_ofÔ­Àí

×÷ÓãºÍ¨¹ý½á¹¹ÌåµÄij¸ö³ÉÔ±±äÁ¿µØµãÕÒµ½¸Ã½á¹¹ÌåµÄÊ׵صã¡£

½ç˵ÈçÏ£º

/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:    the pointer to the member.
 * @type:   the type of the container struct this is embedded in.
 * @member: the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) ({          \
    const typeof( ((type *)0)->member ) *__mptr = (ptr); \
    (type *)( (char *)__mptr - offsetof(type,member) );})

µÇ¼ºó¸´ÖÆ

ptr£º½á¹¹Ìå³ÉÔ±±äÁ¿µÄÖ¸Õë

type£º½á¹¹ÌåÀàÐÍ

member£º½á¹¹Ìå³ÉÔ±±äÁ¿µÄÃû×Ö

»»¾ä»°Ëµ£¬½Ð£ºÒÑÖª½á¹¹ÌåtypeµÄ³ÉÔ±memberµÄµØµãptr£¬Çó½â½á¹¹ÌåtypeµÄÆðʼµØµã¡£

ÅÌË㹫ʽΪ£ºtypeµÄÆðʼµØµã = ptr -size £¨sizeΪmemberµÄ´óС£©

ÒÔÒ»·ùͼ˵Ã÷ptr¡¢type¡¢memberµÄ¹Øϵ£º

Ô­Àí¼òÊö£º

container_ofµÄÃî´¦¾ÍÔÚÓÚÒÔ0×÷Ϊ³ÉÔ±±äÁ¿memberµÄ»ùÖ·¡£

ÆäÖнç˵ÁËÒ»ÆäÖÐÑë±äÁ¿__mptr£¬”__”´ú±íÄÚ²¿Ê¹Ó㬡°m¡±´ú±ímiddle¡£

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

µÇ¼ºó¸´ÖÆ

typeof( ((type *)0)->member )ÊÇ»ñÈ¡memberµÄÀàÐÍ£¬__mptr = (ptr)ÅжÏptrÓëmemberÊÇ·ñΪͳһÀàÐÍ£¬offsetofÅÌËã³ÉÔ±memberµÄ¾Þϸsize¡£

Çý¶¯ÖеÄÏÖʵÀý×Ó

ÀýÈçÄں˵ÄpwmÇý¶¯£¬Í¨¹ý³ÉÔ±±äÁ¿chip£¬ÕÒµ½½á¹¹Ìåbcm2835_pwm£º

struct bcm2835_pwm {
 struct pwm_chip chip;
 struct device *dev;
 void __iomem *base;
 struct clk *clk;
};

static inline struct bcm2835_pwm *to_bcm2835_pwm(struct pwm_chip *chip_ptr)
{
 return container_of(chip_ptr, struct bcm2835_pwm, chip);
}

µÇ¼ºó¸´ÖÆ

ʹÓÃcontainer_ofͨ³£¶¼»á½ç˵һ¸öº¯Êý£¬²¢ÇÒÃüÃûΪto_xxx»òÕßto_find_xxx£¬´ú±íÒªÕÒxxxÕâ¸ö½á¹¹Ì壬´«²ÎÔò´«Èë³ÉÔ±±äÁ¿Ö¸Õ룬ÁíÍ⺯ÊýÒ²»áÉùÃ÷Ϊinline¡£

ÒÔÉϾÍÊÇLinuxÄں˻ù´¡Æª¡ª¡ªcontainer_ofÔ­ÀíºÍÏÖʵӦÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ