Replace LocklessAllocator with new MemoryPool class
MemoryPool maintains a free list of pre-allocated entries stored in a libcds MPMC Queue. In contrast to LocklessAllocator, it's a lot faster, less (and less complex) code to maintain for us and it doesn't fail when it's full.
This commit is contained in:
2
src/3rdparty/CMakeLists.txt
vendored
2
src/3rdparty/CMakeLists.txt
vendored
@@ -10,3 +10,5 @@ ENDIF()
|
||||
|
||||
ADD_SUBDIRECTORY(rpmalloc)
|
||||
ADD_SUBDIRECTORY(weakjack)
|
||||
|
||||
ADD_SUBDIRECTORY(libcds)
|
||||
|
||||
1
src/3rdparty/libcds
vendored
Submodule
1
src/3rdparty/libcds
vendored
Submodule
Submodule src/3rdparty/libcds added at e5bba766b9
@@ -168,6 +168,7 @@ SET(LMMS_REQUIRED_LIBS ${LMMS_REQUIRED_LIBS}
|
||||
${SNDFILE_LIBRARIES}
|
||||
${EXTRA_LIBRARIES}
|
||||
rpmalloc
|
||||
cds-s
|
||||
)
|
||||
|
||||
# Expose required libs for tests binary
|
||||
|
||||
@@ -30,8 +30,8 @@ set(LMMS_SRCS
|
||||
core/LadspaControl.cpp
|
||||
core/LadspaManager.cpp
|
||||
core/LfoController.cpp
|
||||
core/LocklessAllocator.cpp
|
||||
core/Memory.cpp
|
||||
core/MemoryPool.cpp
|
||||
core/MeterModel.cpp
|
||||
core/MicroTimer.cpp
|
||||
core/Mixer.cpp
|
||||
@@ -99,5 +99,7 @@ set(LMMS_SRCS
|
||||
core/midi/MidiTime.cpp
|
||||
core/midi/MidiWinMM.cpp
|
||||
|
||||
core/libcds.cpp
|
||||
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
@@ -1,164 +0,0 @@
|
||||
/*
|
||||
* LocklessAllocator.cpp - allocator with lockless alloc and free
|
||||
*
|
||||
* Copyright (c) 2016 Javier Serrano Polo <javier@jasp.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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "LocklessAllocator.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lmmsconfig.h"
|
||||
|
||||
#ifndef LMMS_BUILD_WIN32
|
||||
#include <strings.h>
|
||||
#endif
|
||||
|
||||
static const size_t SIZEOF_SET = sizeof( int ) * 8;
|
||||
|
||||
|
||||
static size_t align( size_t size, size_t alignment )
|
||||
{
|
||||
size_t misalignment = size % alignment;
|
||||
if( misalignment )
|
||||
{
|
||||
size += alignment - misalignment;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
LocklessAllocator::LocklessAllocator( size_t nmemb, size_t size )
|
||||
{
|
||||
m_capacity = align( nmemb, SIZEOF_SET );
|
||||
m_elementSize = align( size, sizeof( void * ) );
|
||||
m_pool = new char[m_capacity * m_elementSize];
|
||||
|
||||
m_freeStateSets = m_capacity / SIZEOF_SET;
|
||||
m_freeState = new std::atomic_int[m_freeStateSets];
|
||||
std::fill(m_freeState, m_freeState + m_freeStateSets, 0);
|
||||
|
||||
m_available = m_capacity;
|
||||
m_startIndex = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
LocklessAllocator::~LocklessAllocator()
|
||||
{
|
||||
int available = m_available;
|
||||
if( available != m_capacity )
|
||||
{
|
||||
fprintf( stderr, "LocklessAllocator: "
|
||||
"Destroying with elements still allocated\n" );
|
||||
}
|
||||
|
||||
delete[] m_pool;
|
||||
delete[] m_freeState;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef LMMS_BUILD_WIN32
|
||||
static int ffs( int i )
|
||||
{
|
||||
if( !i )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
for( int j = 0;; )
|
||||
{
|
||||
if( i & 1 << j++ )
|
||||
{
|
||||
return j;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
void * LocklessAllocator::alloc()
|
||||
{
|
||||
// Some of these CAS loops could probably use relaxed atomics, as discussed
|
||||
// in http://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange.
|
||||
// Let's use sequentially-consistent ops to be safe for now.
|
||||
int available = m_available.load();
|
||||
do
|
||||
{
|
||||
if( !available )
|
||||
{
|
||||
fprintf( stderr, "LocklessAllocator: No free space\n" );
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
while (!m_available.compare_exchange_weak(available, available - 1));
|
||||
|
||||
const size_t startIndex = m_startIndex++ % m_freeStateSets;
|
||||
for (size_t set = startIndex;; set = ( set + 1 ) % m_freeStateSets)
|
||||
{
|
||||
for (int freeState = m_freeState[set]; freeState != -1;)
|
||||
{
|
||||
int bit = ffs( ~freeState ) - 1;
|
||||
if (m_freeState[set].compare_exchange_weak(freeState,
|
||||
freeState | 1 << bit ) )
|
||||
{
|
||||
return m_pool + ( SIZEOF_SET * set + bit )
|
||||
* m_elementSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void LocklessAllocator::free( void * ptr )
|
||||
{
|
||||
ptrdiff_t diff = (char *)ptr - m_pool;
|
||||
if( diff < 0 || diff % m_elementSize )
|
||||
{
|
||||
invalid:
|
||||
fprintf( stderr, "LocklessAllocator: Invalid pointer\n" );
|
||||
return;
|
||||
}
|
||||
size_t offset = diff / m_elementSize;
|
||||
if( offset >= m_capacity )
|
||||
{
|
||||
goto invalid;
|
||||
}
|
||||
size_t set = offset / SIZEOF_SET;
|
||||
int bit = offset % SIZEOF_SET;
|
||||
int mask = 1 << bit;
|
||||
int prevState = m_freeState[set].fetch_and(~mask);
|
||||
if ( !( prevState & mask ) )
|
||||
{
|
||||
fprintf( stderr, "LocklessAllocator: Block not in use\n" );
|
||||
return;
|
||||
}
|
||||
++m_available;
|
||||
}
|
||||
121
src/core/MemoryPool.cpp
Normal file
121
src/core/MemoryPool.cpp
Normal file
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* MemoryPool.cpp
|
||||
*
|
||||
* Copyright (c) 2018 Lukas W <lukaswhl/at/gmail.com>
|
||||
*
|
||||
* This file is part of LMMS - https://lmms.io
|
||||
*
|
||||
* This file is licensed under the MIT license. See LICENSE.MIT.txt file in the
|
||||
* project root for details.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "MemoryPool.h"
|
||||
|
||||
#include <QtCore/QDebug>
|
||||
|
||||
#include <cds/container/vyukov_mpmc_cycle_queue.h>
|
||||
#include "libcds.h"
|
||||
|
||||
#include "Memory.h"
|
||||
|
||||
class _MemoryPool_Private : MmAllocator<char>
|
||||
{
|
||||
using Alloc = MmAllocator<char>;
|
||||
public:
|
||||
_MemoryPool_Private(size_t size, size_t nmemb)
|
||||
: m_freelist(nmemb)
|
||||
, m_elementSize(size)
|
||||
, m_numElms(nmemb)
|
||||
{
|
||||
m_buffer = new char[m_elementSize * m_numElms];
|
||||
for (size_t i = 0; i < m_numElms; i++) {
|
||||
m_freelist.push(m_buffer + (i * m_elementSize));
|
||||
}
|
||||
}
|
||||
|
||||
~_MemoryPool_Private()
|
||||
{
|
||||
char* ptr = nullptr;
|
||||
while (m_freelist.pop(ptr)) {
|
||||
if (! is_from_pool(ptr)) {
|
||||
Alloc::deallocate(ptr, m_elementSize);
|
||||
}
|
||||
}
|
||||
delete[] m_buffer;
|
||||
}
|
||||
|
||||
void * allocate()
|
||||
{
|
||||
void* ptr = allocate_bounded();
|
||||
if (ptr) {
|
||||
return ptr;
|
||||
} else {
|
||||
qWarning() << "MemoryPool exhausted";
|
||||
return Alloc::allocate(m_elementSize);
|
||||
}
|
||||
}
|
||||
|
||||
void * allocate_bounded()
|
||||
{
|
||||
char* ptr = nullptr;
|
||||
m_freelist.pop(ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void deallocate(void * ptr)
|
||||
{
|
||||
if (is_from_pool(ptr)) {
|
||||
bool pushed = m_freelist.push(reinterpret_cast<char*>(ptr));
|
||||
assert(pushed);
|
||||
} else {
|
||||
do_deallocate(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
void* do_allocate()
|
||||
{
|
||||
return Alloc::allocate(m_elementSize);
|
||||
}
|
||||
void do_deallocate(void* ptr)
|
||||
{
|
||||
Alloc::deallocate(reinterpret_cast<char*>(ptr), m_elementSize);
|
||||
}
|
||||
|
||||
bool is_from_pool(void* ptr)
|
||||
{
|
||||
auto buff = reinterpret_cast<uintptr_t>(m_buffer);
|
||||
auto p = reinterpret_cast<uintptr_t>(ptr);
|
||||
return p >= buff && p < (buff + (m_elementSize * m_numElms));
|
||||
}
|
||||
|
||||
const size_t m_elementSize;
|
||||
const size_t m_numElms;
|
||||
|
||||
char* m_buffer;
|
||||
cds::container::VyukovMPMCCycleQueue<char*> m_freelist;
|
||||
};
|
||||
|
||||
_MemoryPool_Base::_MemoryPool_Base( size_t size, size_t nmemb )
|
||||
: _imp(new _MemoryPool_Private(size, nmemb))
|
||||
{}
|
||||
|
||||
_MemoryPool_Base::~_MemoryPool_Base()
|
||||
{}
|
||||
|
||||
void * _MemoryPool_Base::allocate()
|
||||
{
|
||||
return _imp->allocate();
|
||||
}
|
||||
|
||||
void *_MemoryPool_Base::allocate_bounded()
|
||||
{
|
||||
return _imp->allocate_bounded();
|
||||
}
|
||||
|
||||
void _MemoryPool_Base::deallocate(void * ptr)
|
||||
{
|
||||
return _imp->deallocate(ptr);
|
||||
}
|
||||
|
||||
36
src/core/libcds.cpp
Normal file
36
src/core/libcds.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include "libcds.h"
|
||||
|
||||
#include <cds/init.h>
|
||||
#include <cds/gc/hp.h>
|
||||
|
||||
#include <memory>
|
||||
#include "stdshims.h"
|
||||
|
||||
namespace _cdslib
|
||||
{
|
||||
|
||||
static std::unique_ptr<cds::gc::HP> hpGC;
|
||||
|
||||
void init()
|
||||
{
|
||||
cds::Initialize();
|
||||
hpGC = make_unique<cds::gc::HP>();
|
||||
}
|
||||
|
||||
void deinit()
|
||||
{
|
||||
hpGC.reset();
|
||||
cds::Terminate();
|
||||
}
|
||||
|
||||
void thread_init()
|
||||
{
|
||||
cds::threading::Manager::attachThread();
|
||||
}
|
||||
|
||||
void thread_deinit()
|
||||
{
|
||||
cds::threading::Manager::detachThread();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user