Templatized linked list functions

git-svn-id: https://svn.eduke32.com/eduke32@7720 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2019-06-25 11:30:13 +00:00 committed by Christoph Oelckers
parent 00222e05f6
commit 1e0dd0e534
3 changed files with 69 additions and 119 deletions

View file

@ -1,104 +0,0 @@
/*
Copyright (C) 1994-1995 Apogee Software, Ltd.
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.
*/
#ifndef linklist_h_
#define linklist_h_
#ifdef __cplusplus
extern "C" {
#endif
#define NewNode(type) ((type *)Xmalloc(sizeof(type)))
#define LL_New(rootnode, type, next, prev) \
{ \
(rootnode) = NewNode(type); \
(rootnode)->prev = (rootnode); \
(rootnode)->next = (rootnode); \
}
#define LL_Add(rootnode, newnode, next, prev) \
{ \
(newnode)->next = (rootnode); \
(newnode)->prev = (rootnode)->prev; \
(rootnode)->prev->next = (newnode); \
(rootnode)->prev = (newnode); \
}
#define LL_TransferList(oldroot, newroot, next, prev) \
{ \
if ((oldroot)->prev != (oldroot)) \
{ \
(oldroot)->prev->next = (newroot); \
(oldroot)->next->prev = (newroot)->prev; \
(newroot)->prev->next = (oldroot)->next; \
(newroot)->prev = (oldroot)->prev; \
(oldroot)->next = (oldroot); \
(oldroot)->prev = (oldroot); \
} \
}
#define LL_ReverseList(root, type, next, prev) \
{ \
type *newend = (root)->next, *trav, *tprev; \
for (trav = (root)->prev; trav != newend; trav = tprev) \
{ \
tprev = trav->prev; \
LL_Move(trav, newend, next, prev); \
} \
}
#define LL_Remove(node, next, prev) \
{ \
(node)->prev->next = (node)->next; \
(node)->next->prev = (node)->prev; \
(node)->next = (node); \
(node)->prev = (node); \
}
#define LL_SortedInsertion(rootnode, insertnode, next, prev, type, sortparm) \
{ \
type *hoya = (rootnode)->next; \
while ((hoya != (rootnode)) && ((insertnode)->sortparm > hoya->sortparm)) \
{ \
hoya = hoya->next; \
} \
LL_Add(hoya, (insertnode), next, prev); \
}
#define LL_Move(node, newroot, next, prev) \
{ \
LL_Remove((node), next, prev); \
LL_Add((newroot), (node), next, prev); \
}
#define LL_Empty(list, next, prev) (((list)->next == (list)) && ((list)->prev == (list)))
#define LL_Free(list) Xfree(list)
#define LL_Reset(list, next, prev) (list)->next = (list)->prev = (list)
#ifdef __cplusplus
}
#endif
#endif

View file

@ -208,7 +208,7 @@ static bool MV_Mix(VoiceNode *voice, int const buffer)
void MV_PlayVoice(VoiceNode *voice)
{
DisableInterrupts();
LL_SortedInsertion(&VoiceList, voice, prev, next, VoiceNode, priority);
LL::SortedInsert(&VoiceList, voice, &VoiceNode::priority);
RestoreInterrupts();
}
@ -241,8 +241,7 @@ static void MV_StopVoice(VoiceNode *voice)
DisableInterrupts();
// move the voice from the play list to the free list
LL_Remove(voice, next, prev);
LL_Add((VoiceNode*) &VoicePool, voice, next, prev);
LL::Move(voice, &VoicePool);
RestoreInterrupts();
}
@ -324,9 +323,7 @@ static void MV_ServiceVoc(void)
if (!MV_Mix(voice, MV_MixPage))
{
MV_CleanupVoice(voice);
LL_Remove(voice, next, prev);
LL_Add((VoiceNode*) &VoicePool, voice, next, prev);
LL::Move(voice, &VoicePool);
}
}
while ((voice = next) != &VoiceList);
@ -454,7 +451,7 @@ VoiceNode *MV_AllocVoice(int32_t priority)
DisableInterrupts();
// Check if we have any free voices
if (LL_Empty(&VoicePool, next, prev))
if (LL::Empty(&VoicePool))
{
// check if we have a higher priority than a voice that is playing.
for (voice = node = VoiceList.next; node != &VoiceList; node = node->next)
@ -466,7 +463,7 @@ VoiceNode *MV_AllocVoice(int32_t priority)
if (priority >= voice->priority && voice != &VoiceList && voice->handle >= MV_MINVOICEHANDLE)
MV_Kill(voice->handle);
if (LL_Empty(&VoicePool, next, prev))
if (LL::Empty(&VoicePool))
{
// No free voices
RestoreInterrupts();
@ -475,7 +472,7 @@ VoiceNode *MV_AllocVoice(int32_t priority)
}
voice = VoicePool.next;
LL_Remove(voice, next, prev);
LL::Remove(voice);
RestoreInterrupts();
int32_t vhan = MV_MINVOICEHANDLE;
@ -495,7 +492,7 @@ VoiceNode *MV_AllocVoice(int32_t priority)
int32_t MV_VoiceAvailable(int32_t priority)
{
// Check if we have any free voices
if (!LL_Empty(&VoicePool, next, prev))
if (!LL::Empty(&VoicePool))
return TRUE;
DisableInterrupts();
@ -914,11 +911,11 @@ int32_t MV_Init(int32_t soundcard, int32_t MixRate, int32_t Voices, int32_t numc
MV_MaxVoices = Voices;
LL_Reset((VoiceNode*) &VoiceList, next, prev);
LL_Reset((VoiceNode*) &VoicePool, next, prev);
LL::Reset((VoiceNode*) &VoiceList);
LL::Reset((VoiceNode*) &VoicePool);
for (int index = 0; index < Voices; index++)
LL_Add((VoiceNode*) &VoicePool, &MV_Voices[ index ], next, prev);
LL::Insert(&VoicePool, &MV_Voices[index]);
MV_SetReverseStereo(FALSE);
@ -991,8 +988,8 @@ int32_t MV_Shutdown(void)
// Free any voices we allocated
ALIGNED_FREE_AND_NULL(MV_Voices);
LL_Reset((VoiceNode*) &VoiceList, next, prev);
LL_Reset((VoiceNode*) &VoicePool, next, prev);
LL::Reset((VoiceNode*) &VoiceList);
LL::Reset((VoiceNode*) &VoicePool);
MV_MaxVoices = 1;

View file

@ -0,0 +1,57 @@
#ifndef linklist_h_
#define linklist_h_
#include "compat.h"
namespace LL
{
template <typename T> FORCE_INLINE CONSTEXPR void Reset(T const node) { node->next = node->prev = node; }
template <typename T> FORCE_INLINE CONSTEXPR void Unlink(T const node)
{
node->prev->next = node->next;
node->next->prev = node->prev;
}
template <typename T> FORCE_INLINE CONSTEXPR void Insert(T const root, T const node)
{
node->next = root;
node->prev = root->prev;
root->prev->next = node;
root->prev = node;
}
template <typename T> FORCE_INLINE CONSTEXPR void Remove(T const node)
{
Unlink(node);
Reset(node);
}
template <typename T> FORCE_INLINE CONSTEXPR void Move(T const node, T const root)
{
Unlink(node);
Insert(root, node);
}
template <typename T, typename Tt> FORCE_INLINE CONSTEXPR void SortedInsert(T const root, T const node, Tt remove_pointer_t<T>::*m)
{
T best = root->next;
while ((best != root) && (node->*m > best->*m))
best = best->next;
Insert(best, node);
}
template <typename T> FORCE_INLINE CONSTEXPR bool Empty(T const root) { return ((root->next == root) && (root->prev == root)); }
#if 0
template <typename T> FORCE_INLINE void ReverseList(T const root)
{
T node = root->next;
for (T trav = root->prev, tprev; trav != node; trav = tprev)
{
tprev = trav->prev;
Move(trav, node);
}
}
#endif
} // namespace LL
#endif