Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Two classic macro definition under Linux     - Oracle Database Performance Optimization of memory disk (Database)

- Fragment Android developers learning to resolve (Programming)

- Running the open-source Swift under Linux platform (Linux)

- ld.so.conf.d profile (Linux)

- Deploy OpenStack Juno on Ubuntu 14.04 (Linux)

- RHEL5.8 physical opportunities to Read-only file system (Linux)

- Oracle Linux 5.9 configuration Xmanager (Linux)

- Swift used in the application to add a local push at the specified time (Programming)

- Analysis of MySQL Dockerfile 5.6 (Database)

- HBase in MVCC implementation mechanism and its application (Database)

- Define and modify strings principle in Python (Programming)

- FFmpeg compiled with only H264 decoding library (Programming)

- MySQL common functions summary (Database)

- Comparison of one-time transaction and CTE insert data (Database)

- Java Foundation - The relationship between abstract classes and interfaces (Programming)

- Cool Android realization SVG animation (Programming)

- Let Mac OS X dedicated high-speed mobile hard disk can also be read in Linux (Linux)

- Sorting Algorithm (1) Quick Sort C ++ implementation (Programming)

- Linux install Samba file sharing server (Server)

- Linux development management utility command (Linux)

  Two classic macro definition under Linux
  Add Date : 2016-11-04      
  This paper introduces the classic Linux under the macro definition, feeling geeks wisdom, then according to the classical definition to pave the way for the next article.

offsetof macro definitions:

// Get structure (TYPE) of the variable elements (the MEMBER) In the structure of the offset.
#define offsetof (TYPE, MEMBER) ((size_t) & ((TYPE *) 0) -> MEMBER)

Description: Get structures (TYPE) member variables (MEMBER) In this structure the offset.
1. ((TYPE *) 0) the transition to zero TYPE pointer type, that type TYPE pointer address is 0.
2 ((TYPE *) 0) -.> Data member MEMBER access structure.
3 &. (((TYPE *) 0) -> MEMBER) fetch address data members. Since the TYPE address is 0, the address is here to get the relative offset TYPE MEMBER of.
4. (size_t) (& (((TYPE *) 0) -> MEMBER)) result of the conversion type. For 32-bit systems, size_t is unsigned int type; For 64-bit systems, size_t is unsigned long type.

Example of use:

struct student
 char gender;
 int id;
 int age;
 char name [20];

int _tmain (int argc, _TCHAR * argv [])
 int gender_offset, id_offset, age_offset, name_offset;

 gender_offset = offsetof (struct student, gender);
 id_offset = offsetof (struct student, id);
 age_offset = offsetof (struct student, age);
 name_offset = offsetof (struct student, name);

 printf ( "gender_offset =% d \ n", gender_offset);
 printf ( "id_offset =% d \ n", id_offset);
 printf ( "age_offset =% d \ n", age_offset);
 printf ( "name_offset =% d \ n", name_offset);

 system ( "pause");
 return 0;
/ *
gender_offset = 0
id_offset = 4 // its bytes
age_offset = 8
name_offset = 12
* /

TYPE is the structure, which represents the "whole"; and MEMBER is a member, which is a part of the whole.

container_of macro definitions:

#define container_of (ptr, type, member) ({\
    const typeof (((type *) 0) -> member) * __ mptr = (ptr); \
    (Type *) ((char *) __ mptr - offsetof (type, member));})

Description: According to the "structure (type) variable" in the "domain member variables (member) of the pointer (ptr)" to obtain a pointer to the entire structure variables.
1. typeof (((type *) 0) -> member) Remove the member of the member variable types.
2.const typeof (((type *) 0) -> member) * __ mptr = (ptr) define variables __mptr pointer and ptr assigned to __mptr. After this step, __ mptr data type for the member of a constant pointer, which points to the address pointed to by ptr.
3. (char *) __ mptr __mptr will be converted to a byte pointer.
4. offsetof (type, member)) is to get the "member members" in the "structure type" location offset.
5. (char *) __ mptr - offsetof (type, member)) is used to obtain the starting address "structure type" pointer (a char * pointer).
6. (type *) ((char *) __ mptr - offsetof (type, member)) is to "char * type of structure type pointer to" convert "type * type of structure type pointer."
7. slash "/" indicates that the line connection

Example of use:
Note: this example addressof macro definitions and macro definitions above, except that a rewrite.

// Get structure (TYPE) of the variable elements (the MEMBER) In the structure of the offset.
#define offsetof (TYPE, MEMBER) ((size_t) & ((TYPE *) 0) -> MEMBER)
/ * According to "structure (type) variable" in the "domain member variables (member) of the pointer (ptr)" to obtain a pointer to the entire structure variables * /
#define addressof (PT, TYPE, MEMBER) ((TYPE *) ((char *) (PT) - offsetof (TYPE, MEMBER)))

struct student
 char gender;
 int id;
 int age;
 char name [20];

void main ()
 struct student stu;
 struct student * pstu;

 stu.gender = '1';
 stu.id = 9527;
 stu.age = 24;
 strcpy (stu.name, "abc");

 // According to "id address" get "the address of the structure."
 pstu = addressof (& (stu.id), struct student, id);

 // According to the acquired address of the student body structure, access to other members
 printf ( "gender =% c \ n", pstu-> gender);
 printf ( "age =% d \ n", pstu-> age);
 printf ( "name =% s \ n", pstu-> name);
gender = 1
age = 24
name = abcgender = 1
age = 24
name = abc
* /

type structure is, it represents the "whole"; and the member is a member, which is a part of the whole, and the address of the member is known.

Finally, you can think about what are the macro uses.
- After VMware CentOS full VM clone the network card is unavailable Solutions (Linux)
- Zabbix monitoring disk IO status (Server)
- Traffic monitor Linux Python Version (Programming)
- VirtualBox snapshots (Linux)
- Linux disk partition, format, mount the directory (Linux)
- Cobbler remotely install CentOS system (Linux)
- Jump table (skiplist) of code (Programming)
- Linux Basic Course: tar command description (Linux)
- Configuring a Linux operating system security management services Explain (Linux)
- Some safety precautions of Linux servers (Linux)
- Python programming style (Programming)
- Upgrade Goldengate to (Database)
- Java singleton mode (Singleton mode) (Programming)
- CentOS RedHat YUM Source Extensions Supplement (including 32-bit, 64-bit) (Linux)
- Live Wallpaper for Linux release adds dynamic background (Linux)
- Linux performance optimization tools perf top (Linux)
- CentOS7 yum install third-party source EPEL (Linux)
- To setup a ftp server under Linux (Server)
- Redhat 7 modify the default run level method --RHEL7 use systemd to create a symbolic link to the default runlevel (Linux)
- Source encountered problems and solutions when installing SaltStack (Server)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.