From 1e0dd0e53479802a2bdfa3b5408ad91911aea773 Mon Sep 17 00:00:00 2001 From: terminx Date: Tue, 25 Jun 2019 11:30:13 +0000 Subject: [PATCH] Templatized linked list functions git-svn-id: https://svn.eduke32.com/eduke32@7720 1a8010ca-5511-0410-912e-c29ae57300e0 --- source/audiolib/src/linklist.h | 104 ------------------------------- source/audiolib/src/multivoc.cpp | 27 ++++---- source/build/include/linklist.h | 57 +++++++++++++++++ 3 files changed, 69 insertions(+), 119 deletions(-) delete mode 100644 source/audiolib/src/linklist.h create mode 100644 source/build/include/linklist.h diff --git a/source/audiolib/src/linklist.h b/source/audiolib/src/linklist.h deleted file mode 100644 index d6bc5c7cc..000000000 --- a/source/audiolib/src/linklist.h +++ /dev/null @@ -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 diff --git a/source/audiolib/src/multivoc.cpp b/source/audiolib/src/multivoc.cpp index 76f723087..ec46dfabe 100644 --- a/source/audiolib/src/multivoc.cpp +++ b/source/audiolib/src/multivoc.cpp @@ -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; diff --git a/source/build/include/linklist.h b/source/build/include/linklist.h new file mode 100644 index 000000000..2cbb7537b --- /dev/null +++ b/source/build/include/linklist.h @@ -0,0 +1,57 @@ +#ifndef linklist_h_ +#define linklist_h_ + +#include "compat.h" + +namespace LL +{ +template FORCE_INLINE CONSTEXPR void Reset(T const node) { node->next = node->prev = node; } +template FORCE_INLINE CONSTEXPR void Unlink(T const node) +{ + node->prev->next = node->next; + node->next->prev = node->prev; +} + +template 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 FORCE_INLINE CONSTEXPR void Remove(T const node) +{ + Unlink(node); + Reset(node); +} + +template FORCE_INLINE CONSTEXPR void Move(T const node, T const root) +{ + Unlink(node); + Insert(root, node); +} + +template FORCE_INLINE CONSTEXPR void SortedInsert(T const root, T const node, Tt remove_pointer_t::*m) +{ + T best = root->next; + while ((best != root) && (node->*m > best->*m)) + best = best->next; + Insert(best, node); +} + +template FORCE_INLINE CONSTEXPR bool Empty(T const root) { return ((root->next == root) && (root->prev == root)); } + +#if 0 +template 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