obs-StreamFX/source/util-memory.hpp

115 lines
2.7 KiB
C++
Raw Normal View History

2018-01-08 09:52:53 +00:00
/*
* Modern effects for a modern Streamer
* Copyright (C) 2017 Michael Fabian Dirks
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#pragma once
#include <cstdlib>
#ifdef _MSC_VER
#define aligned_alloc _aligned_malloc
#define aligned_free _aligned_free
#else
#define aligned_free free
#endif
2018-01-08 09:52:53 +00:00
namespace util {
2018-11-07 14:24:25 +00:00
inline size_t aligned_offset(size_t align, size_t pos)
{
return ((pos / align) + 1) * align;
}
2018-01-08 10:35:05 +00:00
void* malloc_aligned(size_t align, size_t size);
2018-11-07 14:24:25 +00:00
void free_aligned(void* mem);
2018-11-07 14:24:25 +00:00
template<typename T, size_t N = 16>
class AlignmentAllocator {
public:
2018-11-07 14:24:25 +00:00
typedef T value_type;
typedef size_t size_type;
typedef std::ptrdiff_t difference_type;
2018-11-07 14:24:25 +00:00
typedef T* pointer;
typedef const T* const_pointer;
2018-11-07 14:24:25 +00:00
typedef T& reference;
typedef const T& const_reference;
public:
2018-11-07 14:24:25 +00:00
inline AlignmentAllocator() throw() {}
2018-11-07 14:24:25 +00:00
template<typename T2>
inline AlignmentAllocator(const AlignmentAllocator<T2, N>&) throw()
{}
2018-11-07 14:24:25 +00:00
inline ~AlignmentAllocator() throw() {}
2018-11-07 14:24:25 +00:00
inline pointer adress(reference r)
{
return &r;
}
2018-11-07 14:24:25 +00:00
inline const_pointer adress(const_reference r) const
{
return &r;
}
2018-11-07 14:24:25 +00:00
inline pointer allocate(size_type n)
{
return (pointer)malloc_aligned(n * sizeof(value_type), N);
}
2018-11-07 14:24:25 +00:00
inline void deallocate(pointer p, size_type)
{
free_aligned(p);
}
2018-11-07 14:24:25 +00:00
inline void construct(pointer p, const value_type& wert)
{
new (p) value_type(wert);
}
2018-11-07 14:24:25 +00:00
inline void destroy(pointer p)
{
p->~value_type();
p;
}
2018-11-07 14:24:25 +00:00
inline size_type max_size() const throw()
{
return size_type(-1) / sizeof(value_type);
}
2018-11-07 14:24:25 +00:00
template<typename T2>
struct rebind {
typedef AlignmentAllocator<T2, N> other;
};
2018-11-07 14:24:25 +00:00
bool operator!=(const AlignmentAllocator<T, N>& other) const
{
return !(*this == other);
}
// Returns true if and only if storage allocated from *this
// can be deallocated from other, and vice versa.
// Always returns true for stateless allocators.
bool operator==(const AlignmentAllocator<T, N>& ) const
2018-11-07 14:24:25 +00:00
{
return true;
}
};
2018-11-07 14:24:25 +00:00
}; // namespace util