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.

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