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.

621 lines
15KB

  1. /*
  2. * Carla common utils
  3. * Copyright (C) 2011-2020 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 <cxxabi.h>
  27. # include <cstdint>
  28. #else
  29. # include <stdint.h>
  30. #endif
  31. #ifdef CARLA_OS_WIN
  32. # define WIN32_LEAN_AND_MEAN 1
  33. # include <winsock2.h>
  34. # include <windows.h>
  35. #else
  36. # include <unistd.h>
  37. #endif
  38. // --------------------------------------------------------------------------------------------------------------------
  39. // misc functions
  40. /*
  41. * Return "true" or "false" according to yesNo.
  42. */
  43. static inline
  44. const char* bool2str(const bool yesNo) noexcept
  45. {
  46. return yesNo ? "true" : "false";
  47. }
  48. /*
  49. * Set a string as empty/null.
  50. */
  51. static inline
  52. void nullStrBuf(char* const strBuf) noexcept
  53. {
  54. strBuf[0] = '\0';
  55. }
  56. /*
  57. * Dummy function.
  58. */
  59. static inline
  60. void pass() noexcept {}
  61. // --------------------------------------------------------------------------------------------------------------------
  62. // string print functions
  63. /*
  64. * Internal noexcept-safe fopen function.
  65. */
  66. static inline
  67. FILE* __carla_fopen(const char* const filename, FILE* const fallback) noexcept
  68. {
  69. #ifdef CARLA_OS_LINUX
  70. if (std::getenv("CARLA_CAPTURE_CONSOLE_OUTPUT") == nullptr)
  71. return fallback;
  72. FILE* ret = nullptr;
  73. try {
  74. ret = std::fopen(filename, "a+");
  75. } CARLA_CATCH_UNWIND catch (...) {}
  76. if (ret == nullptr)
  77. ret = fallback;
  78. return ret;
  79. #else
  80. return fallback;
  81. // unused
  82. (void)filename;
  83. #endif
  84. }
  85. /*
  86. * Print a string to stdout with newline (gray color).
  87. * Does nothing if DEBUG is not defined.
  88. */
  89. #ifndef DEBUG
  90. # define carla_debug(...)
  91. #else
  92. static inline
  93. void carla_debug(const char* const fmt, ...) noexcept
  94. {
  95. static FILE* const output = __carla_fopen("/tmp/carla.debug.log", stdout);
  96. try {
  97. ::va_list args;
  98. ::va_start(args, fmt);
  99. if (output == stdout)
  100. {
  101. #ifdef CARLA_OS_MAC
  102. std::fprintf(output, "\x1b[37;1m[carla] ");
  103. #else
  104. std::fprintf(output, "\x1b[30;1m[carla] ");
  105. #endif
  106. std::vfprintf(output, fmt, args);
  107. std::fprintf(output, "\x1b[0m\n");
  108. }
  109. else
  110. {
  111. std::fprintf(output, "[carla] ");
  112. std::vfprintf(output, fmt, args);
  113. std::fprintf(output, "\n");
  114. }
  115. std::fflush(output);
  116. ::va_end(args);
  117. } CARLA_CATCH_UNWIND catch (...) {}
  118. }
  119. #endif
  120. /*
  121. * Print a string to stdout with newline.
  122. */
  123. static inline
  124. void carla_stdout(const char* const fmt, ...) noexcept
  125. {
  126. static FILE* const output = __carla_fopen("/tmp/carla.stdout.log", stdout);
  127. try {
  128. ::va_list args;
  129. ::va_start(args, fmt);
  130. std::fprintf(output, "[carla] ");
  131. std::vfprintf(output, fmt, args);
  132. std::fprintf(output, "\n");
  133. #ifndef DEBUG
  134. if (output != stdout)
  135. #endif
  136. std::fflush(output);
  137. ::va_end(args);
  138. } CARLA_CATCH_UNWIND catch (...) {}
  139. }
  140. /*
  141. * Print a string to stderr with newline.
  142. */
  143. static inline
  144. void carla_stderr(const char* const fmt, ...) noexcept
  145. {
  146. static FILE* const output = __carla_fopen("/tmp/carla.stderr.log", stderr);
  147. try {
  148. ::va_list args;
  149. ::va_start(args, fmt);
  150. std::fprintf(output, "[carla] ");
  151. std::vfprintf(output, fmt, args);
  152. std::fprintf(output, "\n");
  153. #ifndef DEBUG
  154. if (output != stderr)
  155. #endif
  156. std::fflush(output);
  157. ::va_end(args);
  158. } CARLA_CATCH_UNWIND catch (...) {}
  159. }
  160. /*
  161. * Print a string to stderr with newline (red color).
  162. */
  163. static inline
  164. void carla_stderr2(const char* const fmt, ...) noexcept
  165. {
  166. static FILE* const output = __carla_fopen("/tmp/carla.stderr2.log", stderr);
  167. try {
  168. ::va_list args;
  169. ::va_start(args, fmt);
  170. if (output == stderr)
  171. {
  172. std::fprintf(output, "\x1b[31m[carla] ");
  173. std::vfprintf(output, fmt, args);
  174. std::fprintf(output, "\x1b[0m\n");
  175. }
  176. else
  177. {
  178. std::fprintf(output, "[carla] ");
  179. std::vfprintf(output, fmt, args);
  180. std::fprintf(output, "\n");
  181. }
  182. std::fflush(output);
  183. ::va_end(args);
  184. } CARLA_CATCH_UNWIND catch (...) {}
  185. }
  186. // --------------------------------------------------------------------------------------------------------------------
  187. // carla_safe_assert*
  188. /*
  189. * Print a safe assertion error message.
  190. */
  191. static inline
  192. void carla_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
  193. {
  194. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  195. }
  196. /*
  197. * Print a safe assertion error message, with 1 extra signed integer value.
  198. */
  199. static inline
  200. void carla_safe_assert_int(const char* const assertion, const char* const file,
  201. const int line, const int value) noexcept
  202. {
  203. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
  204. }
  205. /*
  206. * Print a safe assertion error message, with 1 extra unsigned integer value.
  207. */
  208. static inline
  209. void carla_safe_assert_uint(const char* const assertion, const char* const file,
  210. const int line, const uint value) noexcept
  211. {
  212. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %u", assertion, file, line, value);
  213. }
  214. /*
  215. * Print a safe assertion error message, with 2 extra signed integer values.
  216. */
  217. static inline
  218. void carla_safe_assert_int2(const char* const assertion, const char* const file,
  219. const int line, const int v1, const int v2) noexcept
  220. {
  221. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion, file, line, v1, v2);
  222. }
  223. /*
  224. * Print a safe assertion error message, with 2 extra unsigned integer values.
  225. */
  226. static inline
  227. void carla_safe_assert_uint2(const char* const assertion, const char* const file,
  228. const int line, const uint v1, const uint v2) noexcept
  229. {
  230. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %u, v2 %u", assertion, file, line, v1, v2);
  231. }
  232. /*
  233. * Print a safe assertion error message, with a custom error message.
  234. */
  235. static inline
  236. void carla_custom_safe_assert(const char* const message,
  237. const char* const assertion, const char* const file, const int line) noexcept
  238. {
  239. carla_stderr2("Carla assertion failure: %s, condition \"%s\" in file %s, line %i", message, assertion, file, line);
  240. }
  241. // --------------------------------------------------------------------------------------------------------------------
  242. // carla_safe_exception*
  243. /*
  244. * Print a safe exception error message.
  245. */
  246. static inline
  247. void carla_safe_exception(const char* const exception, const char* const file, const int line) noexcept
  248. {
  249. carla_stderr2("Carla exception caught: \"%s\" in file %s, line %i", exception, file, line);
  250. }
  251. // --------------------------------------------------------------------------------------------------------------------
  252. // carla_*sleep
  253. /*
  254. * Sleep for 'secs' seconds.
  255. */
  256. static inline
  257. void carla_sleep(const uint secs) noexcept
  258. {
  259. CARLA_SAFE_ASSERT_RETURN(secs > 0,);
  260. try {
  261. #ifdef CARLA_OS_WIN
  262. ::Sleep(secs * 1000);
  263. #else
  264. ::sleep(secs);
  265. #endif
  266. } CARLA_SAFE_EXCEPTION("carla_sleep");
  267. }
  268. /*
  269. * Sleep for 'msecs' milliseconds.
  270. */
  271. static inline
  272. void carla_msleep(const uint msecs) noexcept
  273. {
  274. CARLA_SAFE_ASSERT_RETURN(msecs > 0,);
  275. try {
  276. #ifdef CARLA_OS_WIN
  277. ::Sleep(msecs);
  278. #else
  279. ::usleep(msecs * 1000);
  280. #endif
  281. } CARLA_SAFE_EXCEPTION("carla_msleep");
  282. }
  283. // --------------------------------------------------------------------------------------------------------------------
  284. // carla_setenv
  285. /*
  286. * Set environment variable 'key' to 'value'.
  287. */
  288. static inline
  289. void carla_setenv(const char* const key, const char* const value) noexcept
  290. {
  291. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  292. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  293. #ifdef CARLA_OS_WIN
  294. try {
  295. ::SetEnvironmentVariableA(key, value);
  296. } CARLA_SAFE_EXCEPTION("carla_setenv");
  297. #else
  298. ::setenv(key, value, 1);
  299. #endif
  300. }
  301. /*
  302. * Unset environment variable 'key'.
  303. */
  304. static inline
  305. void carla_unsetenv(const char* const key) noexcept
  306. {
  307. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  308. #ifdef CARLA_OS_WIN
  309. try {
  310. ::SetEnvironmentVariableA(key, nullptr);
  311. } CARLA_SAFE_EXCEPTION("carla_unsetenv");
  312. #else
  313. ::unsetenv(key);
  314. #endif
  315. }
  316. // --------------------------------------------------------------------------------------------------------------------
  317. // carla_strdup
  318. /*
  319. * Custom 'strdup' function.
  320. * Returned value is always valid, and must be freed with "delete[] var".
  321. * May throw.
  322. */
  323. static inline
  324. const char* carla_strdup(const char* const strBuf)
  325. {
  326. CARLA_SAFE_ASSERT(strBuf != nullptr);
  327. const std::size_t bufferLen = (strBuf != nullptr) ? std::strlen(strBuf) : 0;
  328. char* const buffer = new char[bufferLen+1];
  329. if (bufferLen > 0)
  330. std::memcpy(buffer, strBuf, bufferLen);
  331. buffer[bufferLen] = '\0';
  332. return buffer;
  333. }
  334. /*
  335. * Custom 'strdup' function.
  336. * Calls "std::free(strBuf)".
  337. * Returned value is always valid, and must be freed with "delete[] var".
  338. * May throw.
  339. */
  340. static inline
  341. const char* carla_strdup_free(char* const strBuf)
  342. {
  343. const char* const buffer(carla_strdup(strBuf));
  344. std::free(strBuf);
  345. return buffer;
  346. }
  347. /*
  348. * Custom 'strdup' function, safe version.
  349. * Returned value may be null. It must be freed with "delete[] var".
  350. */
  351. static inline
  352. const char* carla_strdup_safe(const char* const strBuf) noexcept
  353. {
  354. CARLA_SAFE_ASSERT_RETURN(strBuf != nullptr, nullptr);
  355. const std::size_t bufferLen = std::strlen(strBuf);
  356. char* buffer;
  357. try {
  358. buffer = new char[bufferLen+1];
  359. } CARLA_SAFE_EXCEPTION_RETURN("carla_strdup_safe", nullptr);
  360. if (bufferLen > 0)
  361. std::memcpy(buffer, strBuf, bufferLen);
  362. buffer[bufferLen] = '\0';
  363. return buffer;
  364. }
  365. // --------------------------------------------------------------------------------------------------------------------
  366. // memory functions
  367. /*
  368. * Add array values to another array.
  369. */
  370. template<typename T>
  371. static inline
  372. void carla_add(T dest[], const T src[], const std::size_t count) noexcept
  373. {
  374. CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
  375. CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
  376. CARLA_SAFE_ASSERT_RETURN(dest != src,);
  377. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  378. for (std::size_t i=0; i<count; ++i)
  379. *dest++ += *src++;
  380. }
  381. /*
  382. * Add array values to another array, with a multiplication factor.
  383. */
  384. template<typename T>
  385. static inline
  386. void carla_addWithMultiply(T dest[], const T src[], const T& multiplier, const std::size_t count) noexcept
  387. {
  388. CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
  389. CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
  390. CARLA_SAFE_ASSERT_RETURN(dest != src,);
  391. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  392. for (std::size_t i=0; i<count; ++i)
  393. *dest++ += *src++ * multiplier;
  394. }
  395. /*
  396. * Copy array values to another array.
  397. */
  398. template<typename T>
  399. static inline
  400. void carla_copy(T dest[], const T src[], const std::size_t count) noexcept
  401. {
  402. CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
  403. CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
  404. CARLA_SAFE_ASSERT_RETURN(dest != src,);
  405. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  406. std::memcpy(dest, src, count*sizeof(T));
  407. }
  408. /*
  409. * Copy array values to another array, with a multiplication factor.
  410. */
  411. template<typename T>
  412. static inline
  413. void carla_copyWithMultiply(T dest[], const T src[], const T& multiplier, const std::size_t count) noexcept
  414. {
  415. CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
  416. CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
  417. CARLA_SAFE_ASSERT_RETURN(dest != src,);
  418. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  419. for (std::size_t i=0; i<count; ++i)
  420. *dest++ = *src++ * multiplier;
  421. }
  422. /*
  423. * Fill an array with a fixed value.
  424. */
  425. template<typename T>
  426. static inline
  427. void carla_fill(T data[], const T& value, const std::size_t count) noexcept
  428. {
  429. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  430. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  431. if (value == 0)
  432. {
  433. std::memset(data, 0, count*sizeof(T));
  434. }
  435. else
  436. {
  437. for (std::size_t i=0; i<count; ++i)
  438. *data++ = value;
  439. }
  440. }
  441. /*
  442. * Multiply an array with a fixed value.
  443. */
  444. template<typename T>
  445. static inline
  446. void carla_multiply(T data[], const T& multiplier, const std::size_t count) noexcept
  447. {
  448. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  449. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  450. if (multiplier == 0)
  451. {
  452. std::memset(data, 0, count*sizeof(T));
  453. }
  454. else
  455. {
  456. for (std::size_t i=0; i<count; ++i)
  457. *data++ *= multiplier;
  458. }
  459. }
  460. /*
  461. * Clear a byte array.
  462. */
  463. static inline
  464. void carla_zeroBytes(uint8_t bytes[], const std::size_t count) noexcept
  465. {
  466. CARLA_SAFE_ASSERT_RETURN(bytes != nullptr,);
  467. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  468. std::memset(bytes, 0, count*sizeof(uint8_t));
  469. }
  470. /*
  471. * Clear a char array.
  472. */
  473. static inline
  474. void carla_zeroChars(char chars[], const std::size_t count) noexcept
  475. {
  476. CARLA_SAFE_ASSERT_RETURN(chars != nullptr,);
  477. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  478. std::memset(chars, 0, count*sizeof(char));
  479. }
  480. /*
  481. * Clear a pointer array.
  482. */
  483. template<typename T>
  484. static inline
  485. void carla_zeroPointers(T* ptrs[], const std::size_t count) noexcept
  486. {
  487. CARLA_SAFE_ASSERT_RETURN(ptrs != nullptr,);
  488. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  489. std::memset(ptrs, 0, count*sizeof(T*));
  490. }
  491. /*
  492. * Clear a single struct.
  493. */
  494. template <typename T>
  495. static inline
  496. void carla_zeroStruct(T& s) noexcept
  497. {
  498. std::memset(&s, 0, sizeof(T));
  499. }
  500. /*
  501. * Clear a struct array.
  502. */
  503. template <typename T>
  504. static inline
  505. void carla_zeroStructs(T structs[], const std::size_t count) noexcept
  506. {
  507. CARLA_SAFE_ASSERT_RETURN(structs != nullptr,);
  508. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  509. std::memset(structs, 0, count*sizeof(T));
  510. }
  511. /*
  512. * Copy a single struct.
  513. */
  514. template <typename T>
  515. static inline
  516. void carla_copyStruct(T& dest, const T& src) noexcept
  517. {
  518. std::memcpy(&dest, &src, sizeof(T));
  519. }
  520. /*
  521. * Copy a struct array.
  522. */
  523. template <typename T>
  524. static inline
  525. void carla_copyStructs(T dest[], const T src[], const std::size_t count) noexcept
  526. {
  527. CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
  528. CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
  529. CARLA_SAFE_ASSERT_RETURN(dest != src,);
  530. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  531. std::memcpy(dest, src, count*sizeof(T));
  532. }
  533. // --------------------------------------------------------------------------------------------------------------------
  534. #endif // CARLA_UTILS_HPP_INCLUDED