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.

441 lines
10KB

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