EDKII CR macro: Gets the parent struct-body variable pointer based on the member pointer

Source: Internet
Author: User

Core tips:

1. CR macro (containing Record): Gets the parent struct-body variable pointer based on the member pointer

2.0 pointers to the magical.

There are many uses of CR macros in Edkii code, such as the clock interrupt handler function Coretimertick.

VOID Efiapi Coretimertick (in  UINT64   Duration  ) {  IEVENT          *Event;   ...   if (! Islistempty (&mefitimerlist)) {    =CR (Mefitimerlist.forwardlink, IEVENT, Timer.link, event_signature);   ...  }}

Definition of CR macro
  1. Containing_record-returns A pointer to the structure
  2. From one of it ' s elements.
  3. #define _CR (record, type, Field) ((Type *)((CHAR8 *) (record)-(CHAR8 * ) & (((TYPE *) 0 )))
  4. #define CR (Record, TYPE, Field, Signature) \\par _CR (Record, TYPE, Field)

CR usage: _CR (pointer Record for a struct member variable, struct type definition type, struct member variable name Field);

The key point for this macro definition is the 0 pointer. struct call struct–> Member is the pointer to the struct and the Member offset, 0->member call can directly get the Member offset. This member the actual pointer minus the offset, which is the pointer to the struct itself. It's a clever use!

The following examples illustrate:

Use of CR macros
  1. #include "StdAfx.h"
  2. #define CHAR8 Char
  3. Containing_record-returns A pointer to the structure
  4. From one of it ' s elements.
  5. #define _CR field "   ((TYPE *) (( CHAR8 *) (Record)-( CHAR8 *) & (((TYPE *) 0)-> field
  6. #define CR (Record, TYPE, Field, Signature) \\par _CR (Record, TYPE, Field)
  7. typedef struct _mystruct
  8. {
  9. int a ;
  10. Char b ;
  11. Long C ;
  12. int D ;
  13. } mystruct;
  14. int _tmain (intargc, _tchar* argv[])
  15. {
  16. MyStruct MyStruct    = {Ten,' a ', 30,25};
  17. printf    ("MyStruct Address 0x%x \ n", &mystruct);
  18. MyStruct * pmystruct = CR(& (MyStruct.C), MyStruct, C, NULL);
  19. printf ("pmystruct Address 0x%x \ n", pmystruct);
  20. GetChar ();
  21. return 0;
  22. }

EDKII CR macro: Gets the parent struct-body variable pointer based on the member pointer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.