Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Two classic macro definition under Linux     - Integrated security administrator Linux accident management (Linux)

- Linux server is how to do after the invasion (Linux)

- Docker knowledge base (Server)

- Ubuntu 14.04 configure JDK1.8.0_25, switchable version (Linux)

- Oracle Data Guard LOG_ARCHIVE_DEST_n optional attribute parameters (Database)

- How to install OpenOffice Ubuntu or Linux Mint (Linux)

- Sshuttle A VPN-based transparent proxy that uses ssh (Server)

- Linux Shell Understanding and Learning (Linux)

- ORA-04031 error appears in the solution 11.2.0.4 run utlrp.sql (Database)

- Linux System Getting Started tutorial: Ubuntu desktop using the command line to change the system proxy settings (Linux)

- Ubuntu apt-mirror established local private sources (Linux)

- Oracle data files deleted recover after physical (Database)

- Fatal NI connect error 12170 error in Alert Log (Database)

- The Linux C truncate function clears the file notes (Programming)

- CentOS 6.5 can not connect to the network under VMware (Linux)

- Let OpenCV face detection score output codes (Programming)

- Linux System Getting Started Learning: Linux how to install 7zip (Linux)

- FPM quickly create packages with RPM (Linux)

- Percona MySQL 5.6 semi-synchronous replication (Database)

- MySQL concat function is SQL injection (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;
}
//result:
/ *
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);
}
/*result
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.
     
         
       
         
  More:      
 
- Linux Getting Started Tutorial: How to set up a static MAC address on VMware ESXi virtual machine (Mobile)
- Oracle archive log size than the size of the online journal of the much smaller (Database)
- Ubuntu Slingscold (Linux)
- Oracle 12C truncate table cascade (Database)
- Linux Operating System Security Study (Linux)
- Shell scripts quickly deploy Tomcat project (Server)
- Use C program in JavaScript (Programming)
- Linux processes in memory and memory cgroup statistics (Linux)
- Ubuntu 14.04 configure JDK1.8.0_25, switchable version (Linux)
- Setting Derby as Linux / Windows running as a service from the start (Server)
- 10 tips daily Docker (Server)
- Usage sed some of the parameters (Linux)
- Convert MySQL date string to a NULL value exception handling (Database)
- Mac OS X Server installation and application (Linux)
- Docker installation under CentOS7 (Linux)
- Plasma 5.4 How to install on Kubuntu 15.04 (Linux)
- Generic mechanism C11 standard (Programming)
- CentOS6.6 ordinary users to use sudo command to borrow root user privileges (Linux)
- Linux rights management (Linux)
- Mistakenly deleted redo log file group being given the lead to start the database ORA-03113 (Database)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.