/* ============================================================================== This file is part of the Water library. Copyright (c) 2016 ROLI Ltd. Copyright (C) 2017-2018 Filipe Coelho Permission is granted to use this software under the terms of the ISC license http://www.isc.org/downloads/software-support-policy/isc-license/ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ============================================================================== */ #ifndef WATER_MEMORY_H_INCLUDED #define WATER_MEMORY_H_INCLUDED #include "../water.h" #include namespace water { //============================================================================== /** Fills a block of memory with zeros. */ inline void zeromem (void* memory, size_t numBytes) noexcept { std::memset (memory, 0, numBytes); } /** Overwrites a structure or object with zeros. */ template inline void zerostruct (Type& structure) noexcept { std::memset (&structure, 0, sizeof (structure)); } /** Delete an object pointer, and sets the pointer to null. Remember that it's not good c++ practice to use delete directly - always try to use a ScopedPointer or other automatic lifetime-management system rather than resorting to deleting raw pointers! */ template inline void deleteAndZero (Type& pointer) { delete pointer; pointer = nullptr; } /** A handy function which adds a number of bytes to any type of pointer and returns the result. This can be useful to avoid casting pointers to a char* and back when you want to move them by a specific number of bytes, */ template inline Type* addBytesToPointer (Type* basePointer, IntegerType bytes) noexcept { return (Type*) (((char*) basePointer) + bytes); } /** A handy function which returns the difference between any two pointers, in bytes. The address of the second pointer is subtracted from the first, and the difference in bytes is returned. */ template inline int getAddressDifference (Type1* pointer1, Type2* pointer2) noexcept { return (int) (((const char*) pointer1) - (const char*) pointer2); } /** If a pointer is non-null, this returns a new copy of the object that it points to, or safely returns nullptr if the pointer is null. */ template inline Type* createCopyIfNotNull (const Type* objectToCopy) { return objectToCopy != nullptr ? new Type (*objectToCopy) : nullptr; } //============================================================================== /** A handy function to read un-aligned memory without a performance penalty or bus-error. */ template inline Type readUnaligned (const void* srcPtr) noexcept { Type value; std::memcpy (&value, srcPtr, sizeof (Type)); return value; } /** A handy function to write un-aligned memory without a performance penalty or bus-error. */ template inline void writeUnaligned (void* dstPtr, Type value) noexcept { std::memcpy (dstPtr, &value, sizeof(Type)); } } #endif // WATER_MEMORY_H_INCLUDED