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.

463 lines
11KB

  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. #ifndef QTCREATOR_TEST
  75. std::fprintf(stdout, "\x1b[30;1m");
  76. #endif
  77. std::vfprintf(stdout, fmt, args);
  78. #ifndef QTCREATOR_TEST
  79. std::fprintf(stdout, "\x1b[0m\n");
  80. #else
  81. std::fprintf(stdout, "\n");
  82. #endif
  83. ::va_end(args);
  84. } catch (...) {}
  85. }
  86. #endif
  87. /*
  88. * Print a string to stdout with newline.
  89. */
  90. static inline
  91. void carla_stdout(const char* const fmt, ...) noexcept
  92. {
  93. try {
  94. ::va_list args;
  95. ::va_start(args, fmt);
  96. std::vfprintf(stdout, fmt, args);
  97. std::fprintf(stdout, "\n");
  98. ::va_end(args);
  99. } catch (...) {}
  100. }
  101. /*
  102. * Print a string to stderr with newline.
  103. */
  104. static inline
  105. void carla_stderr(const char* const fmt, ...) noexcept
  106. {
  107. try {
  108. ::va_list args;
  109. ::va_start(args, fmt);
  110. std::vfprintf(stderr, fmt, args);
  111. std::fprintf(stderr, "\n");
  112. ::va_end(args);
  113. } catch (...) {}
  114. }
  115. /*
  116. * Print a string to stderr with newline (red color).
  117. */
  118. static inline
  119. void carla_stderr2(const char* const fmt, ...) noexcept
  120. {
  121. try {
  122. ::va_list args;
  123. ::va_start(args, fmt);
  124. #ifndef QTCREATOR_TEST
  125. std::fprintf(stderr, "\x1b[31m");
  126. #endif
  127. std::vfprintf(stderr, fmt, args);
  128. #ifndef QTCREATOR_TEST
  129. std::fprintf(stderr, "\x1b[0m\n");
  130. #else
  131. std::fprintf(stderr, "\n");
  132. #endif
  133. ::va_end(args);
  134. } catch (...) {}
  135. }
  136. // -----------------------------------------------------------------------
  137. // carla_safe_assert*
  138. /*
  139. * Print a safe assertion error message.
  140. */
  141. static inline
  142. void carla_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
  143. {
  144. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  145. }
  146. /*
  147. * Print a safe assertion error message, with 1 extra integer value.
  148. */
  149. static inline
  150. void carla_safe_assert_int(const char* const assertion, const char* const file, const int line, const int value) noexcept
  151. {
  152. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
  153. }
  154. static inline
  155. void carla_safe_assert_uint(const char* const assertion, const char* const file, const int line, const uint value) noexcept
  156. {
  157. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %u", assertion, file, line, value);
  158. }
  159. /*
  160. * Print a safe assertion error message, with 2 extra integer values.
  161. */
  162. static inline
  163. void carla_safe_assert_int2(const char* const assertion, const char* const file, const int line, const int v1, const int v2) noexcept
  164. {
  165. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion, file, line, v1, v2);
  166. }
  167. static inline
  168. void carla_safe_assert_uint2(const char* const assertion, const char* const file, const int line, const uint v1, const uint v2) noexcept
  169. {
  170. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %u, v2 %u", assertion, file, line, v1, v2);
  171. }
  172. // -----------------------------------------------------------------------
  173. // carla_safe_exception*
  174. /*
  175. * Print a safe exception error message.
  176. */
  177. static inline
  178. void carla_safe_exception(const char* const exception, const char* const file, const int line) noexcept
  179. {
  180. carla_stderr2("Carla exception caught: \"%s\" in file %s, line %i", exception, file, line);
  181. }
  182. // -----------------------------------------------------------------------
  183. // carla_*sleep
  184. /*
  185. * Sleep for 'secs' seconds.
  186. */
  187. static inline
  188. void carla_sleep(const uint secs) noexcept
  189. {
  190. CARLA_SAFE_ASSERT_RETURN(secs > 0,);
  191. try {
  192. #ifdef CARLA_OS_WIN
  193. ::Sleep(secs * 1000);
  194. #else
  195. ::sleep(secs);
  196. #endif
  197. } CARLA_SAFE_EXCEPTION("carla_sleep");
  198. }
  199. /*
  200. * Sleep for 'msecs' milliseconds.
  201. */
  202. static inline
  203. void carla_msleep(const uint msecs) noexcept
  204. {
  205. CARLA_SAFE_ASSERT_RETURN(msecs > 0,);
  206. try {
  207. #ifdef CARLA_OS_WIN
  208. ::Sleep(msecs);
  209. #else
  210. ::usleep(msecs * 1000);
  211. #endif
  212. } CARLA_SAFE_EXCEPTION("carla_msleep");
  213. }
  214. // -----------------------------------------------------------------------
  215. // carla_setenv
  216. /*
  217. * Set environment variable 'key' to 'value'.
  218. */
  219. static inline
  220. void carla_setenv(const char* const key, const char* const value) noexcept
  221. {
  222. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  223. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  224. #ifdef CARLA_OS_WIN
  225. try {
  226. ::SetEnvironmentVariableA(key, value);
  227. } CARLA_SAFE_EXCEPTION("carla_setenv");
  228. #else
  229. ::setenv(key, value, 1);
  230. #endif
  231. }
  232. // -----------------------------------------------------------------------
  233. // carla_strdup
  234. /*
  235. * Custom 'strdup' function.
  236. * Returned value is always valid, and must be freed with "delete[] var".
  237. * May throw.
  238. */
  239. static inline
  240. const char* carla_strdup(const char* const strBuf)
  241. {
  242. CARLA_SAFE_ASSERT(strBuf != nullptr);
  243. const std::size_t bufferLen = (strBuf != nullptr) ? std::strlen(strBuf) : 0;
  244. char* const buffer = new char[bufferLen+1];
  245. if (strBuf != nullptr && bufferLen > 0)
  246. std::strncpy(buffer, strBuf, bufferLen);
  247. buffer[bufferLen] = '\0';
  248. return buffer;
  249. }
  250. /*
  251. * Custom 'strdup' function.
  252. * Calls "std::free(strBuf)".
  253. * Returned value is always valid, and must be freed with "delete[] var".
  254. * May throw.
  255. */
  256. static inline
  257. const char* carla_strdup_free(char* const strBuf)
  258. {
  259. const char* const buffer(carla_strdup(strBuf));
  260. std::free(strBuf);
  261. return buffer;
  262. }
  263. /*
  264. * Custom 'strdup' function, safe version.
  265. * Returned value may be null.
  266. */
  267. static inline
  268. const char* carla_strdup_safe(const char* const strBuf) noexcept
  269. {
  270. CARLA_SAFE_ASSERT(strBuf != nullptr);
  271. const std::size_t bufferLen = (strBuf != nullptr) ? std::strlen(strBuf) : 0;
  272. char* buffer;
  273. try {
  274. buffer = new char[bufferLen+1];
  275. } CARLA_SAFE_EXCEPTION_RETURN("carla_strdup_safe", nullptr);
  276. if (strBuf != nullptr && bufferLen > 0)
  277. std::strncpy(buffer, strBuf, bufferLen);
  278. buffer[bufferLen] = '\0';
  279. return buffer;
  280. }
  281. // -----------------------------------------------------------------------
  282. // memory functions
  283. /*
  284. * Add array values to another array.
  285. */
  286. template<typename T>
  287. static inline
  288. void carla_add(T* dataDst, const T* dataSrc, const std::size_t size) noexcept
  289. {
  290. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  291. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  292. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  293. for (std::size_t i=0; i < size; ++i)
  294. *dataDst++ += *dataSrc++;
  295. }
  296. /*
  297. * Copy array values to another array.
  298. */
  299. template<typename T>
  300. static inline
  301. void carla_copy(T* const dataDst, const T* const dataSrc, const std::size_t size) noexcept
  302. {
  303. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  304. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  305. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  306. std::memcpy(dataDst, dataSrc, size*sizeof(T));
  307. }
  308. /*
  309. * Fill an array with a fixed value.
  310. */
  311. template<typename T>
  312. static inline
  313. void carla_fill(T* data, const T v, const std::size_t size) noexcept
  314. {
  315. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  316. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  317. if (v == 0)
  318. {
  319. std::memset(data, 0, size*sizeof(T));
  320. }
  321. else
  322. {
  323. for (std::size_t i=0; i < size; ++i)
  324. *data++ = v;
  325. }
  326. }
  327. /*
  328. * Clear a byte array.
  329. */
  330. static inline
  331. void carla_zeroBytes(void* const memory, const std::size_t numBytes) noexcept
  332. {
  333. CARLA_SAFE_ASSERT_RETURN(memory != nullptr,);
  334. CARLA_SAFE_ASSERT_RETURN(numBytes > 0,);
  335. std::memset(memory, 0, numBytes);
  336. }
  337. /*
  338. * Clear a char array.
  339. */
  340. static inline
  341. void carla_zeroChar(char* const data, const std::size_t numChars) noexcept
  342. {
  343. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  344. CARLA_SAFE_ASSERT_RETURN(numChars > 0,);
  345. std::memset(data, 0, numChars*sizeof(char));
  346. }
  347. /*
  348. * Clear a pointer array.
  349. */
  350. template<typename T>
  351. static inline
  352. void carla_zeroPointers(T* pointers[], const std::size_t numPointers) noexcept
  353. {
  354. CARLA_SAFE_ASSERT_RETURN(pointers != nullptr,);
  355. CARLA_SAFE_ASSERT_RETURN(numPointers > 0,);
  356. std::memset(pointers, 0, numPointers*sizeof(T*));
  357. }
  358. /*
  359. * Clear a single struct/class.
  360. */
  361. template <typename T>
  362. static inline
  363. void carla_zeroStruct(T& structure) noexcept
  364. {
  365. std::memset(&structure, 0, sizeof(T));
  366. }
  367. /*
  368. * Clear an array of struct/classes.
  369. */
  370. template <typename T>
  371. static inline
  372. void carla_zeroStruct(T* const structure, const std::size_t count) noexcept
  373. {
  374. CARLA_SAFE_ASSERT_RETURN(structure != nullptr,);
  375. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  376. std::memset(structure, 0, count*sizeof(T));
  377. }
  378. /*
  379. * Copy a single struct/class.
  380. */
  381. template <typename T>
  382. static inline
  383. void carla_copyStruct(T& struct1, const T& struct2) noexcept
  384. {
  385. std::memcpy(&struct1, &struct2, sizeof(T));
  386. }
  387. /*
  388. * Copy an array of struct/classes.
  389. */
  390. template <typename T>
  391. static inline
  392. void carla_copyStruct(T* const struct1, const T* const struct2, const std::size_t count) noexcept
  393. {
  394. CARLA_SAFE_ASSERT_RETURN(struct1 != nullptr,);
  395. CARLA_SAFE_ASSERT_RETURN(struct2 != nullptr,);
  396. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  397. std::memcpy(struct1, struct2, count*sizeof(T));
  398. }
  399. // -----------------------------------------------------------------------
  400. #endif // CARLA_UTILS_HPP_INCLUDED