Move aligned malloc to MemoryManager.h, port to stl allocator

This commit is contained in:
Lukas W
2017-11-30 12:39:15 +01:00
parent a863830795
commit 2a08909380
6 changed files with 69 additions and 114 deletions

View File

@@ -1,42 +0,0 @@
/*
* Copyright (c) 2014 Simon Symeonidis <lethaljellybean/at/gmail/com>
* Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* This file is part of LMMS - https://lmms.io
*
* 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 (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#ifndef MEMORY_HELPER_H
#define MEMORY_HELPER_H
/**
* Helper class to alocate aligned memory and free it.
*/
class MemoryHelper {
public:
static void* alignedMalloc( size_t );
static void alignedFree( void* );
private:
};
#endif

View File

@@ -2,8 +2,9 @@
* MemoryManager.h
*
* Copyright (c) 2017 Lukas W <lukaswhl/at/gmail.com>
* Copyright (c) 2014 Simon Symeonidis <lethaljellybean/at/gmail/com>
* Copyright (c) 2014 Vesa Kivimäki
* Copyright (c) 2007-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* This file is part of LMMS - https://lmms.io
*
@@ -62,8 +63,41 @@ struct MmAllocator
}
typedef std::vector<T, MmAllocator<T> > vector;
private:
MemoryManager::ThreadGuard m_threadGuard;
};
class _AlignedAllocator_Base
{
protected:
void* alloc_impl( size_t n, size_t alignment );
void dealloc_impl( void* p );
};
template<typename T>
class AlignedAllocator : _AlignedAllocator_Base
{
public:
typedef T value_type;
template<class U> struct rebind { typedef MmAllocator<U> other; };
AlignedAllocator( size_t alignment = 16 )
: alignment(alignment) {}
T* allocate( std::size_t n )
{
return reinterpret_cast<T*>( alloc_impl( sizeof(T) * n, alignment ) );
}
void deallocate( T* p, std::size_t )
{
dealloc_impl( p );
}
private:
std::size_t alignment;
};
#define MM_OPERATORS \
public: \