[winpr,interlocked] code cleanup

* properly compile with and without NONAMELESSUNION
* WINPR_ASSERT all arguments
This commit is contained in:
akallabeth 2024-10-30 10:59:54 +01:00
parent e37ec8e0fc
commit 38a2e28dec
No known key found for this signature in database
GPG Key ID: A49454A3FC909FD5

View File

@ -19,6 +19,7 @@
#include <winpr/config.h>
#include <winpr/assert.h>
#include <winpr/platform.h>
#include <winpr/synch.h>
#include <winpr/handle.h>
@ -32,11 +33,20 @@
#include <stdio.h>
#include <stdlib.h>
#if defined(NONAMELESSUNION)
#define GETSTRUCT(x) (x).s
#define GETSTRUCTPTR(x) (*(x)).s
#else
#define GETSTRUCT(x) (x)
#define GETSTRUCTPTR(x) (*(x))
#endif
VOID InitializeSListHead(WINPR_PSLIST_HEADER ListHead)
{
WINPR_ASSERT(ListHead);
#ifdef _WIN64
ListHead->s.Alignment = 0;
ListHead->s.Region = 0;
GETSTRUCTPTR(ListHead).Alignment = 0;
GETSTRUCTPTR(ListHead).Region = 0;
ListHead->Header8.Init = 1;
#else
ListHead->Alignment = 0;
@ -46,9 +56,11 @@ VOID InitializeSListHead(WINPR_PSLIST_HEADER ListHead)
WINPR_PSLIST_ENTRY InterlockedPushEntrySList(WINPR_PSLIST_HEADER ListHead,
WINPR_PSLIST_ENTRY ListEntry)
{
WINPR_SLIST_HEADER old;
WINPR_SLIST_HEADER newHeader;
WINPR_SLIST_HEADER old = { 0 };
WINPR_SLIST_HEADER newHeader = { 0 };
WINPR_ASSERT(ListHead);
WINPR_ASSERT(ListEntry);
#ifdef _WIN64
newHeader.HeaderX64.NextEntry = (((ULONG_PTR)ListEntry) >> 4);
@ -61,25 +73,25 @@ WINPR_PSLIST_ENTRY InterlockedPushEntrySList(WINPR_PSLIST_HEADER ListHead,
newHeader.HeaderX64.Depth = old.HeaderX64.Depth + 1;
newHeader.HeaderX64.Sequence = old.HeaderX64.Sequence + 1;
if (InterlockedCompareExchange64((LONGLONG*)ListHead, newHeader.s.Alignment,
old.s.Alignment))
if (InterlockedCompareExchange64((LONGLONG*)ListHead, GETSTRUCT(newHeader).Alignment,
GETSTRUCT(old).Alignment))
{
InterlockedCompareExchange64(&((LONGLONG*)ListHead)[1], newHeader.s.Region,
old.s.Region);
InterlockedCompareExchange64(&((LONGLONG*)ListHead)[1], GETSTRUCT(newHeader).Region,
GETSTRUCT(old).Region);
break;
}
}
return (PSLIST_ENTRY)((ULONG_PTR)old.HeaderX64.NextEntry << 4);
#else
newHeader.s.Next.Next = ListEntry;
GETSTRUCT(newHeader).Next.Next = ListEntry;
do
{
old = *ListHead;
ListEntry->Next = old.s.Next.Next;
newHeader.s.Depth = old.s.Depth + 1;
newHeader.s.Sequence = old.s.Sequence + 1;
ListEntry->Next = GETSTRUCT(old).Next.Next;
GETSTRUCT(newHeader).Depth = GETSTRUCT(old).Depth + 1;
GETSTRUCT(newHeader).Sequence = GETSTRUCT(old).Sequence + 1;
if (old.Alignment > INT64_MAX)
return NULL;
if (newHeader.Alignment > INT64_MAX)
@ -90,13 +102,17 @@ WINPR_PSLIST_ENTRY InterlockedPushEntrySList(WINPR_PSLIST_HEADER ListHead,
(LONGLONG)newHeader.Alignment,
(LONGLONG)old.Alignment) != (LONGLONG)old.Alignment);
return old.s.Next.Next;
return GETSTRUCT(old).Next.Next;
#endif
}
WINPR_PSLIST_ENTRY InterlockedPushListSListEx(WINPR_PSLIST_HEADER ListHead, WINPR_PSLIST_ENTRY List,
WINPR_PSLIST_ENTRY ListEnd, ULONG Count)
{
WINPR_ASSERT(ListHead);
WINPR_ASSERT(List);
WINPR_ASSERT(ListEnd);
#ifdef _WIN64
#else
@ -107,10 +123,12 @@ WINPR_PSLIST_ENTRY InterlockedPushListSListEx(WINPR_PSLIST_HEADER ListHead, WINP
WINPR_PSLIST_ENTRY InterlockedPopEntrySList(WINPR_PSLIST_HEADER ListHead)
{
WINPR_SLIST_HEADER old;
WINPR_SLIST_HEADER newHeader;
WINPR_SLIST_HEADER old = { 0 };
WINPR_SLIST_HEADER newHeader = { 0 };
WINPR_PSLIST_ENTRY entry = NULL;
WINPR_ASSERT(ListHead);
#ifdef _WIN64
while (1)
{
@ -125,11 +143,11 @@ WINPR_PSLIST_ENTRY InterlockedPopEntrySList(WINPR_PSLIST_HEADER ListHead)
newHeader.HeaderX64.Depth = old.HeaderX64.Depth - 1;
newHeader.HeaderX64.Sequence = old.HeaderX64.Sequence - 1;
if (InterlockedCompareExchange64((LONGLONG*)ListHead, newHeader.s.Alignment,
old.s.Alignment))
if (InterlockedCompareExchange64((LONGLONG*)ListHead, GETSTRUCT(newHeader).Alignment,
GETSTRUCT(old).Alignment))
{
InterlockedCompareExchange64(&((LONGLONG*)ListHead)[1], newHeader.s.Region,
old.s.Region);
InterlockedCompareExchange64(&((LONGLONG*)ListHead)[1], GETSTRUCT(newHeader).Region,
GETSTRUCT(old).Region);
break;
}
}
@ -138,14 +156,14 @@ WINPR_PSLIST_ENTRY InterlockedPopEntrySList(WINPR_PSLIST_HEADER ListHead)
{
old = *ListHead;
entry = old.s.Next.Next;
entry = GETSTRUCT(old).Next.Next;
if (!entry)
return NULL;
newHeader.s.Next.Next = entry->Next;
newHeader.s.Depth = old.s.Depth - 1;
newHeader.s.Sequence = old.s.Sequence + 1;
GETSTRUCT(newHeader).Next.Next = entry->Next;
GETSTRUCT(newHeader).Depth = GETSTRUCT(old).Depth - 1;
GETSTRUCT(newHeader).Sequence = GETSTRUCT(old).Sequence + 1;
if (old.Alignment > INT64_MAX)
return NULL;
@ -162,15 +180,16 @@ WINPR_PSLIST_ENTRY InterlockedPopEntrySList(WINPR_PSLIST_HEADER ListHead)
WINPR_PSLIST_ENTRY InterlockedFlushSList(WINPR_PSLIST_HEADER ListHead)
{
WINPR_SLIST_HEADER old;
WINPR_SLIST_HEADER newHeader;
WINPR_SLIST_HEADER old = { 0 };
WINPR_SLIST_HEADER newHeader = { 0 };
WINPR_ASSERT(ListHead);
if (!QueryDepthSList(ListHead))
return NULL;
#ifdef _WIN64
newHeader.s.Alignment = 0;
newHeader.s.Region = 0;
GETSTRUCT(newHeader).Alignment = 0;
GETSTRUCT(newHeader).Region = 0;
newHeader.HeaderX64.HeaderType = 1;
while (1)
@ -178,11 +197,11 @@ WINPR_PSLIST_ENTRY InterlockedFlushSList(WINPR_PSLIST_HEADER ListHead)
old = *ListHead;
newHeader.HeaderX64.Sequence = old.HeaderX64.Sequence + 1;
if (InterlockedCompareExchange64((LONGLONG*)ListHead, newHeader.s.Alignment,
old.s.Alignment))
if (InterlockedCompareExchange64((LONGLONG*)ListHead, GETSTRUCT(newHeader).Alignment,
GETSTRUCT(old).Alignment))
{
InterlockedCompareExchange64(&((LONGLONG*)ListHead)[1], newHeader.s.Region,
old.s.Region);
InterlockedCompareExchange64(&((LONGLONG*)ListHead)[1], GETSTRUCT(newHeader).Region,
GETSTRUCT(old).Region);
break;
}
}
@ -194,7 +213,7 @@ WINPR_PSLIST_ENTRY InterlockedFlushSList(WINPR_PSLIST_HEADER ListHead)
do
{
old = *ListHead;
newHeader.s.Sequence = old.s.Sequence + 1;
GETSTRUCT(newHeader).Sequence = GETSTRUCT(old).Sequence + 1;
if (old.Alignment > INT64_MAX)
return NULL;
@ -206,21 +225,25 @@ WINPR_PSLIST_ENTRY InterlockedFlushSList(WINPR_PSLIST_HEADER ListHead)
(LONGLONG)newHeader.Alignment,
(LONGLONG)old.Alignment) != (LONGLONG)old.Alignment);
return old.s.Next.Next;
return GETSTRUCT(old).Next.Next;
#endif
}
USHORT QueryDepthSList(WINPR_PSLIST_HEADER ListHead)
{
WINPR_ASSERT(ListHead);
#ifdef _WIN64
return ListHead->HeaderX64.Depth;
#else
return ListHead->s.Depth;
return GETSTRUCTPTR(ListHead).Depth;
#endif
}
LONG InterlockedIncrement(LONG volatile* Addend)
{
WINPR_ASSERT(Addend);
#if defined(__GNUC__) || defined(__clang__)
WINPR_PRAGMA_DIAG_PUSH
WINPR_PRAGMA_DIAG_IGNORED_ATOMIC_SEQ_CST
@ -233,6 +256,8 @@ LONG InterlockedIncrement(LONG volatile* Addend)
LONG InterlockedDecrement(LONG volatile* Addend)
{
WINPR_ASSERT(Addend);
#if defined(__GNUC__) || defined(__clang__)
WINPR_PRAGMA_DIAG_PUSH
WINPR_PRAGMA_DIAG_IGNORED_ATOMIC_SEQ_CST
@ -245,6 +270,8 @@ LONG InterlockedDecrement(LONG volatile* Addend)
LONG InterlockedExchange(LONG volatile* Target, LONG Value)
{
WINPR_ASSERT(Target);
#if defined(__GNUC__) || defined(__clang__)
WINPR_PRAGMA_DIAG_PUSH
WINPR_PRAGMA_DIAG_IGNORED_ATOMIC_SEQ_CST
@ -257,6 +284,8 @@ LONG InterlockedExchange(LONG volatile* Target, LONG Value)
LONG InterlockedExchangeAdd(LONG volatile* Addend, LONG Value)
{
WINPR_ASSERT(Addend);
#if defined(__GNUC__) || defined(__clang__)
WINPR_PRAGMA_DIAG_PUSH
WINPR_PRAGMA_DIAG_IGNORED_ATOMIC_SEQ_CST
@ -269,6 +298,8 @@ LONG InterlockedExchangeAdd(LONG volatile* Addend, LONG Value)
LONG InterlockedCompareExchange(LONG volatile* Destination, LONG Exchange, LONG Comperand)
{
WINPR_ASSERT(Destination);
#if defined(__GNUC__) || defined(__clang__)
WINPR_PRAGMA_DIAG_PUSH
WINPR_PRAGMA_DIAG_IGNORED_ATOMIC_SEQ_CST
@ -282,6 +313,8 @@ LONG InterlockedCompareExchange(LONG volatile* Destination, LONG Exchange, LONG
PVOID InterlockedCompareExchangePointer(PVOID volatile* Destination, PVOID Exchange,
PVOID Comperand)
{
WINPR_ASSERT(Destination);
#if defined(__GNUC__) || defined(__clang__)
WINPR_PRAGMA_DIAG_PUSH
WINPR_PRAGMA_DIAG_IGNORED_ATOMIC_SEQ_CST
@ -367,6 +400,8 @@ LONGLONG InterlockedCompareExchange64(LONGLONG volatile* Destination, LONGLONG E
LONGLONG InterlockedCompareExchange64(LONGLONG volatile* Destination, LONGLONG Exchange,
LONGLONG Comperand)
{
WINPR_ASSERT(Destination);
#if defined(__GNUC__) || defined(__clang__)
WINPR_PRAGMA_DIAG_PUSH
WINPR_PRAGMA_DIAG_IGNORED_ATOMIC_SEQ_CST
@ -391,21 +426,25 @@ LONGLONG InterlockedCompareExchange64(LONGLONG volatile* Destination, LONGLONG E
VOID InitializeListHead(WINPR_PLIST_ENTRY ListHead)
{
WINPR_ASSERT(ListHead);
ListHead->Flink = ListHead->Blink = ListHead;
}
BOOL IsListEmpty(const WINPR_LIST_ENTRY* ListHead)
{
WINPR_ASSERT(ListHead);
return (BOOL)(ListHead->Flink == ListHead);
}
BOOL RemoveEntryList(WINPR_PLIST_ENTRY Entry)
{
WINPR_PLIST_ENTRY OldFlink = NULL;
WINPR_PLIST_ENTRY OldBlink = NULL;
WINPR_ASSERT(Entry);
WINPR_PLIST_ENTRY OldFlink = Entry->Flink;
WINPR_ASSERT(OldFlink);
WINPR_PLIST_ENTRY OldBlink = Entry->Blink;
WINPR_ASSERT(OldBlink);
OldFlink = Entry->Flink;
OldBlink = Entry->Blink;
OldFlink->Blink = OldBlink;
OldBlink->Flink = OldFlink;
@ -414,9 +453,12 @@ BOOL RemoveEntryList(WINPR_PLIST_ENTRY Entry)
VOID InsertHeadList(WINPR_PLIST_ENTRY ListHead, WINPR_PLIST_ENTRY Entry)
{
WINPR_PLIST_ENTRY OldFlink = NULL;
WINPR_ASSERT(ListHead);
WINPR_ASSERT(Entry);
WINPR_PLIST_ENTRY OldFlink = ListHead->Flink;
WINPR_ASSERT(OldFlink);
OldFlink = ListHead->Flink;
Entry->Flink = OldFlink;
Entry->Blink = ListHead;
OldFlink->Blink = Entry;
@ -425,11 +467,14 @@ VOID InsertHeadList(WINPR_PLIST_ENTRY ListHead, WINPR_PLIST_ENTRY Entry)
WINPR_PLIST_ENTRY RemoveHeadList(WINPR_PLIST_ENTRY ListHead)
{
WINPR_PLIST_ENTRY Flink = NULL;
WINPR_PLIST_ENTRY Entry = NULL;
WINPR_ASSERT(ListHead);
WINPR_PLIST_ENTRY Entry = ListHead->Flink;
WINPR_ASSERT(Entry);
WINPR_PLIST_ENTRY Flink = Entry->Flink;
WINPR_ASSERT(Flink);
Entry = ListHead->Flink;
Flink = Entry->Flink;
ListHead->Flink = Flink;
Flink->Blink = ListHead;
@ -438,9 +483,12 @@ WINPR_PLIST_ENTRY RemoveHeadList(WINPR_PLIST_ENTRY ListHead)
VOID InsertTailList(WINPR_PLIST_ENTRY ListHead, WINPR_PLIST_ENTRY Entry)
{
WINPR_PLIST_ENTRY OldBlink = NULL;
WINPR_ASSERT(ListHead);
WINPR_ASSERT(Entry);
WINPR_PLIST_ENTRY OldBlink = ListHead->Blink;
WINPR_ASSERT(OldBlink);
OldBlink = ListHead->Blink;
Entry->Flink = ListHead;
Entry->Blink = OldBlink;
OldBlink->Flink = Entry;
@ -449,11 +497,14 @@ VOID InsertTailList(WINPR_PLIST_ENTRY ListHead, WINPR_PLIST_ENTRY Entry)
WINPR_PLIST_ENTRY RemoveTailList(WINPR_PLIST_ENTRY ListHead)
{
WINPR_PLIST_ENTRY Blink = NULL;
WINPR_PLIST_ENTRY Entry = NULL;
WINPR_ASSERT(ListHead);
WINPR_PLIST_ENTRY Entry = ListHead->Blink;
WINPR_ASSERT(Entry);
WINPR_PLIST_ENTRY Blink = Entry->Blink;
WINPR_ASSERT(Blink);
Entry = ListHead->Blink;
Blink = Entry->Blink;
ListHead->Blink = Blink;
Blink->Flink = ListHead;
@ -462,6 +513,9 @@ WINPR_PLIST_ENTRY RemoveTailList(WINPR_PLIST_ENTRY ListHead)
VOID AppendTailList(WINPR_PLIST_ENTRY ListHead, WINPR_PLIST_ENTRY ListToAppend)
{
WINPR_ASSERT(ListHead);
WINPR_ASSERT(ListToAppend);
WINPR_PLIST_ENTRY ListEnd = ListHead->Blink;
ListHead->Blink->Flink = ListToAppend;
@ -472,15 +526,17 @@ VOID AppendTailList(WINPR_PLIST_ENTRY ListHead, WINPR_PLIST_ENTRY ListToAppend)
VOID PushEntryList(WINPR_PSINGLE_LIST_ENTRY ListHead, WINPR_PSINGLE_LIST_ENTRY Entry)
{
WINPR_ASSERT(ListHead);
WINPR_ASSERT(Entry);
Entry->Next = ListHead->Next;
ListHead->Next = Entry;
}
WINPR_PSINGLE_LIST_ENTRY PopEntryList(WINPR_PSINGLE_LIST_ENTRY ListHead)
{
WINPR_PSINGLE_LIST_ENTRY FirstEntry = NULL;
FirstEntry = ListHead->Next;
WINPR_ASSERT(ListHead);
WINPR_PSINGLE_LIST_ENTRY FirstEntry = ListHead->Next;
if (FirstEntry != NULL)
ListHead->Next = FirstEntry->Next;