Skip to content

Commit 973d5ee

Browse files
Eric Wonggitster
authored andcommitted
introduce container_of macro
This macro is popular within the Linux kernel for supporting intrusive data structures such as linked lists, red-black trees, and chained hash tables while allowing the compiler to do type checking. Later patches will use container_of() to remove the limitation of "hashmap_entry" being location-dependent. This will complete the transition to compile-time type checking for the hashmap API. This macro already exists in our source as "list_entry" in list.h and making "list_entry" an alias to "container_of" as the Linux kernel has done is a possibility. Signed-off-by: Eric Wong <[email protected]> Reviewed-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 26b455f commit 973d5ee

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

git-compat-util.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,4 +1312,14 @@ void unleak_memory(const void *ptr, size_t len);
13121312
*/
13131313
#include "banned.h"
13141314

1315+
/*
1316+
* container_of - Get the address of an object containing a field.
1317+
*
1318+
* @ptr: pointer to the field.
1319+
* @type: type of the object.
1320+
* @member: name of the field within the object.
1321+
*/
1322+
#define container_of(ptr, type, member) \
1323+
((type *) ((char *)(ptr) - offsetof(type, member)))
1324+
13151325
#endif

0 commit comments

Comments
 (0)