common.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * Author......: Jens Steube <jens.steube@gmail.com>
  3. * License.....: MIT
  4. */
  5. #include "common.h"
  6. #define MSG_ENOMEM "Insufficient memory available"
  7. void dump_hex (const char *s, size_t size)
  8. {
  9. size_t i;
  10. for (i = 0; i < size; i++)
  11. {
  12. printf ("%02x ", (unsigned char) s[i]);
  13. }
  14. printf ("\n");
  15. }
  16. void log_msg (FILE *fp, const char *fmt, va_list ap)
  17. {
  18. vfprintf (fp, fmt, ap);
  19. fprintf (fp, "\n");
  20. }
  21. void log_info (const char *fmt, ...)
  22. {
  23. va_list ap;
  24. va_start (ap, fmt);
  25. log_msg (stdout, fmt, ap);
  26. va_end (ap);
  27. }
  28. void log_warning (const char *fmt, ...)
  29. {
  30. va_list ap;
  31. va_start (ap, fmt);
  32. log_msg (stderr, fmt, ap);
  33. va_end (ap);
  34. }
  35. void log_error (const char *fmt, ...)
  36. {
  37. va_list ap;
  38. va_start (ap, fmt);
  39. fprintf (stderr, "\n\n");
  40. log_msg (stderr, fmt, ap);
  41. va_end (ap);
  42. }
  43. uint32_t get_random_num (uint32_t min, uint32_t max)
  44. {
  45. if (min == max) return (min);
  46. return ((rand () % (max - min)) + min);
  47. }
  48. void *mycalloc (size_t nmemb, size_t size)
  49. {
  50. void *p = calloc (nmemb, size);
  51. if (p == NULL)
  52. {
  53. log_error ("%s", MSG_ENOMEM);
  54. exit (-1);
  55. }
  56. return (p);
  57. }
  58. void *mymalloc (size_t size)
  59. {
  60. void *p = malloc (size);
  61. if (p == NULL)
  62. {
  63. log_error ("%s", MSG_ENOMEM);
  64. exit (-1);
  65. }
  66. return (p);
  67. }
  68. void *malloc_tiny (const size_t size)
  69. {
  70. // this alloc system reduced the number of memory fragment drastically
  71. #define MEM_ALLOC_SIZE 0x10000
  72. if (size > MEM_ALLOC_SIZE)
  73. {
  74. // we can't handle it here
  75. return mymalloc (size);
  76. }
  77. static char *buffer = NULL;
  78. static size_t bufree = 0;
  79. if (size > bufree)
  80. {
  81. buffer = mymalloc (MEM_ALLOC_SIZE);
  82. bufree = MEM_ALLOC_SIZE;
  83. }
  84. char *p = buffer;
  85. buffer += size;
  86. bufree -= size;
  87. return p;
  88. }
  89. void myfree (void *ptr)
  90. {
  91. free (ptr);
  92. }
  93. void *myrealloc (void *ptr, size_t size)
  94. {
  95. void *p = realloc (ptr, size);
  96. if (p == NULL)
  97. {
  98. log_error ("%s", MSG_ENOMEM);
  99. exit (-1);
  100. }
  101. return (p);
  102. }
  103. char *mystrdup (const char *s)
  104. {
  105. char *b = mymalloc (strlen (s) + 1);
  106. strcpy (b, s);
  107. return (b);
  108. }
  109. int in_superchop (char *buf)
  110. {
  111. int len = strlen (buf);
  112. while (len)
  113. {
  114. if (buf[len - 1] == '\n')
  115. {
  116. len--;
  117. continue;
  118. }
  119. if (buf[len - 1] == '\r')
  120. {
  121. len--;
  122. continue;
  123. }
  124. break;
  125. }
  126. buf[len] = 0;
  127. return len;
  128. }