Use a normal vector for effect sublists

This commit is contained in:
Chris Robinson 2018-11-18 22:45:55 -08:00
parent 81aed2ea01
commit 362979cefb
4 changed files with 45 additions and 53 deletions

View File

@ -2390,7 +2390,6 @@ ALCdevice_struct::ALCdevice_struct(DeviceType type)
{
almtx_init(&BufferLock, almtx_plain);
VECTOR_INIT(EffectList);
almtx_init(&EffectLock, almtx_plain);
VECTOR_INIT(FilterList);
@ -2423,10 +2422,11 @@ ALCdevice_struct::~ALCdevice_struct()
almtx_destroy(&BufferLock);
ReleaseALEffects(this);
#define FREE_EFFECTSUBLIST(x) al_free((x)->Effects)
VECTOR_FOR_EACH(EffectSubList, EffectList, FREE_EFFECTSUBLIST);
#undef FREE_EFFECTSUBLIST
VECTOR_DEINIT(EffectList);
std::for_each(EffectList.begin(), EffectList.end(),
[](EffectSubList &entry) noexcept -> void
{ al_free(entry.Effects); }
);
EffectList.clear();
almtx_destroy(&EffectLock);
ReleaseALFilters(this);

View File

@ -514,10 +514,9 @@ typedef struct BufferSubList {
} BufferSubList;
typedef struct EffectSubList {
ALuint64 FreeMask;
struct ALeffect *Effects; /* 64 */
ALuint64 FreeMask{0u};
struct ALeffect *Effects{nullptr}; /* 64 */
} EffectSubList;
TYPEDEF_VECTOR(EffectSubList, vector_EffectSubList)
typedef struct FilterSubList {
ALuint64 FreeMask;
@ -609,7 +608,7 @@ struct ALCdevice_struct {
almtx_t BufferLock;
// Map of Effects for this device
vector_EffectSubList EffectList{};
al::vector<EffectSubList> EffectList;
almtx_t EffectLock;
// Map of Filters for this device

View File

@ -87,16 +87,15 @@ static inline ALeffectslot *LookupEffectSlot(ALCcontext *context, ALuint id)
static inline ALeffect *LookupEffect(ALCdevice *device, ALuint id)
{
EffectSubList *sublist;
ALuint lidx = (id-1) >> 6;
ALsizei slidx = (id-1) & 0x3f;
if(UNLIKELY(lidx >= VECTOR_SIZE(device->EffectList)))
if(UNLIKELY(lidx >= device->EffectList.size()))
return nullptr;
sublist = &VECTOR_ELEM(device->EffectList, lidx);
if(UNLIKELY(sublist->FreeMask & (U64(1)<<slidx)))
EffectSubList &sublist = device->EffectList[lidx];
if(UNLIKELY(sublist.FreeMask & (U64(1)<<slidx)))
return nullptr;
return sublist->Effects + slidx;
return sublist.Effects + slidx;
}

View File

@ -24,6 +24,8 @@
#include <math.h>
#include <float.h>
#include <algorithm>
#include "AL/al.h"
#include "AL/alc.h"
@ -58,16 +60,15 @@ static void InitEffectParams(ALeffect *effect, ALenum type);
static inline ALeffect *LookupEffect(ALCdevice *device, ALuint id)
{
EffectSubList *sublist;
ALuint lidx = (id-1) >> 6;
ALsizei slidx = (id-1) & 0x3f;
if(UNLIKELY(lidx >= VECTOR_SIZE(device->EffectList)))
return NULL;
sublist = &VECTOR_ELEM(device->EffectList, lidx);
if(UNLIKELY(sublist->FreeMask & (U64(1)<<slidx)))
return NULL;
return sublist->Effects + slidx;
if(UNLIKELY(lidx >= device->EffectList.size()))
return nullptr;
EffectSubList &sublist = device->EffectList[lidx];
if(UNLIKELY(sublist.FreeMask & (U64(1)<<slidx)))
return nullptr;
return sublist.Effects + slidx;
}
@ -374,44 +375,39 @@ void InitEffect(ALeffect *effect)
static ALeffect *AllocEffect(ALCcontext *context)
{
ALCdevice *device = context->Device;
EffectSubList *sublist, *subend;
ALeffect *effect = NULL;
ALsizei lidx = 0;
ALsizei slidx;
almtx_lock(&device->EffectLock);
sublist = VECTOR_BEGIN(device->EffectList);
subend = VECTOR_END(device->EffectList);
for(;sublist != subend;++sublist)
auto sublist = std::find_if(device->EffectList.begin(), device->EffectList.end(),
[](const EffectSubList &entry) noexcept -> bool
{ return entry.FreeMask != 0; }
);
auto lidx = std::distance(device->EffectList.begin(), sublist);
ALeffect *effect{nullptr};
ALsizei slidx{0};
if(LIKELY(sublist != device->EffectList.end()))
{
if(sublist->FreeMask)
{
slidx = CTZ64(sublist->FreeMask);
effect = sublist->Effects + slidx;
break;
}
++lidx;
slidx = CTZ64(sublist->FreeMask);
effect = sublist->Effects + slidx;
}
if(UNLIKELY(!effect))
else
{
const EffectSubList empty_sublist = { 0, NULL };
/* Don't allocate so many list entries that the 32-bit ID could
* overflow...
*/
if(UNLIKELY(VECTOR_SIZE(device->EffectList) >= 1<<25))
if(UNLIKELY(device->EffectList.size() >= 1<<25))
{
almtx_unlock(&device->EffectLock);
alSetError(context, AL_OUT_OF_MEMORY, "Too many effects allocated");
return NULL;
}
lidx = (ALsizei)VECTOR_SIZE(device->EffectList);
VECTOR_PUSH_BACK(device->EffectList, empty_sublist);
sublist = &VECTOR_BACK(device->EffectList);
device->EffectList.emplace_back();
sublist = device->EffectList.end() - 1;
sublist->FreeMask = ~U64(0);
sublist->Effects = static_cast<ALeffect*>(al_calloc(16, sizeof(ALeffect)*64));
if(UNLIKELY(!sublist->Effects))
{
VECTOR_POP_BACK(device->EffectList);
device->EffectList.pop_back();
almtx_unlock(&device->EffectLock);
alSetError(context, AL_OUT_OF_MEMORY, "Failed to allocate effect batch");
return NULL;
@ -421,7 +417,7 @@ static ALeffect *AllocEffect(ALCcontext *context)
effect = sublist->Effects + slidx;
}
memset(effect, 0, sizeof(*effect));
effect = new (effect) ALeffect{};
InitEffectParams(effect, AL_EFFECT_NULL);
/* Add 1 to avoid effect ID 0. */
@ -439,30 +435,28 @@ static void FreeEffect(ALCdevice *device, ALeffect *effect)
ALsizei lidx = id >> 6;
ALsizei slidx = id & 0x3f;
memset(effect, 0, sizeof(*effect));
effect->~ALeffect();
VECTOR_ELEM(device->EffectList, lidx).FreeMask |= U64(1) << slidx;
device->EffectList[lidx].FreeMask |= U64(1) << slidx;
}
void ReleaseALEffects(ALCdevice *device)
{
EffectSubList *sublist = VECTOR_BEGIN(device->EffectList);
EffectSubList *subend = VECTOR_END(device->EffectList);
size_t leftover = 0;
for(;sublist != subend;++sublist)
for(auto &sublist : device->EffectList)
{
ALuint64 usemask = ~sublist->FreeMask;
ALuint64 usemask = ~sublist.FreeMask;
while(usemask)
{
ALsizei idx = CTZ64(usemask);
ALeffect *effect = sublist->Effects + idx;
ALeffect *effect = sublist.Effects + idx;
memset(effect, 0, sizeof(*effect));
effect->~ALeffect();
++leftover;
usemask &= ~(U64(1) << idx);
}
sublist->FreeMask = ~usemask;
sublist.FreeMask = ~usemask;
}
if(leftover > 0)
WARN("(%p) Deleted " SZFMT " Effect%s\n", device, leftover, (leftover==1)?"":"s");