首页 > 其他 > 详细

2017-1-15-libubox analysis

时间:2017-01-16 00:04:27      阅读:385      评论:0      收藏:0      [点我收藏+]

2017-1-15-libubox analysis

utils.h

提供了一些简单的实用工具函数。比如大小端转换,位图操作,编译器属性的封装,连续内存申请函数calloc_a,静态计算数组大小的宏,断言/调试的实用工具,苹果兼容的时钟获取时间的封装,base64编解码。

  1. /* 
  2. *  
  3. * calloc_a(size_t len, [void **addr, size_t len,...], NULL) 
  4. * 申请一个足够大内存块来保存多个对齐的对象。 
  5. * 返回一个指针,指针指向全部对象(以第一个块开始)注意:释放这个指针将释放所有全部对象的内存 
  6. * 所有其它指针被保存在额外的addr参数指向的位置。 
  7. * 最后一个参数必须是NULL指针 
  8. */ 
  9.  
  10. #define calloc_a(len, ...) __calloc_a(len, ##__VA_ARGS__, NULL) 
  11. void *__calloc_a(size_t len, ...); 
  12.  

calloc_a示例:

  1. #include <string.h> 
  2. #include <libubox/utils.h> 
  3.  
  4. struct sleeper { 
  5. int aa; 
  6. int bb; 
  7. }; 
  8.  
  9. #define NAME_LEN 32 
  10. int main(int argc, char **argv) 

  11. struct sleeper *s; 
  12. char *name; 
  13. int *a1; 
  14. s = (struct sleeper *)calloc_a(sizeof(*s), &name, NAME_LEN, &a1, sizeof(*a1)); 
  15. s->aa = 0x10101010
  16. s->bb = 0x20202020
  17. strncpy(name, "SSSSSSSSSSSSSSSSSSSSSSSS", NAME_LEN-1); 
  18. name[NAME_LEN-1] = ‘\0‘
  19. *a1 = 0xaaaaaaaa
  20. free(s); 
  21. return 0

  22.  
  23. (gdb) x/20x s 
  24. 0x602010: 0x10101010 0x20202020 0x53535353 0x53535353 
  25. 0x602020: 0x53535353 0x53535353 0x53535353 0x53535353 
  26. 0x602030: 0x00000000 0x00000000 0xaaaaaaaa 0x00000000 
技术分享
  1. /* 
  2. * 计算数组大小 
  3. */ 
  4. #ifndef ARRAY_SIZE 
  5. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 
  6. #endif 
  1. /* 
  2. * BUILD_BUG_ON基于一个GCC不支持负数数组,在编译时报错 
  3. * 但是4.4后GCC支持变长数组,不会引起编译器报错。 
  4. * 加入GCC优化选项后,编译时不会错,但是链接时会有找不到符号__BUILD_BUG_ON_CONDITION_FAILED的错误 
  5. */ 
  6. #define __BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 
  7.  
  8. #ifdef __OPTIMIZE__ 
  9. extern int __BUILD_BUG_ON_CONDITION_FAILED; 
  10. #define BUILD_BUG_ON(condition) \ 
  11. do { \ 
  12. __BUILD_BUG_ON(condition); \ 
  13. if (condition) \ 
  14. __BUILD_BUG_ON_CONDITION_FAILED = 1; \ 
  15. } while(0) 
  16. #else 
  17. #define BUILD_BUG_ON __BUILD_BUG_ON 
  18. #endif 

FIXUP:

  1. /* Force a compilation error if condition is true */ 
  2. -#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 
  3. +#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition)) 

  4. +/* Force a compilation error if condition is constant and true */ 
  5. +#define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)])) 
  6.  
  7. /* Force a compilation error if condition is true, but also produce a 
  8. result (of value 0 and type size_t), so the expression can be used 
  9. e.g. in a structure initializer (or where-ever else comma expressions 
  10. aren‘t permitted). */ 
  11. -#define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1) 
  12. +#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) 
  13. +#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); })) 
  1. /* 
  2. */ 
  3. #ifdef __APPLE__ 
  4.  
  5. #define CLOCK_REALTIME 0 
  6. #define CLOCK_MONOTONIC 1 
  7.  
  8. void clock_gettime(int type, struct timespec *tv); 
  9.  
  10. #endif 
  1. #ifdef __GNUC__ 
  2. #define _GNUC_MIN_VER(maj, min) (((__GNUC__ << 8) + __GNUC_MINOR__) >= (((maj) << 8) + (min))) 
  3. #else 
  4. #define _GNUC_MIN_VER(maj, min) 0 
  5. #endif 
  6.  
  7. #if defined(__linux__) || defined(__CYGWIN__) 
  8. #include <byteswap.h> 
  9. #include <endian.h> 
  10.  
  11. #elif defined(__APPLE__) 
  12. #include <machine/endian.h> 
  13. #include <machine/byte_order.h> 
  14. #define bswap_32(x) OSSwapInt32(x) 
  15. #define bswap_64(x) OSSwapInt64(x) 
  16. #elif defined(__FreeBSD__) 
  17. #include <sys/endian.h> 
  18. #define bswap_32(x) bswap32(x) 
  19. #define bswap_64(x) bswap64(x) 
  20. #else 
  21. #include <machine/endian.h> 
  22. #define bswap_32(x) swap32(x) 
  23. #define bswap_64(x) swap64(x) 
  24. #endif 
  25.  
  26. #ifndef __BYTE_ORDER 
  27. #define __BYTE_ORDER BYTE_ORDER 
  28. #endif 
  29. #ifndef __BIG_ENDIAN 
  30. #define __BIG_ENDIAN BIG_ENDIAN 
  31. #endif 
  32. #ifndef __LITTLE_ENDIAN 
  33. #define __LITTLE_ENDIAN LITTLE_ENDIAN 
  34. #endif 
  35.  
  36. #define __u_bswap16(x) ({ uint16_t val = (x); ((uint16_t)(((val >> 8) & 0xffu) | ((val & 0xffu) << 8))); }) 
  37.  
  38. #if _GNUC_MIN_VER(4, 2) 
  39. #define __u_bswap32(x) __builtin_bswap32(x) 
  40. #define __u_bswap64(x) __builtin_bswap64(x) 
  41. #else 
  42. #define __u_bswap32(x) bswap_32(x) 
  43. #define __u_bswap64(x) bswap_64(x) 
  44. #endif 
  45.  
  46. #if __BYTE_ORDER == __LITTLE_ENDIAN 
  47.  
  48. #define cpu_to_be64(x) __u_bswap64(x) 
  49. #define cpu_to_be32(x) __u_bswap32(x) 
  50. #define cpu_to_be16(x) __u_bswap16((uint16_t) (x)) 
  51.  
  52. #define be64_to_cpu(x) __u_bswap64(x) 
  53. #define be32_to_cpu(x) __u_bswap32(x) 
  54. #define be16_to_cpu(x) __u_bswap16((uint16_t) (x)) 
  55.  
  56. #define cpu_to_le64(x) (x) 
  57. #define cpu_to_le32(x) (x) 
  58. #define cpu_to_le16(x) (x) 
  59.  
  60. #define le64_to_cpu(x) (x) 
  61. #define le32_to_cpu(x) (x) 
  62. #define le16_to_cpu(x) (x) 
  63.  
  64. #else /* __BYTE_ORDER == __LITTLE_ENDIAN */ 
  65.  
  66. #define cpu_to_le64(x) __u_bswap64(x) 
  67. #define cpu_to_le32(x) __u_bswap32(x) 
  68. #define cpu_to_le16(x) __u_bswap16((uint16_t) (x)) 
  69.  
  70. #define le64_to_cpu(x) __u_bswap64(x) 
  71. #define le32_to_cpu(x) __u_bswap32(x) 
  72. #define le16_to_cpu(x) __u_bswap16((uint16_t) (x)) 
  73.  
  74. #define cpu_to_be64(x) (x) 
  75. #define cpu_to_be32(x) (x) 
  76. #define cpu_to_be16(x) (x) 
  77.  
  78. #define be64_to_cpu(x) (x) 
  79. #define be32_to_cpu(x) (x) 
  80. #define be16_to_cpu(x) (x) 
  81.  
  82. #endif 
  83.  
  84. #ifndef __packed 
  85. #define __packed __attribute__((packed)) 
  86. #endif 
  87.  
  88. #ifndef BITS_PER_LONG 
  89. #define BITS_PER_LONG (8 * sizeof(unsigned long)) 
  90. #endif 
  91.  
  92. static inline void bitfield_set(unsigned long *bits, int bit) 

  93. bits[bit / BITS_PER_LONG] |= (1UL << (bit % BITS_PER_LONG)); 

  94.  
  95. static inline bool bitfield_test(unsigned long *bits, int bit) 

  96. return !!(bits[bit / BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))); 

  97.  
  98. #endif 

Leilei Wang

2017-1-15-libubox analysis

原文:http://www.cnblogs.com/nicephil/p/6288097.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!