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.

484 lines
12KB

  1. /*
  2. * Carla common utils
  3. * Copyright (C) 2011-2015 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,
  151. const int value) noexcept
  152. {
  153. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
  154. }
  155. static inline
  156. void carla_safe_assert_uint(const char* const assertion, const char* const file, const int line,
  157. const uint value) noexcept
  158. {
  159. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %u", assertion, file, line, value);
  160. }
  161. /*
  162. * Print a safe assertion error message, with 2 extra integer values.
  163. */
  164. static inline
  165. void carla_safe_assert_int2(const char* const assertion, const char* const file, const int line,
  166. const int v1, const int v2) noexcept
  167. {
  168. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion, file, line, v1, v2);
  169. }
  170. static inline
  171. void carla_safe_assert_uint2(const char* const assertion, const char* const file, const int line,
  172. const uint v1, const uint v2) noexcept
  173. {
  174. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %u, v2 %u", assertion, file, line, v1, v2);
  175. }
  176. // --------------------------------------------------------------------------------------------------------------------
  177. // carla_safe_exception*
  178. /*
  179. * Print a safe exception error message.
  180. */
  181. static inline
  182. void carla_safe_exception(const char* const exception, const char* const file, const int line) noexcept
  183. {
  184. carla_stderr2("Carla exception caught: \"%s\" in file %s, line %i", exception, file, line);
  185. }
  186. // --------------------------------------------------------------------------------------------------------------------
  187. // carla_*sleep
  188. /*
  189. * Sleep for 'secs' seconds.
  190. */
  191. static inline
  192. void carla_sleep(const uint secs) noexcept
  193. {
  194. CARLA_SAFE_ASSERT_RETURN(secs > 0,);
  195. try {
  196. #ifdef CARLA_OS_WIN
  197. ::Sleep(secs * 1000);
  198. #else
  199. ::sleep(secs);
  200. #endif
  201. } CARLA_SAFE_EXCEPTION("carla_sleep");
  202. }
  203. /*
  204. * Sleep for 'msecs' milliseconds.
  205. */
  206. static inline
  207. void carla_msleep(const uint msecs) noexcept
  208. {
  209. CARLA_SAFE_ASSERT_RETURN(msecs > 0,);
  210. try {
  211. #ifdef CARLA_OS_WIN
  212. ::Sleep(msecs);
  213. #else
  214. ::usleep(msecs * 1000);
  215. #endif
  216. } CARLA_SAFE_EXCEPTION("carla_msleep");
  217. }
  218. // --------------------------------------------------------------------------------------------------------------------
  219. // carla_setenv
  220. /*
  221. * Set environment variable 'key' to 'value'.
  222. */
  223. static inline
  224. void carla_setenv(const char* const key, const char* const value) noexcept
  225. {
  226. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  227. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  228. #ifdef CARLA_OS_WIN
  229. try {
  230. ::SetEnvironmentVariableA(key, value);
  231. } CARLA_SAFE_EXCEPTION("carla_setenv");
  232. #else
  233. ::setenv(key, value, 1);
  234. #endif
  235. }
  236. /*
  237. * Unset environment variable 'key'.
  238. */
  239. static inline
  240. void carla_unsetenv(const char* const key) noexcept
  241. {
  242. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  243. #ifdef CARLA_OS_WIN
  244. try {
  245. ::SetEnvironmentVariableA(key, nullptr);
  246. } CARLA_SAFE_EXCEPTION("carla_unsetenv");
  247. #else
  248. ::unsetenv(key);
  249. #endif
  250. }
  251. // --------------------------------------------------------------------------------------------------------------------
  252. // carla_strdup
  253. /*
  254. * Custom 'strdup' function.
  255. * Returned value is always valid, and must be freed with "delete[] var".
  256. * May throw.
  257. */
  258. static inline
  259. const char* carla_strdup(const char* const strBuf)
  260. {
  261. CARLA_SAFE_ASSERT(strBuf != nullptr);
  262. const std::size_t bufferLen = (strBuf != nullptr) ? std::strlen(strBuf) : 0;
  263. char* const buffer = new char[bufferLen+1];
  264. if (strBuf != nullptr && bufferLen > 0)
  265. std::memcpy(buffer, strBuf, bufferLen);
  266. buffer[bufferLen] = '\0';
  267. return buffer;
  268. }
  269. /*
  270. * Custom 'strdup' function.
  271. * Calls "std::free(strBuf)".
  272. * Returned value is always valid, and must be freed with "delete[] var".
  273. * May throw.
  274. */
  275. static inline
  276. const char* carla_strdup_free(char* const strBuf)
  277. {
  278. const char* const buffer(carla_strdup(strBuf));
  279. std::free(strBuf);
  280. return buffer;
  281. }
  282. /*
  283. * Custom 'strdup' function, safe version.
  284. * Returned value may be null.
  285. */
  286. static inline
  287. const char* carla_strdup_safe(const char* const strBuf) noexcept
  288. {
  289. CARLA_SAFE_ASSERT(strBuf != nullptr);
  290. const std::size_t bufferLen = (strBuf != nullptr) ? std::strlen(strBuf) : 0;
  291. char* buffer;
  292. try {
  293. buffer = new char[bufferLen+1];
  294. } CARLA_SAFE_EXCEPTION_RETURN("carla_strdup_safe", nullptr);
  295. if (strBuf != nullptr && bufferLen > 0)
  296. std::memcpy(buffer, strBuf, bufferLen);
  297. buffer[bufferLen] = '\0';
  298. return buffer;
  299. }
  300. // --------------------------------------------------------------------------------------------------------------------
  301. // memory functions
  302. /*
  303. * Add array values to another array.
  304. */
  305. template<typename T>
  306. static inline
  307. void carla_add(T dest[], const T src[], const std::size_t count) noexcept
  308. {
  309. CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
  310. CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
  311. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  312. for (std::size_t i=0; i<count; ++i)
  313. *dest++ += *src++;
  314. }
  315. /*
  316. * Copy array values to another array.
  317. */
  318. template<typename T>
  319. static inline
  320. void carla_copy(T dest[], const T src[], const std::size_t count) noexcept
  321. {
  322. CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
  323. CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
  324. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  325. std::memcpy(dest, src, count*sizeof(T));
  326. }
  327. /*
  328. * Fill an array with a fixed value.
  329. */
  330. template<typename T>
  331. static inline
  332. void carla_fill(T data[], const T& value, const std::size_t count) noexcept
  333. {
  334. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  335. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  336. if (value == 0)
  337. {
  338. std::memset(data, 0, count*sizeof(T));
  339. }
  340. else
  341. {
  342. for (std::size_t i=0; i<count; ++i)
  343. *data++ = value;
  344. }
  345. }
  346. /*
  347. * Clear a byte array.
  348. */
  349. static inline
  350. void carla_zeroBytes(uint8_t bytes[], const std::size_t count) noexcept
  351. {
  352. CARLA_SAFE_ASSERT_RETURN(bytes != nullptr,);
  353. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  354. std::memset(bytes, 0, count*sizeof(uint8_t));
  355. }
  356. /*
  357. * Clear a char array.
  358. */
  359. static inline
  360. void carla_zeroChars(char chars[], const std::size_t count) noexcept
  361. {
  362. CARLA_SAFE_ASSERT_RETURN(chars != nullptr,);
  363. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  364. std::memset(chars, 0, count*sizeof(char));
  365. }
  366. /*
  367. * Clear a pointer array.
  368. */
  369. template<typename T>
  370. static inline
  371. void carla_zeroPointers(T* ptrs[], const std::size_t count) noexcept
  372. {
  373. CARLA_SAFE_ASSERT_RETURN(ptrs != nullptr,);
  374. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  375. std::memset(ptrs, 0, count*sizeof(T*));
  376. }
  377. /*
  378. * Clear a single struct.
  379. */
  380. template <typename T>
  381. static inline
  382. void carla_zeroStruct(T& s) noexcept
  383. {
  384. std::memset(&s, 0, sizeof(T));
  385. }
  386. /*
  387. * Clear a struct array.
  388. */
  389. template <typename T>
  390. static inline
  391. void carla_zeroStructs(T structs[], const std::size_t count) noexcept
  392. {
  393. CARLA_SAFE_ASSERT_RETURN(structs != nullptr,);
  394. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  395. std::memset(structs, 0, count*sizeof(T));
  396. }
  397. /*
  398. * Copy a single struct.
  399. */
  400. template <typename T>
  401. static inline
  402. void carla_copyStruct(T& dest, const T& src) noexcept
  403. {
  404. std::memcpy(&dest, &src, sizeof(T));
  405. }
  406. /*
  407. * Copy a struct array.
  408. */
  409. template <typename T>
  410. static inline
  411. void carla_copyStructs(T dest[], const T src[], const std::size_t count) noexcept
  412. {
  413. CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
  414. CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
  415. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  416. std::memcpy(dest, src, count*sizeof(T));
  417. }
  418. // --------------------------------------------------------------------------------------------------------------------
  419. #endif // CARLA_UTILS_HPP_INCLUDED