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.

91 lines
3.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2018 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_MEMORY_H_INCLUDED
  21. #define WATER_MEMORY_H_INCLUDED
  22. #include "../water.h"
  23. #include <cstring>
  24. namespace water {
  25. //==============================================================================
  26. /** Fills a block of memory with zeros. */
  27. inline void zeromem (void* memory, size_t numBytes) noexcept { std::memset (memory, 0, numBytes); }
  28. /** Overwrites a structure or object with zeros. */
  29. template <typename Type>
  30. inline void zerostruct (Type& structure) noexcept { std::memset (&structure, 0, sizeof (structure)); }
  31. /** Delete an object pointer, and sets the pointer to null.
  32. Remember that it's not good c++ practice to use delete directly - always try to use a ScopedPointer
  33. or other automatic lifetime-management system rather than resorting to deleting raw pointers!
  34. */
  35. template <typename Type>
  36. inline void deleteAndZero (Type& pointer) { delete pointer; pointer = nullptr; }
  37. /** A handy function which adds a number of bytes to any type of pointer and returns the result.
  38. This can be useful to avoid casting pointers to a char* and back when you want to move them by
  39. a specific number of bytes,
  40. */
  41. template <typename Type, typename IntegerType>
  42. inline Type* addBytesToPointer (Type* basePointer, IntegerType bytes) noexcept { return (Type*) (((char*) basePointer) + bytes); }
  43. /** A handy function which returns the difference between any two pointers, in bytes.
  44. The address of the second pointer is subtracted from the first, and the difference in bytes is returned.
  45. */
  46. template <typename Type1, typename Type2>
  47. inline int getAddressDifference (Type1* pointer1, Type2* pointer2) noexcept { return (int) (((const char*) pointer1) - (const char*) pointer2); }
  48. /** If a pointer is non-null, this returns a new copy of the object that it points to, or safely returns
  49. nullptr if the pointer is null.
  50. */
  51. template <class Type>
  52. inline Type* createCopyIfNotNull (const Type* objectToCopy) { return objectToCopy != nullptr ? new Type (*objectToCopy) : nullptr; }
  53. //==============================================================================
  54. /** A handy function to read un-aligned memory without a performance penalty or bus-error. */
  55. template <typename Type>
  56. inline Type readUnaligned (const void* srcPtr) noexcept
  57. {
  58. Type value;
  59. std::memcpy (&value, srcPtr, sizeof (Type));
  60. return value;
  61. }
  62. /** A handy function to write un-aligned memory without a performance penalty or bus-error. */
  63. template <typename Type>
  64. inline void writeUnaligned (void* dstPtr, Type value) noexcept
  65. {
  66. std::memcpy (dstPtr, &value, sizeof(Type));
  67. }
  68. }
  69. #endif // WATER_MEMORY_H_INCLUDED