In lesson 7 i think get_container_of instead of container_of may be better.container_of seems rather powerless. #3
Replies: 4 comments
-
I'm not sure what But regarding the I could just use /**
Macro that returns a pointer to the data structure that contains a specified field of
that data structure. This is a lightweight method to hide information by placing a
public data structure inside a larger private data structure and using a pointer to
the public data structure to retrieve a pointer to the private data structure.
This function computes the offset, in bytes, of field specified by Field from the beginning
of the data structure specified by TYPE. This offset is subtracted from Record, and is
used to return a pointer to a data structure of the type specified by TYPE. If the data type
specified by TYPE does not contain the field specified by Field, then the module will not compile.
@param Record Pointer to the field specified by Field within a data structure of type TYPE.
@param TYPE The name of the data structure type to return. This data structure must
contain the field specified by Field.
@param Field The name of the field in the data structure specified by TYPE to which Record points.
@return A pointer to the structure from one of it's elements.
**/
#define BASE_CR(Record, TYPE, Field) ((TYPE *) ((CHAR8 *) (Record) - OFFSET_OF (TYPE, Field))) Or just /**
Macro that calls DebugAssert() if the containing record does not have a
matching signature. If the signatures matches, then a pointer to the data
structure that contains a specified field of that data structure is returned.
This is a lightweight method hide information by placing a public data
structure inside a larger private data structure and using a pointer to the
public data structure to retrieve a pointer to the private data structure.
If MDEPKG_NDEBUG is defined or the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit
of PcdDebugProperyMask is clear, then this macro computes the offset, in bytes,
of the field specified by Field from the beginning of the data structure specified
by TYPE. This offset is subtracted from Record, and is used to return a pointer
to a data structure of the type specified by TYPE.
If MDEPKG_NDEBUG is not defined and the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit
of PcdDebugProperyMask is set, then this macro computes the offset, in bytes,
of field specified by Field from the beginning of the data structure specified
by TYPE. This offset is subtracted from Record, and is used to compute a pointer
to a data structure of the type specified by TYPE. The Signature field of the
data structure specified by TYPE is compared to TestSignature. If the signatures
match, then a pointer to the pointer to a data structure of the type specified by
TYPE is returned. If the signatures do not match, then DebugAssert() is called
with a description of "CR has a bad signature" and Record is returned.
If the data type specified by TYPE does not contain the field specified by Field,
then the module will not compile.
If TYPE does not contain a field called Signature, then the module will not
compile.
@param Record The pointer to the field specified by Field within a data
structure of type TYPE.
@param TYPE The name of the data structure type to return This
data structure must contain the field specified by Field.
@param Field The name of the field in the data structure specified
by TYPE to which Record points.
@param TestSignature The 32-bit signature value to match.
**/
#if !defined (MDEPKG_NDEBUG)
#define CR(Record, TYPE, Field, TestSignature) \
(DebugAssertEnabled () && (BASE_CR (Record, TYPE, Field)->Signature != TestSignature)) ? \
(TYPE *) (_ASSERT (CR has Bad Signature), Record) : \
BASE_CR (Record, TYPE, Field)
#else
#define CR(Record, TYPE, Field, TestSignature) \
BASE_CR (Record, TYPE, Field)
#endif |
Beta Was this translation helpful? Give feedback.
-
I have studied linux kernel for a long time.Every time i read this container_of I'm a bit unnatural.thank your for your sharing of uefi material. |
Beta Was this translation helpful? Give feedback.
-
All is that ,the purpose of container_of(X,Y,Z) is getting the container of the X through the information of Y Z.so i think get_container_of is better. |
Beta Was this translation helpful? Give feedback.
-
I thought about it again.In C the pointer is more frequently used than struct ,so may be the reason container_of but not get_container_of is used , because get is verb but not noun,profile_t *p = container_of(&prop.sex, struct profile, sex); container_of seems natural. |
Beta Was this translation helpful? Give feedback.
-
^ ^
Beta Was this translation helpful? Give feedback.
All reactions