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.

438 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. } CARLA_SAFE_EXCEPTION_RETURN("carla_strdup_safe", nullptr);
  264. if (strBuf != nullptr && bufferLen > 0)
  265. std::strncpy(buffer, strBuf, bufferLen);
  266. buffer[bufferLen] = '\0';
  267. return buffer;
  268. }
  269. // -----------------------------------------------------------------------
  270. // memory functions
  271. /*
  272. * Add array values to another array.
  273. */
  274. template<typename T>
  275. static inline
  276. void carla_add(T* dataDst, const T* dataSrc, const std::size_t size) noexcept
  277. {
  278. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  279. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  280. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  281. for (std::size_t i=0; i < size; ++i)
  282. *dataDst++ += *dataSrc++;
  283. }
  284. /*
  285. * Copy array values to another array.
  286. */
  287. template<typename T>
  288. static inline
  289. void carla_copy(T* const dataDst, const T* const dataSrc, const std::size_t size) noexcept
  290. {
  291. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  292. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  293. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  294. std::memcpy(dataDst, dataSrc, size*sizeof(T));
  295. }
  296. /*
  297. * Fill an array with a fixed value.
  298. */
  299. template<typename T>
  300. static inline
  301. void carla_fill(T* data, const T v, const std::size_t size) noexcept
  302. {
  303. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  304. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  305. if (v == 0)
  306. {
  307. std::memset(data, 0, size*sizeof(T));
  308. }
  309. else
  310. {
  311. for (std::size_t i=0; i < size; ++i)
  312. *data++ = v;
  313. }
  314. }
  315. /*
  316. * Clear a byte array.
  317. */
  318. static inline
  319. void carla_zeroBytes(void* const memory, const std::size_t numBytes) noexcept
  320. {
  321. CARLA_SAFE_ASSERT_RETURN(memory != nullptr,);
  322. CARLA_SAFE_ASSERT_RETURN(numBytes > 0,);
  323. std::memset(memory, 0, numBytes);
  324. }
  325. /*
  326. * Clear a char array.
  327. */
  328. static inline
  329. void carla_zeroChar(char* const data, const std::size_t numChars) noexcept
  330. {
  331. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  332. CARLA_SAFE_ASSERT_RETURN(numChars > 0,);
  333. std::memset(data, 0, numChars*sizeof(char));
  334. }
  335. /*
  336. * Clear a single struct/class.
  337. */
  338. template <typename T>
  339. static inline
  340. void carla_zeroStruct(T& structure) noexcept
  341. {
  342. std::memset(&structure, 0, sizeof(T));
  343. }
  344. /*
  345. * Clear an array of struct/classes.
  346. */
  347. template <typename T>
  348. static inline
  349. void carla_zeroStruct(T* const structure, const std::size_t count) noexcept
  350. {
  351. CARLA_SAFE_ASSERT_RETURN(structure != nullptr,);
  352. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  353. std::memset(structure, 0, count*sizeof(T));
  354. }
  355. /*
  356. * Copy a single struct/class.
  357. */
  358. template <typename T>
  359. static inline
  360. void carla_copyStruct(T& struct1, const T& struct2) noexcept
  361. {
  362. std::memcpy(&struct1, &struct2, sizeof(T));
  363. }
  364. /*
  365. * Copy an array of struct/classes.
  366. */
  367. template <typename T>
  368. static inline
  369. void carla_copyStruct(T* const struct1, const T* const struct2, const std::size_t count) noexcept
  370. {
  371. CARLA_SAFE_ASSERT_RETURN(struct1 != nullptr,);
  372. CARLA_SAFE_ASSERT_RETURN(struct2 != nullptr,);
  373. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  374. std::memcpy(struct1, struct2, count*sizeof(T));
  375. }
  376. // -----------------------------------------------------------------------
  377. #endif // CARLA_UTILS_HPP_INCLUDED