Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

414 lines
9.7KB

  1. /*
  2. * Carla common utils
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_UTILS_HPP_INCLUDED
  18. #define CARLA_UTILS_HPP_INCLUDED
  19. #include "CarlaDefines.h"
  20. #include <cassert>
  21. #include <cstdarg>
  22. #include <cstdio>
  23. #include <cstdlib>
  24. #include <cstring>
  25. #ifdef CARLA_PROPER_CPP11_SUPPORT
  26. # include <cstdint>
  27. #else
  28. # include <stdint.h>
  29. #endif
  30. #ifdef CARLA_OS_WIN
  31. # include <winsock2.h>
  32. # include <windows.h>
  33. #else
  34. # include <unistd.h>
  35. #endif
  36. // -----------------------------------------------------------------------
  37. // misc functions
  38. /*
  39. * Return "true" or "false" according to yesNo.
  40. */
  41. static inline
  42. const char* bool2str(const bool yesNo) noexcept
  43. {
  44. return yesNo ? "true" : "false";
  45. }
  46. /*
  47. * Set a string as empty/null.
  48. */
  49. static inline
  50. void nullStrBuf(char* const strBuf) noexcept
  51. {
  52. strBuf[0] = '\0';
  53. }
  54. /*
  55. * Dummy function.
  56. */
  57. static inline
  58. void pass() noexcept {}
  59. // -----------------------------------------------------------------------
  60. // string print functions
  61. /*
  62. * Print a string to stdout with newline (gray color).
  63. * Does nothing if DEBUG is not defined.
  64. */
  65. #ifndef DEBUG
  66. # define carla_debug(...)
  67. #else
  68. static inline
  69. void carla_debug(const char* const fmt, ...) noexcept
  70. {
  71. try {
  72. ::va_list args;
  73. ::va_start(args, fmt);
  74. std::fprintf(stdout, "\x1b[30;1m");
  75. std::vfprintf(stdout, fmt, args);
  76. std::fprintf(stdout, "\x1b[0m\n");
  77. ::va_end(args);
  78. } catch (...) {}
  79. }
  80. #endif
  81. /*
  82. * Print a string to stdout with newline.
  83. */
  84. static inline
  85. void carla_stdout(const char* const fmt, ...) noexcept
  86. {
  87. try {
  88. ::va_list args;
  89. ::va_start(args, fmt);
  90. std::vfprintf(stdout, fmt, args);
  91. std::fprintf(stdout, "\n");
  92. ::va_end(args);
  93. } catch (...) {}
  94. }
  95. /*
  96. * Print a string to stderr with newline.
  97. */
  98. static inline
  99. void carla_stderr(const char* const fmt, ...) noexcept
  100. {
  101. try {
  102. ::va_list args;
  103. ::va_start(args, fmt);
  104. std::vfprintf(stderr, fmt, args);
  105. std::fprintf(stderr, "\n");
  106. ::va_end(args);
  107. } catch (...) {}
  108. }
  109. /*
  110. * Print a string to stderr with newline (red color).
  111. */
  112. static inline
  113. void carla_stderr2(const char* const fmt, ...) noexcept
  114. {
  115. try {
  116. ::va_list args;
  117. ::va_start(args, fmt);
  118. std::fprintf(stderr, "\x1b[31m");
  119. std::vfprintf(stderr, fmt, args);
  120. std::fprintf(stderr, "\x1b[0m\n");
  121. ::va_end(args);
  122. } catch (...) {}
  123. }
  124. // -----------------------------------------------------------------------
  125. // carla_safe_assert*
  126. /*
  127. * Print a safe assertion error message.
  128. */
  129. static inline
  130. void carla_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
  131. {
  132. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  133. }
  134. /*
  135. * Print a safe assertion error message, with 1 extra integer value.
  136. */
  137. static inline
  138. void carla_safe_assert_int(const char* const assertion, const char* const file, const int line, const int value) noexcept
  139. {
  140. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
  141. }
  142. static inline
  143. void carla_safe_assert_uint(const char* const assertion, const char* const file, const int line, const uint value) noexcept
  144. {
  145. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %u", assertion, file, line, value);
  146. }
  147. /*
  148. * Print a safe assertion error message, with 2 extra integer values.
  149. */
  150. static inline
  151. void carla_safe_assert_int2(const char* const assertion, const char* const file, const int line, const int v1, const int v2) noexcept
  152. {
  153. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion, file, line, v1, v2);
  154. }
  155. static inline
  156. void carla_safe_assert_uint2(const char* const assertion, const char* const file, const int line, const uint v1, const uint v2) noexcept
  157. {
  158. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %u, v2 %u", assertion, file, line, v1, v2);
  159. }
  160. // -----------------------------------------------------------------------
  161. // carla_safe_exception*
  162. /*
  163. * Print a safe exception error message.
  164. */
  165. static inline
  166. void carla_safe_exception(const char* const exception, const char* const file, const int line) noexcept
  167. {
  168. carla_stderr2("Carla exception caught: \"%s\" in file %s, line %i", exception, file, line);
  169. }
  170. // -----------------------------------------------------------------------
  171. // carla_*sleep
  172. /*
  173. * Sleep for 'secs' seconds.
  174. */
  175. static inline
  176. void carla_sleep(const uint secs) noexcept
  177. {
  178. CARLA_SAFE_ASSERT_RETURN(secs > 0,);
  179. try {
  180. #ifdef CARLA_OS_WIN
  181. ::Sleep(secs * 1000);
  182. #else
  183. ::sleep(secs);
  184. #endif
  185. } CARLA_SAFE_EXCEPTION("carla_sleep");
  186. }
  187. /*
  188. * Sleep for 'msecs' milliseconds.
  189. */
  190. static inline
  191. void carla_msleep(const uint msecs) noexcept
  192. {
  193. CARLA_SAFE_ASSERT_RETURN(msecs > 0,);
  194. try {
  195. #ifdef CARLA_OS_WIN
  196. ::Sleep(msecs);
  197. #else
  198. ::usleep(msecs * 1000);
  199. #endif
  200. } CARLA_SAFE_EXCEPTION("carla_msleep");
  201. }
  202. // -----------------------------------------------------------------------
  203. // carla_setenv
  204. /*
  205. * Set environment variable 'key' to 'value'.
  206. */
  207. static inline
  208. void carla_setenv(const char* const key, const char* const value) noexcept
  209. {
  210. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  211. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  212. #ifdef CARLA_OS_WIN
  213. try {
  214. ::SetEnvironmentVariableA(key, value);
  215. } CARLA_SAFE_EXCEPTION("carla_setenv");
  216. #else
  217. ::setenv(key, value, 1);
  218. #endif
  219. }
  220. // -----------------------------------------------------------------------
  221. // carla_strdup
  222. /*
  223. * Custom 'strdup' function.
  224. * Returned value is always valid, and must be freed with "delete[] var".
  225. * May throw.
  226. */
  227. static inline
  228. const char* carla_strdup(const char* const strBuf)
  229. {
  230. CARLA_SAFE_ASSERT(strBuf != nullptr);
  231. const size_t bufferLen = (strBuf != nullptr) ? std::strlen(strBuf) : 0;
  232. char* const buffer = new char[bufferLen+1];
  233. if (strBuf != nullptr && bufferLen > 0)
  234. std::strncpy(buffer, strBuf, bufferLen);
  235. buffer[bufferLen] = '\0';
  236. return buffer;
  237. }
  238. /*
  239. * Custom 'strdup' function.
  240. * Calls "std::free(strBuf)".
  241. * Returned value is always valid, and must be freed with "delete[] var".
  242. * May throw.
  243. */
  244. static inline
  245. const char* carla_strdup_free(char* const strBuf)
  246. {
  247. const char* const buffer(carla_strdup(strBuf));
  248. std::free(strBuf);
  249. return buffer;
  250. }
  251. // -----------------------------------------------------------------------
  252. // memory functions
  253. /*
  254. * Add array values to another array.
  255. */
  256. template<typename T>
  257. static inline
  258. void carla_add(T* dataDst, const T* dataSrc, const size_t size) noexcept
  259. {
  260. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  261. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  262. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  263. for (size_t i=0; i < size; ++i)
  264. *dataDst++ += *dataSrc++;
  265. }
  266. /*
  267. * Copy array values to another array.
  268. */
  269. template<typename T>
  270. static inline
  271. void carla_copy(T* const dataDst, const T* const dataSrc, const size_t size) noexcept
  272. {
  273. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  274. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  275. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  276. std::memcpy(dataDst, dataSrc, size*sizeof(T));
  277. }
  278. /*
  279. * Fill an array with a fixed value.
  280. */
  281. template<typename T>
  282. static inline
  283. void carla_fill(T* data, const size_t size, const T v) noexcept
  284. {
  285. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  286. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  287. if (v == 0)
  288. {
  289. std::memset(data, 0, size*sizeof(T));
  290. }
  291. else
  292. {
  293. for (size_t i=0; i < size; ++i)
  294. *data++ = v;
  295. }
  296. }
  297. /*
  298. * Clear a byte array.
  299. */
  300. static inline
  301. void carla_zeroBytes(void* const memory, const size_t numBytes) noexcept
  302. {
  303. CARLA_SAFE_ASSERT_RETURN(memory != nullptr,);
  304. CARLA_SAFE_ASSERT_RETURN(numBytes > 0,);
  305. std::memset(memory, 0, numBytes);
  306. }
  307. /*
  308. * Clear a char array.
  309. */
  310. static inline
  311. void carla_zeroChar(char* const data, const size_t numChars) noexcept
  312. {
  313. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  314. CARLA_SAFE_ASSERT_RETURN(numChars > 0,);
  315. std::memset(data, 0, numChars*sizeof(char));
  316. }
  317. /*
  318. * Clear a single struct/class.
  319. */
  320. template <typename T>
  321. static inline
  322. void carla_zeroStruct(T& structure) noexcept
  323. {
  324. std::memset(&structure, 0, sizeof(T));
  325. }
  326. /*
  327. * Clear an array of struct/classes.
  328. */
  329. template <typename T>
  330. static inline
  331. void carla_zeroStruct(T* const structure, const size_t count) noexcept
  332. {
  333. CARLA_SAFE_ASSERT_RETURN(structure != nullptr,);
  334. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  335. std::memset(structure, 0, count*sizeof(T));
  336. }
  337. /*
  338. * Copy a single struct/class.
  339. */
  340. template <typename T>
  341. static inline
  342. void carla_copyStruct(T& struct1, const T& struct2) noexcept
  343. {
  344. std::memcpy(&struct1, &struct2, sizeof(T));
  345. }
  346. /*
  347. * Copy an array of struct/classes.
  348. */
  349. template <typename T>
  350. static inline
  351. void carla_copyStruct(T* const struct1, const T* const struct2, const size_t count) noexcept
  352. {
  353. CARLA_SAFE_ASSERT_RETURN(struct1 != nullptr,);
  354. CARLA_SAFE_ASSERT_RETURN(struct2 != nullptr,);
  355. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  356. std::memcpy(struct1, struct2, count*sizeof(T));
  357. }
  358. // -----------------------------------------------------------------------
  359. #endif // CARLA_UTILS_HPP_INCLUDED