util.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2007, Novell Inc.
  3. *
  4. * This program is licensed under the BSD license, read LICENSE.BSD
  5. * for further information
  6. */
  7. /*
  8. * util.h
  9. *
  10. */
  11. #ifndef LIBSOLV_UTIL_H
  12. #define LIBSOLV_UTIL_H
  13. #include <stddef.h>
  14. #include <string.h>
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /**
  19. * malloc
  20. * exits with error message on error
  21. */
  22. extern void *solv_malloc(size_t);
  23. extern void *solv_malloc2(size_t, size_t);
  24. extern void *solv_calloc(size_t, size_t);
  25. extern void *solv_realloc(void *, size_t);
  26. extern void *solv_realloc2(void *, size_t, size_t);
  27. extern void *solv_extend_realloc(void *, size_t, size_t, size_t);
  28. extern void *solv_free(void *);
  29. extern char *solv_strdup(const char *);
  30. extern void solv_oom(size_t, size_t);
  31. extern unsigned int solv_timems(unsigned int subtract);
  32. extern int solv_setcloexec(int fd, int state);
  33. extern void solv_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard);
  34. extern char *solv_dupjoin(const char *str1, const char *str2, const char *str3);
  35. extern char *solv_dupappend(const char *str1, const char *str2, const char *str3);
  36. extern int solv_hex2bin(const char **strp, unsigned char *buf, int bufl);
  37. extern char *solv_bin2hex(const unsigned char *buf, int l, char *str);
  38. extern size_t solv_validutf8(const char *buf);
  39. extern char *solv_latin1toutf8(const char *buf);
  40. extern char *solv_replacebadutf8(const char *buf, int replchar);
  41. static inline void *solv_extend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
  42. {
  43. if (nmemb == 1)
  44. {
  45. if ((len & block) == 0)
  46. buf = solv_extend_realloc(buf, len + 1, size, block);
  47. }
  48. else
  49. {
  50. if (((len - 1) | block) != ((len + nmemb - 1) | block))
  51. buf = solv_extend_realloc(buf, len + nmemb, size, block);
  52. }
  53. return buf;
  54. }
  55. /**
  56. * extend an array by reallocation and zero's the new section
  57. * buf old pointer
  58. * len current size
  59. * nmbemb number of elements to add
  60. * size size of each element
  61. * block block size used to allocate the elements
  62. */
  63. static inline void *solv_zextend(void *buf, size_t len, size_t nmemb, size_t size, size_t block)
  64. {
  65. buf = solv_extend(buf, len, nmemb, size, block);
  66. memset((char *)buf + len * size, 0, nmemb * size);
  67. return buf;
  68. }
  69. static inline void *solv_extend_resize(void *buf, size_t len, size_t size, size_t block)
  70. {
  71. if (len)
  72. buf = solv_extend_realloc(buf, len, size, block);
  73. return buf;
  74. }
  75. static inline void *solv_calloc_block(size_t len, size_t size, size_t block)
  76. {
  77. void *buf;
  78. if (!len)
  79. return 0;
  80. buf = solv_extend_realloc((void *)0, len, size, block);
  81. memset(buf, 0, ((len + block) & ~block) * size);
  82. return buf;
  83. }
  84. static inline void *solv_memdup(const void *buf, size_t len)
  85. {
  86. void *newbuf;
  87. if (!buf)
  88. return 0;
  89. newbuf = solv_malloc(len);
  90. if (len)
  91. memcpy(newbuf, buf, len);
  92. return newbuf;
  93. }
  94. static inline void *solv_memdup2(const void *buf, size_t num, size_t len)
  95. {
  96. void *newbuf;
  97. if (!buf)
  98. return 0;
  99. newbuf = solv_malloc2(num, len);
  100. if (num)
  101. memcpy(newbuf, buf, num * len);
  102. return newbuf;
  103. }
  104. #ifdef __cplusplus
  105. }
  106. #endif
  107. #endif /* LIBSOLV_UTIL_H */