C language code writing skills (1)

zhaozj2021-02-16  46

When IPv6 source code is analyzed in FreeBSD, several C language code writing skills:

1. According to the macro definition, support for different networks, such Switch statements combined with macro definition sets with the precompiled instructions, which facilitates reducing the compiled kernel size, making the program structure clearer, run faster; if in IF_ETHERSUB. C in the Switch module:

Switch (Ether_TYPE) {#ifdef inet case ethertype_ip: ... breaf; case ethertype_arp: ........ Break; # Endif # ifdef ipx case ethertype_ipx: ....... Break; # Endif # ifdef INET6 CASE ETHERTYPE_IPV6: ....... Break; # Endif # ifdef Netatalk Case Ethertype_at: ....... Break; Case Ethertype_aarp: ....... Break; #endif default:. ... Break;}

2. For modules that require frequent processing, add it to a file module. When the node is processed, each node needs to process the header selection in the extended first part, so edit this module to the IP6_INPUT module processed together with the IPv6 header, and other extensions are not all If you need to process, some are only processed in the source or destination host, such as the first part of the FRAG. This speeds up the processing of the datagram and improves the network speed.

3, the use of GOTO statement. In a function, if there is a lot of small modules (about 10 rows), you can set together, such as errors processing, etc., linked through GOTO. Such a module is typically placed on the tail of the function, making the program structure clearer;

4, the simplification of complex data structures. #Define can be redefined by the complex data structure members, so that the structure variables more intuitive, easy maintenance and reading; eg: struct ip6_hdr {union {struct ip6_hdrctl {u_int32_t ip6_un1_flow; u_int16_t ip6_un1_plen; u_int8_t ip6_un1_nxt; u_int8_t ip6_un1_hlim;} ip6_un1 ; U_int8_t ip6_un2_vfc;} ip6_ctlun; struct in6_addr ip6_src; struct in6_addr ip6_dst;};

#define ip6_vfc ip6_ctlun.ip6_un2_vfc # define ip6_flow ip6_ctlun.ip6_un1.ip6_un1_flow # define ip6_plen ip6_ctlun.ip6_un1.ip6_un1_plen # define ip6_nxt ip6_ctlun.ip6_un1.ip6_un1_nxt # define ip6_hlim ip6_ctlun.ip6_un1.ip6_un1_hlim # define ip6_hops ip6_ctlun.ip6_un1.ip6_un1_hlim

5. Class function macro (also called a macro) and function calls, this is a significant time and space conversion problem in C. The reaction time of the class function macro is fast, and the space is more space; the function is reversed, the time response is slow, and the space takes up low. At pretreatment, the class function macro is compiled into the source program so that there is no problem with the call time, and the function is relatively independent, and the call is called - dynamic calls when needed. Such functions macro:

#define Panic (param) {Printf (param); exit (-1);} If you are a small function module, you can consider the use of class function macro, convenient and fast.

转载请注明原文地址:https://www.9cbs.com/read-20418.html

New Post(0)