引言
iso646.h 是一个定义了运算符别名的头文件,用来处理 ISO646 标准字符集不支持的运算操作;limits.h 被封装成了 climits,里面定义了一系列数据类型的最大值最小值。
iso646.h
该头文件在 C++中的封装 ciso646 其实没有 #include "iso646.h",因为 C++中是以关键字的形式定义了这些别名的。
代码位置:http://www.aospxref.com/android-12.0.0_r3/xref/external/libcxx/include/ciso646
11 #ifndef _LIBCPP_CISO64612 #define _LIBCPP_CISO64613 14 /*15 ciso646 synopsis16 17 */18 19 #include <__config>20 21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)22 #pragma GCC system_header23 #endif24 25 #endif // _LIBCPP_CISO646
复制代码
我们来看头文件中的定义:
代码位置:http://www.aospxref.com/android-12.0.0_r3/xref/external/clang/lib/Headers/iso646.h
可以看到,实际上是,只有在未定义__cplusplus 时,才定义如下的宏,即将我们常用的运算符定义为字符拼写意义,如:&& 增加别名为 and,依次类似。
26 #ifndef __ISO646_H27 #define __ISO646_H28 29 #ifndef __cplusplus30 #define and &&31 #define and_eq &=32 #define bitand &33 #define bitor |34 #define compl ~35 #define not !36 #define not_eq !=37 #define or ||38 #define or_eq |=39 #define xor ^40 #define xor_eq ^=41 #endif42 43 #endif /* __ISO646_H */
复制代码
limits.h
定义了一系列最值宏常量,最开始 include 了 float.h 和 linux/limits.h,将其中相关的定义也包含进来了。
代码位置:http://www.aospxref.com/android-12.0.0_r3/xref/bionic/libc/include/limits.h
40 /* Historically bionic exposed the content of <float.h> from <limits.h> and <sys/limits.h> too. */41 #include <float.h>42 43 #include <linux/limits.h>
复制代码
float.h
定义了一系列有关浮点数的宏,这里就不展开了。
linux/limits.h
定义了一系列与 linux 相关的宏,
如下:http://www.aospxref.com/android-12.0.0_r3/xref/bionic/libc/kernel/uapi/linux/limits.h比如管道的 buffer 大小,路径的长度,等。
19 #ifndef _UAPI_LINUX_LIMITS_H20 #define _UAPI_LINUX_LIMITS_H21 #define NR_OPEN 102422 #define NGROUPS_MAX 6553623 #define ARG_MAX 13107224 #define LINK_MAX 12725 #define MAX_CANON 25526 #define MAX_INPUT 25527 #define NAME_MAX 25528 #define PATH_MAX 409629 #define PIPE_BUF 409630 #define XATTR_NAME_MAX 25531 #define XATTR_SIZE_MAX 6553632 #define XATTR_LIST_MAX 6553633 #define RTSIG_MAX 3234 #endif
复制代码
操作系统使用的量
如密码的长度等。
45 #define PASS_MAX 128 /* _PASSWORD_LEN from <pwd.h> */46 47 #define NL_ARGMAX 948 #define NL_LANGMAX 1449 #define NL_MSGMAX 3276750 #define NL_NMAX 151 #define NL_SETMAX 25552 #define NL_TEXTMAX 25553 54 #define TMP_MAX 308915776
复制代码
数据类型的大小
依次定义了 CHAR_BIT、LONG_BIT、WORD_BIT、char/unsigned char/signed char、short/unsigned short、int/unsigned int、long/unsigned long、long long/unsigned long long 的大小,同时区分 32 位机器和 64 位机器。
58 #define CHAR_BIT 859 #ifdef __LP64__60 # define LONG_BIT 6461 #else62 # define LONG_BIT 3263 #endif64 #define WORD_BIT 3265 66 #define SCHAR_MAX 0x7f /* max value for a signed char */67 #define SCHAR_MIN (-0x7f-1) /* min value for a signed char */68 69 #define UCHAR_MAX 0xffU /* max value for an unsigned char */70 #ifdef __CHAR_UNSIGNED__71 # define CHAR_MIN 0 /* min value for a char */72 # define CHAR_MAX 0xff /* max value for a char */73 #else74 # define CHAR_MAX 0x7f75 # define CHAR_MIN (-0x7f-1)76 #endif77 78 #define USHRT_MAX 0xffffU /* max value for an unsigned short */79 #define SHRT_MAX 0x7fff /* max value for a short */80 #define SHRT_MIN (-0x7fff-1) /* min value for a short */81 82 #define UINT_MAX 0xffffffffU /* max value for an unsigned int */83 #define INT_MAX 0x7fffffff /* max value for an int */84 #define INT_MIN (-0x7fffffff-1) /* min value for an int */85 86 #ifdef __LP64__87 # define ULONG_MAX 0xffffffffffffffffUL /* max value for unsigned long */88 # define LONG_MAX 0x7fffffffffffffffL /* max value for a signed long */89 # define LONG_MIN (-0x7fffffffffffffffL-1) /* min value for a signed long */90 #else91 # define ULONG_MAX 0xffffffffUL /* max value for an unsigned long */92 # define LONG_MAX 0x7fffffffL /* max value for a long */93 # define LONG_MIN (-0x7fffffffL-1)/* min value for a long */94 #endif95 96 # define ULLONG_MAX 0xffffffffffffffffULL /* max value for unsigned long long */97 # define LLONG_MAX 0x7fffffffffffffffLL /* max value for a signed long long */98 # define LLONG_MIN (-0x7fffffffffffffffLL-1) /* min value for a signed long long */
复制代码
针对 Glibc 的定义增加
为了兼容更多的库,针对 Glibc 增加了一些相关的定义如与 uid_t/gid_t 相关的范围取值,size_t 的取值范围
116 #if defined(__USE_BSD) || defined(__BIONIC__) /* Historically bionic exposed these. */117 # define UID_MAX UINT_MAX /* max value for a uid_t */118 # define GID_MAX UINT_MAX /* max value for a gid_t */119 #if defined(__LP64__)120 #define SIZE_T_MAX ULONG_MAX121 #else122 #define SIZE_T_MAX UINT_MAX123 #endif124 #endif
复制代码
针对一些特定量的设置,如 MB_LEN_MAX,SEM_VALUE_MAX 等
132 #define MB_LEN_MAX 4133 134 #define NZERO 20135 136 #define IOV_MAX 1024137 #define SEM_VALUE_MAX 0x3fffffff
复制代码
针对 POSIX 的兼容
如:pthread_t 相关的值,
140 #include <bits/posix_limits.h>141 142 #define HOST_NAME_MAX _POSIX_HOST_NAME_MAX143 #define LOGIN_NAME_MAX 256144 #define TTY_NAME_MAX 32145 146 /* >= _POSIX_THREAD_DESTRUCTOR_ITERATIONS */147 #define PTHREAD_DESTRUCTOR_ITERATIONS 4148 /* >= _POSIX_THREAD_KEYS_MAX */149 #define PTHREAD_KEYS_MAX 128150 /* bionic has no specific limit */151 #undef PTHREAD_THREADS_MAX
复制代码
http://www.aospxref.com/android-12.0.0_r3/xref/bionic/libc/include/bits/posix_limits.h
posix_limits.h 定义了一系列与其相关的宏定义,这里就不展开说明了。
评论