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.

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