LMMS
Loading...
Searching...
No Matches
Memory.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017-2018 Filipe Coelho <falktx@falktx.com>
7
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
10
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
14
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 OF THIS SOFTWARE.
22
23 ==============================================================================
24*/
25
26#ifndef WATER_MEMORY_H_INCLUDED
27#define WATER_MEMORY_H_INCLUDED
28
29#include "../water.h"
30
31#include <cstring>
32
33namespace water {
34
35//==============================================================================
37inline void zeromem (void* memory, size_t numBytes) noexcept { std::memset (memory, 0, numBytes); }
38
40template <typename Type>
41inline void zerostruct (Type& structure) noexcept { std::memset (&structure, 0, sizeof (structure)); }
42
48template <typename Type>
49inline void deleteAndZero (Type& pointer) { delete pointer; pointer = nullptr; }
50
55template <typename Type, typename IntegerType>
56inline Type* addBytesToPointer (Type* basePointer, IntegerType bytes) noexcept { return (Type*) (((char*) basePointer) + bytes); }
57
61template <typename Type1, typename Type2>
62inline int getAddressDifference (Type1* pointer1, Type2* pointer2) noexcept { return (int) (((const char*) pointer1) - (const char*) pointer2); }
63
67template <class Type>
68inline Type* createCopyIfNotNull (const Type* objectToCopy) { return objectToCopy != nullptr ? new Type (*objectToCopy) : nullptr; }
69
70//==============================================================================
72template <typename Type>
73inline Type readUnaligned (const void* srcPtr) noexcept
74{
75 Type value;
76 std::memcpy (&value, srcPtr, sizeof (Type));
77
78 return value;
79}
80
82template <typename Type>
83inline void writeUnaligned (void* dstPtr, Type value) noexcept
84{
85 std::memcpy (dstPtr, &value, sizeof(Type));
86}
87
88}
89
90#endif // WATER_MEMORY_H_INCLUDED
static PuglViewHint int value
Definition pugl.h:1708
Definition AudioSampleBuffer.h:33
int getAddressDifference(Type1 *pointer1, Type2 *pointer2) noexcept
Definition Memory.h:62
Type readUnaligned(const void *srcPtr) noexcept
Definition Memory.h:73
void writeUnaligned(void *dstPtr, Type value) noexcept
Definition Memory.h:83
void deleteAndZero(Type &pointer)
Definition Memory.h:49
void zeromem(void *memory, size_t numBytes) noexcept
Definition Memory.h:37
void zerostruct(Type &structure) noexcept
Definition Memory.h:41
Type * addBytesToPointer(Type *basePointer, IntegerType bytes) noexcept
Definition Memory.h:56
Type * createCopyIfNotNull(const Type *objectToCopy)
Definition Memory.h:68