opr: Add opr_containerof
authorSimon Wilkinson <sxw@your-file-system.com>
Sat, 22 Oct 2011 08:45:10 +0000 (09:45 +0100)
committerDerrick Brashear <shadow@dementix.org>
Sat, 12 Nov 2011 14:46:52 +0000 (06:46 -0800)
Add the opr_containerof macro, which can be used to find the base
address of a structure which contains a member whose location is known.

This formulation is heavily used throughout OpenAFS to determine the
base address of structures containing queue pointers - this provides
a central definition, rather than coding it from scratch each time.

Change-Id: I2a32a214252ad304238f740ad92579e5bb7287be
Reviewed-on: http://gerrit.openafs.org/5836
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Derrick Brashear <shadow@dementix.org>

src/opr/opr.h

index b53211c..d7db2cc 100644 (file)
@@ -1,7 +1,13 @@
 #ifndef OPENAFS_OPR_OPR_H
 #define OPENAFS_OPR_OPR_H 1
 
+/* macros */
+
+#define opr_containerof(ptr, structure, member) \
+   ((structure *)((char *)(ptr)-(char *)(&((structure *)NULL)->member)))
+
 /* assert.c */
+
 #ifdef AFS_NT40_ENV
 # define opr_abort() opr_NTAbort()
 extern void opr_NTAbort(void);