diff --git a/.clang-format b/.clang-format index 75347cd..63feee2 100644 --- a/.clang-format +++ b/.clang-format @@ -1,246 +1,5 @@ --- Language: Cpp -# BasedOnStyle: Microsoft -AccessModifierOffset: -2 -AlignAfterOpenBracket: Align -AlignArrayOfStructures: None -AlignConsecutiveAssignments: - Enabled: false - AcrossEmptyLines: false - AcrossComments: false - AlignCompound: false - AlignFunctionPointers: false - PadOperators: true -AlignConsecutiveBitFields: - Enabled: false - AcrossEmptyLines: false - AcrossComments: false - AlignCompound: false - AlignFunctionPointers: false - PadOperators: false -AlignConsecutiveDeclarations: - Enabled: false - AcrossEmptyLines: false - AcrossComments: false - AlignCompound: false - AlignFunctionPointers: false - PadOperators: false -AlignConsecutiveMacros: - Enabled: false - AcrossEmptyLines: false - AcrossComments: false - AlignCompound: false - AlignFunctionPointers: false - PadOperators: false -AlignConsecutiveShortCaseStatements: - Enabled: false - AcrossEmptyLines: false - AcrossComments: false - AlignCaseColons: false -AlignEscapedNewlines: Right -AlignOperands: Align -AlignTrailingComments: - Kind: Always - OverEmptyLines: 0 -AllowAllArgumentsOnNextLine: true -AllowAllParametersOfDeclarationOnNextLine: true -AllowBreakBeforeNoexceptSpecifier: Never -AllowShortBlocksOnASingleLine: Never -AllowShortCaseLabelsOnASingleLine: false -AllowShortCompoundRequirementOnASingleLine: true -AllowShortEnumsOnASingleLine: false -AllowShortFunctionsOnASingleLine: None -AllowShortIfStatementsOnASingleLine: Never -AllowShortLambdasOnASingleLine: All -AllowShortLoopsOnASingleLine: false -AlwaysBreakAfterDefinitionReturnType: None -AlwaysBreakAfterReturnType: None -AlwaysBreakBeforeMultilineStrings: false -AlwaysBreakTemplateDeclarations: MultiLine -AttributeMacros: - - __capability -BinPackArguments: true -BinPackParameters: true -BitFieldColonSpacing: Both -BraceWrapping: - AfterCaseLabel: false - AfterClass: true - AfterControlStatement: Always - AfterEnum: true - AfterExternBlock: true - AfterFunction: true - AfterNamespace: true - AfterObjCDeclaration: true - AfterStruct: true - AfterUnion: false - BeforeCatch: true - BeforeElse: true - BeforeLambdaBody: false - BeforeWhile: false - IndentBraces: false - SplitEmptyFunction: true - SplitEmptyRecord: true - SplitEmptyNamespace: true -BreakAdjacentStringLiterals: true -BreakAfterAttributes: Leave -BreakAfterJavaFieldAnnotations: false -BreakArrays: true -BreakBeforeBinaryOperators: None -BreakBeforeConceptDeclarations: Always -BreakBeforeBraces: Custom -BreakBeforeInlineASMColon: OnlyMultiline -BreakBeforeTernaryOperators: true -BreakConstructorInitializers: BeforeColon -BreakInheritanceList: BeforeColon -BreakStringLiterals: true -ColumnLimit: 120 -CommentPragmas: '^ IWYU pragma:' -CompactNamespaces: false -ConstructorInitializerIndentWidth: 4 -ContinuationIndentWidth: 4 -Cpp11BracedListStyle: true -DerivePointerAlignment: false -DisableFormat: false -EmptyLineAfterAccessModifier: Never -EmptyLineBeforeAccessModifier: LogicalBlock -ExperimentalAutoDetectBinPacking: false -FixNamespaceComments: true -ForEachMacros: - - foreach - - Q_FOREACH - - BOOST_FOREACH -IfMacros: - - KJ_IF_MAYBE -IncludeBlocks: Preserve -IncludeCategories: - - Regex: '^"(llvm|llvm-c|clang|clang-c)/' - Priority: 2 - SortPriority: 0 - CaseSensitive: false - - Regex: '^(<|"(gtest|gmock|isl|json)/)' - Priority: 3 - SortPriority: 0 - CaseSensitive: false - - Regex: '.*' - Priority: 1 - SortPriority: 0 - CaseSensitive: false -IncludeIsMainRegex: '(Test)?$' -IncludeIsMainSourceRegex: '' -IndentAccessModifiers: false -IndentCaseBlocks: false -IndentCaseLabels: false -IndentExternBlock: AfterExternBlock -IndentGotoLabels: true -IndentPPDirectives: None -IndentRequiresClause: true -IndentWidth: 4 -IndentWrappedFunctionNames: false -InsertBraces: false -InsertNewlineAtEOF: false -InsertTrailingCommas: None -IntegerLiteralSeparator: - Binary: 0 - BinaryMinDigits: 0 - Decimal: 0 - DecimalMinDigits: 0 - Hex: 0 - HexMinDigits: 0 -JavaScriptQuotes: Leave -JavaScriptWrapImports: true -KeepEmptyLinesAtTheStartOfBlocks: true -KeepEmptyLinesAtEOF: false -LambdaBodyIndentation: Signature -LineEnding: DeriveLF -MacroBlockBegin: '' -MacroBlockEnd: '' -MaxEmptyLinesToKeep: 1 -NamespaceIndentation: None -ObjCBinPackProtocolList: Auto -ObjCBlockIndentWidth: 2 -ObjCBreakBeforeNestedBlockParam: true -ObjCSpaceAfterProperty: false -ObjCSpaceBeforeProtocolList: true -PackConstructorInitializers: BinPack -PenaltyBreakAssignment: 2 -PenaltyBreakBeforeFirstCallParameter: 19 -PenaltyBreakComment: 300 -PenaltyBreakFirstLessLess: 120 -PenaltyBreakOpenParenthesis: 0 -PenaltyBreakScopeResolution: 500 -PenaltyBreakString: 1000 -PenaltyBreakTemplateDeclaration: 10 -PenaltyExcessCharacter: 1000000 -PenaltyIndentedWhitespace: 0 -PenaltyReturnTypeOnItsOwnLine: 1000 -PointerAlignment: Right -PPIndentWidth: -1 -QualifierAlignment: Leave -ReferenceAlignment: Pointer -ReflowComments: true -RemoveBracesLLVM: false -RemoveParentheses: Leave -RemoveSemicolon: false -RequiresClausePosition: OwnLine -RequiresExpressionIndentation: OuterScope -SeparateDefinitionBlocks: Leave -ShortNamespaceLines: 1 -SkipMacroDefinitionBody: false -SortIncludes: CaseSensitive -SortJavaStaticImport: Before -SortUsingDeclarations: LexicographicNumeric -SpaceAfterCStyleCast: false -SpaceAfterLogicalNot: false -SpaceAfterTemplateKeyword: true -SpaceAroundPointerQualifiers: Default -SpaceBeforeAssignmentOperators: true -SpaceBeforeCaseColon: false -SpaceBeforeCpp11BracedList: false -SpaceBeforeCtorInitializerColon: true -SpaceBeforeInheritanceColon: true -SpaceBeforeJsonColon: false -SpaceBeforeParens: ControlStatements -SpaceBeforeParensOptions: - AfterControlStatements: true - AfterForeachMacros: true - AfterFunctionDefinitionName: false - AfterFunctionDeclarationName: false - AfterIfMacros: true - AfterOverloadedOperator: false - AfterPlacementOperator: true - AfterRequiresInClause: false - AfterRequiresInExpression: false - BeforeNonEmptyParentheses: false -SpaceBeforeRangeBasedForLoopColon: true -SpaceBeforeSquareBrackets: false -SpaceInEmptyBlock: false -SpacesBeforeTrailingComments: 1 -SpacesInAngles: Never -SpacesInContainerLiterals: true -SpacesInLineCommentPrefix: - Minimum: 1 - Maximum: -1 -SpacesInParens: Never -SpacesInParensOptions: - InCStyleCasts: false - InConditionalStatements: false - InEmptyParentheses: false - Other: false -SpacesInSquareBrackets: false -Standard: Latest -StatementAttributeLikeMacros: - - Q_EMIT -StatementMacros: - - Q_UNUSED - - QT_REQUIRE_VERSION -TabWidth: 4 -UseTab: Never -VerilogBreakBetweenInstancePorts: true -WhitespaceSensitiveMacros: - - BOOST_PP_STRINGIZE - - CF_SWIFT_NAME - - NS_SWIFT_NAME - - PP_STRINGIZE - - STRINGIZE +BasedOnStyle: Microsoft ... diff --git a/.gitignore b/.gitignore index 4958dc5..406fb17 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -# Created by https://www.toptal.com/developers/gitignore/api/c -# Edit at https://www.toptal.com/developers/gitignore?templates=c +compile_commands.json +.peru/ ### C ### # Prerequisites @@ -28,6 +28,7 @@ # Shared objects (inc. Windows DLLs) *.dll +*.sys *.so *.so.* *.dylib @@ -45,19 +46,3 @@ *.su *.idb *.pdb - -# Kernel Module Compile Results -*.mod* -*.cmd -.tmp_versions/ -modules.order -Module.symvers -Mkfile.old -dkms.conf - -# End of https://www.toptal.com/developers/gitignore/api/c - -*.sys -*.base -compile_commands.json -*.zip diff --git a/Makefile b/Makefile index f9b8f8a..e59c31e 100644 --- a/Makefile +++ b/Makefile @@ -1,86 +1,51 @@ -# -# by Frank Rysanek -# -# Based on the source code of ioperm.c by Marcel Telka -# - thanks a million :-) -# +CC = x86_64-w64-mingw32-gcc +RC = x86_64-w64-mingw32-windres -# See also my_names.h -# Note that after changing the driver name, you also have -# to rename $(DRVNAME).c manually in this directory :-) -DRVNAME = even +CFLAGS = -Wall -Wpedantic -DNDEBUG -I/usr/x86_64-w64-mingw32/sys-root/mingw/include/ddk -O0 -std=c2x +LDFLAGS = -Wl,--subsystem,native \ + -Wl,--image-base,0x10000 \ + -Wl,--file-alignment,0x1000 \ + -Wl,--section-alignment,0x1000 \ + -Wl,--entry,DriverEntry@8 \ + -mdll -nostartfiles -nostdlib \ + -s -INCLUDES = -I/usr/x86_64-w64-mingw32/sys-root/mingw/include/ddk -INCLUDES += +VERSION = $(shell git describe --long --tags --dirty) - -# We could in fact just add -DEVEN_DRIVER_NAME=\"$(DRVNAME)\" to CFLAGS, -# but we'd have to be careful to "make clean" after changing -# the driver name here in the makefile... -#CFLAGS = -Wall $(INCLUDES) -DEVEN_DRIVER_NAME=\"$(DRVNAME)\" -CFLAGS = -Wall -Wpedantic -DNDEBUG $(INCLUDES) - -ifneq ($(RELEASE),) -VERSION = $(shell git describe --tags --always --dirty) -CFLAGS += -DEVEN_VERSION=\"$(VERSION)\" +ifneq ($(VERSION),) + CFLAGS += -DEVEN_VERSION=\"$(VERSION)\" + RCFLAGS += -DEVEN_VERSION=\\\"$(VERSION)\\\" endif -# Kernel-mode libs: +# Kernel-mode libs: # libntoskrnl = basic kernel-mode environment # libhal = WRITE_PORT_UCHAR et al. #KRNLIBS = -lntoskrnl -lhal KRNLIBS = -lntoskrnl -CC = x86_64-w64-mingw32-gcc -DLLTOOL = x86_64-w64-mingw32-dlltool -STRIP = x86_64-w64-mingw32-strip -#STRIP = true +LDFLAGS += $(KRNLIBS) -all: $(DRVNAME).sys +.SUFFIXES: .sys -# Dependencies on header files: -$(DRVNAME).sys: even_names.h even_ioctl.h even_peb.h utlist.h +.PHONY: clean third_party -# This shall get appended to the built-in set of suffixes supported: -.SUFFIXES: .sys .exe -# Otherwise the custom inference rules below wouldn't ever kick in. +all: even.sys -# This is implicit, no need to define this explicitly: -#.c.o: -# $(CC) $(CFLAGS) -c -o $@ $< +even.o: even_names.h even_ioctl.h even_peb.h third_party/utlist.h -# This inference rule allows us to turn an .o into a .sys without -# much hassle, implicitly. -# The downside is, that you cannot potentially add further object files -# to get linked into the .sys driver (such as, some custom ASM routines). -# Oh wait, maybe you can... try adding your .o after the last $(CC) in the rule... -.o.sys: - $(CC) -Wl,--base-file,$*.base \ - -Wl,--entry,DriverEntry@8 \ - -nostartfiles -nostdlib \ - -o junk.tmp \ - $*.o \ - $(KRNLIBS) - -rm -f junk.tmp - $(DLLTOOL) --dllname $*.sys \ - --base-file $*.base --output-exp $*.exp - $(CC) -Wl,--subsystem,native \ - -Wl,--image-base,0x10000 \ - -Wl,--file-alignment,0x1000 \ - -Wl,--section-alignment,0x1000 \ - -Wl,--entry,DriverEntry@8 \ - -Wl,$*.exp \ - -mdll -nostartfiles -nostdlib \ - -o $*.sys \ - $*.o \ - $(KRNLIBS) - $(STRIP) $*.sys +%.res.o: %.rc + $(RC) -i $< -o $@ $(RCFLAGS) -JUNK = *.base *.exp *.o *~ junk.tmp +%.sys: %.o %.res.o + $(CC) $^ -o $@ $(LDFLAGS) clean: - rm -f $(JUNK) *.sys + $(RM) *.o *.sys -semiclean: - rm -f $(JUNK) +third_party/utlist.h: .peru/lastimports +third_party: .peru/lastimports + peru reup + +.peru/lastimports: peru.yaml + peru sync diff --git a/README.md b/README.md new file mode 100644 index 0000000..09932cd --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +even.sys +======== + +> Even is not odd. + +A stub for Htsysm7679. + +## Building + +This project uses the MinGW toolchain to build. + +## Notes + +This stub didn't implement Htsysm7679's signature checks. +It's not a 1:1 replacement, but good enough to get executables running. + +## License + +SPDX-License-Identifier: GPL-2.0-or-later + + even.sys - a stub for Htsysm7679 + Copyright (C) 2024 Zephyr Lykos + + 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. diff --git a/even.c b/even.c index e2bc49d..a825361 100644 --- a/even.c +++ b/even.c @@ -24,7 +24,7 @@ #include "even_names.h" #include "even_peb.h" -#include "utlist.h" +#include "third_party/utlist.h" /* * Htsysm7679 constatns @@ -99,7 +99,7 @@ static VOID __stdcall even_unload(IN PDRIVER_OBJECT DriverObject) ANSI_STRING SymbolicLinkNameA; UNICODE_STRING SymbolicLinkNameW; - DbgPrint("Even Htsysm7679: DriverUnload called\r\n"); // catch this using DBGVIEW from www.sysinternals.com + DbgPrint("Even Htsysm7679: DriverUnload called\r\n"); even_peb_t *el, *tmp; DL_FOREACH_SAFE(even_fake_peb, el, tmp) @@ -108,8 +108,6 @@ static VOID __stdcall even_unload(IN PDRIVER_OBJECT DriverObject) ExFreePool(el); } - /*RtlInitUnicodeString( &SymbolicLinkName, EVEN_DOSDEVICE_NAME );*/ - /* If I want to use normal string combining logic in my_names.h, I need to mess with ANSI vs. Unicode */ RtlInitString(&SymbolicLinkNameA, EVEN_DOSDEVICE_NAME); RtlAnsiStringToUnicodeString(&SymbolicLinkNameW, &SymbolicLinkNameA, TRUE); /* Init from an ANSI string, and do allocate target buffer */ @@ -422,7 +420,7 @@ NTSTATUS __stdcall DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRIN "GNU General Public License for more details." "\r\n"); - DbgPrint("Even Htsysm7679: DriverEntry Called\r\n"); // catch this using DBGVIEW from www.sysinternals.com + DbgPrint("Even Htsysm7679: DriverEntry Called\r\n"); /* support for service stopping */ DriverObject->DriverUnload = even_unload; diff --git a/even.rc b/even.rc new file mode 100644 index 0000000..d1aa9e2 --- /dev/null +++ b/even.rc @@ -0,0 +1,33 @@ +#include +#include "even_names.h" + +VS_VERSION_INFO VERSIONINFO + FILEVERSION 0,0,0,0 + PRODUCTVERSION 0,0,0,0 + FILEFLAGSMASK 0x3fL +#ifdef _DEBUG + FILEFLAGS 0x1L +#else + FILEFLAGS 0x0L +#endif + FILEOS VOS_UNKNOWN + FILETYPE VFT_DLL + FILESUBTYPE VFT2_UNKNOWN +{ + BLOCK "StringFileInfo" + { + BLOCK "040904b0" + { + VALUE "CompanyName", "Zephyr Lykos" + VALUE "FileDescription", "Even is not odd (Htsysm7679 stub)" +#ifdef EVEN_VERSION + VALUE "FileVersion", EVEN_VERSION + VALUE "ProductVersion", EVEN_VERSION +#endif + VALUE "InternalName", "odd.sys" + VALUE "LegalCopyright", "\xA9 2024 Zephyr Lykos" + VALUE "OriginalFilename", "even.sys" + VALUE "ProductName", "Even" + } + } +} diff --git a/even_ioctl.h b/even_ioctl.h index 35dc91f..97a6a4c 100644 --- a/even_ioctl.h +++ b/even_ioctl.h @@ -1,46 +1,50 @@ -#ifndef _EVEN_IOCTL_H_ -#define _EVEN_IOCTL_H_ 1 - -/* - METHOD_BUFFERED handles the copy_to_user/copy_from_user automagically for us, at the cost of some overhead - METHOD_OUT_DIRECT = no automagical copy, MDL set up for "Kernel_driver -> User_mode_app" direction - METHOD_IN_DIRECT = no automagical copy, MDL set up for "User_mode_app -> Kernel_driver" direction - METHOD_NEITHER = no automagical copy, no automagical MDL setup. Driver (ioctl code) must be guaranteed - to run in the context of the calling user-space thread and ask for the user->system - mapping itself, and its access to the buffer should be wrapped in a try/catch to handle - potential exceptions... - - That's right, DIRECT IN/OUT are from the driver's point of view, relative to the user-space app!!! - Both the MSDN and the Coreproject.com article say so, the CodeProject article with added emphasis. -*/ - -/* CTL Codes above 0x800 are free for your custom use - Note: using METHOD_BUFFERED = we're lazy and we don't care about a bit of overhead */ - -#define IOCTL_CHECK_1 CTL_CODE(0xaa01, 0xe10, METHOD_BUFFERED, FILE_ANY_ACCESS) -#define IOCTL_CHECK_2 CTL_CODE(0xaa01, 0xe30, METHOD_BUFFERED, FILE_ANY_ACCESS) -#define IOCTL_CHECK_3 CTL_CODE(0xaa01, 0xe31, METHOD_BUFFERED, FILE_ANY_ACCESS) -#define IOCTL_WRITE_PEB_1 CTL_CODE(0xaa01, 0xe20, METHOD_BUFFERED, FILE_ANY_ACCESS) -#define IOCTL_WRITE_PEB_2 CTL_CODE(0xaa01, 0xe21, METHOD_BUFFERED, FILE_ANY_ACCESS) -#define IOCTL_READ_PEB CTL_CODE(0xaa01, 0xe22, METHOD_BUFFERED, FILE_ANY_ACCESS) - -struct even_ioctl_in_data -{ - unsigned int offset; - unsigned int value; -}; - -struct even_ioctl_out_data -{ - unsigned int result; -}; - -#define USE_EVEN_IOCTL_OUT_DATA(x) \ - struct even_ioctl_out_data##x \ - { \ - unsigned int result; \ - unsigned char data[x * 4]; \ - }; - -#endif /* _EVEN_IOCTL_H_ */ -// vim: sw=4 et +/* + * even.sys - a stub for Htsysm7679 + * Copyright (C) 2024 Zephyr Lykos + * + * 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 _EVEN_IOCTL_H_ +#define _EVEN_IOCTL_H_ 1 + +#define IOCTL_CHECK_1 CTL_CODE(0xaa01, 0xe10, METHOD_BUFFERED, FILE_ANY_ACCESS) +#define IOCTL_CHECK_2 CTL_CODE(0xaa01, 0xe30, METHOD_BUFFERED, FILE_ANY_ACCESS) +#define IOCTL_CHECK_3 CTL_CODE(0xaa01, 0xe31, METHOD_BUFFERED, FILE_ANY_ACCESS) +#define IOCTL_WRITE_PEB_1 CTL_CODE(0xaa01, 0xe20, METHOD_BUFFERED, FILE_ANY_ACCESS) +#define IOCTL_WRITE_PEB_2 CTL_CODE(0xaa01, 0xe21, METHOD_BUFFERED, FILE_ANY_ACCESS) +#define IOCTL_READ_PEB CTL_CODE(0xaa01, 0xe22, METHOD_BUFFERED, FILE_ANY_ACCESS) + +struct even_ioctl_in_data +{ + unsigned int offset; + unsigned int value; +}; + +struct even_ioctl_out_data +{ + unsigned int result; +}; + +#define USE_EVEN_IOCTL_OUT_DATA(x) \ + struct even_ioctl_out_data##x \ + { \ + unsigned int result; \ + unsigned char data[x * 4]; \ + }; + +#endif /* _EVEN_IOCTL_H_ */ +// vim: sw=4 et diff --git a/even_names.h b/even_names.h index 1ab63da..588ecea 100644 --- a/even_names.h +++ b/even_names.h @@ -1,15 +1,31 @@ -#ifndef _EVEN_NAMES_H_ -#define _EVEN_NAMES_H_ 1 - -/* Remember: two "string literals" "will get concatenated into one" - if there's no punctuation between them: - "hello" "world" => "hello world" -*/ -#ifndef EVEN_DRIVER_NAME -#define EVEN_DRIVER_NAME "Htsysm7679" /* not used itself outside of this header file */ -#endif -/* for the driver */ -#define EVEN_DEVICE_NAME "\\Device\\" EVEN_DRIVER_NAME -#define EVEN_DOSDEVICE_NAME "\\DosDevices\\" EVEN_DRIVER_NAME /* AKA symlink name */ - -#endif /* _EVEN_NAMES_H_ */ +/* + * even.sys - a stub for Htsysm7679 + * Copyright (C) 2024 Zephyr Lykos + * + * 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 _EVEN_NAMES_H_ +#define _EVEN_NAMES_H_ 1 + +#ifndef EVEN_DRIVER_NAME +#define EVEN_DRIVER_NAME "Htsysm7679" +#endif + +#define EVEN_DEVICE_NAME "\\Device\\" EVEN_DRIVER_NAME +#define EVEN_DOSDEVICE_NAME "\\DosDevices\\" EVEN_DRIVER_NAME /* AKA symlink name */ + +#endif /* _EVEN_NAMES_H_ */ diff --git a/even_peb.h b/even_peb.h index dabb3b2..de1c53e 100644 --- a/even_peb.h +++ b/even_peb.h @@ -1,3 +1,23 @@ +/* + * even.sys - a stub for Htsysm7679 + * Copyright (C) 2024 Zephyr Lykos + * + * 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 _EVEN_PEB_H_ #define _EVEN_PEB_H_ 1 @@ -11,5 +31,5 @@ typedef struct even_peb struct even_peb *next, *prev; } even_peb_t; -#endif +#endif /* _EVEN_PEB_H_ */ // vim: sw=4 et diff --git a/peru.yaml b/peru.yaml new file mode 100644 index 0000000..fda4077 --- /dev/null +++ b/peru.yaml @@ -0,0 +1,8 @@ +imports: + utlist: third_party/ + +git module utlist: + url: https://github.com/troydhanson/uthash.git + rev: eeba1961f203869116a865e57c968e9c86e1b8c4 + export: src/ + pick: src/utlist.h diff --git a/third_party/utlist.h b/third_party/utlist.h new file mode 100644 index 0000000..08fc59a --- /dev/null +++ b/third_party/utlist.h @@ -0,0 +1,1076 @@ +/* +Copyright (c) 2007-2022, Troy D. Hanson https://troydhanson.github.io/uthash/ +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER +OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef UTLIST_H +#define UTLIST_H + +#define UTLIST_VERSION 2.3.0 + +#include + +/* + * This file contains macros to manipulate singly and doubly-linked lists. + * + * 1. LL_ macros: singly-linked lists. + * 2. DL_ macros: doubly-linked lists. + * 3. CDL_ macros: circular doubly-linked lists. + * + * To use singly-linked lists, your structure must have a "next" pointer. + * To use doubly-linked lists, your structure must "prev" and "next" pointers. + * Either way, the pointer to the head of the list must be initialized to NULL. + * + * ----------------.EXAMPLE ------------------------- + * struct item { + * int id; + * struct item *prev, *next; + * } + * + * struct item *list = NULL: + * + * int main() { + * struct item *item; + * ... allocate and populate item ... + * DL_APPEND(list, item); + * } + * -------------------------------------------------- + * + * For doubly-linked lists, the append and delete macros are O(1) + * For singly-linked lists, append and delete are O(n) but prepend is O(1) + * The sort macro is O(n log(n)) for all types of single/double/circular lists. + */ + +/* These macros use decltype or the earlier __typeof GNU extension. + As decltype is only available in newer compilers (VS2010 or gcc 4.3+ + when compiling c++ source) this code uses whatever method is needed + or, for VS2008 where neither is available, uses casting workarounds. */ +#if !defined(LDECLTYPE) && !defined(NO_DECLTYPE) +#if defined(_MSC_VER) /* MS compiler */ +#if _MSC_VER >= 1600 && defined(__cplusplus) /* VS2010 or newer in C++ mode */ +#define LDECLTYPE(x) decltype(x) +#else /* VS2008 or older (or VS2010 in C mode) */ +#define NO_DECLTYPE +#endif +#elif defined(__MCST__) /* Elbrus C Compiler */ +#define LDECLTYPE(x) __typeof(x) +#elif defined(__BORLANDC__) || defined(__ICCARM__) || defined(__LCC__) || defined(__WATCOMC__) +#define NO_DECLTYPE +#else /* GNU, Sun and other compilers */ +#define LDECLTYPE(x) __typeof(x) +#endif +#endif + +/* for VS2008 we use some workarounds to get around the lack of decltype, + * namely, we always reassign our tmp variable to the list head if we need + * to dereference its prev/next pointers, and save/restore the real head.*/ +#ifdef NO_DECLTYPE +#define IF_NO_DECLTYPE(x) x +#define LDECLTYPE(x) char* +#define UTLIST_SV(elt,list) _tmp = (char*)(list); {char **_alias = (char**)&(list); *_alias = (elt); } +#define UTLIST_NEXT(elt,list,next) ((char*)((list)->next)) +#define UTLIST_NEXTASGN(elt,list,to,next) { char **_alias = (char**)&((list)->next); *_alias=(char*)(to); } +/* #define UTLIST_PREV(elt,list,prev) ((char*)((list)->prev)) */ +#define UTLIST_PREVASGN(elt,list,to,prev) { char **_alias = (char**)&((list)->prev); *_alias=(char*)(to); } +#define UTLIST_RS(list) { char **_alias = (char**)&(list); *_alias=_tmp; } +#define UTLIST_CASTASGN(a,b) { char **_alias = (char**)&(a); *_alias=(char*)(b); } +#else +#define IF_NO_DECLTYPE(x) +#define UTLIST_SV(elt,list) +#define UTLIST_NEXT(elt,list,next) ((elt)->next) +#define UTLIST_NEXTASGN(elt,list,to,next) ((elt)->next)=(to) +/* #define UTLIST_PREV(elt,list,prev) ((elt)->prev) */ +#define UTLIST_PREVASGN(elt,list,to,prev) ((elt)->prev)=(to) +#define UTLIST_RS(list) +#define UTLIST_CASTASGN(a,b) (a)=(b) +#endif + +/****************************************************************************** + * The sort macro is an adaptation of Simon Tatham's O(n log(n)) mergesort * + * Unwieldy variable names used here to avoid shadowing passed-in variables. * + *****************************************************************************/ +#define LL_SORT(list, cmp) \ + LL_SORT2(list, cmp, next) + +#define LL_SORT2(list, cmp, next) \ +do { \ + LDECLTYPE(list) _ls_p; \ + LDECLTYPE(list) _ls_q; \ + LDECLTYPE(list) _ls_e; \ + LDECLTYPE(list) _ls_tail; \ + IF_NO_DECLTYPE(LDECLTYPE(list) _tmp;) \ + int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \ + if (list) { \ + _ls_insize = 1; \ + _ls_looping = 1; \ + while (_ls_looping) { \ + UTLIST_CASTASGN(_ls_p,list); \ + (list) = NULL; \ + _ls_tail = NULL; \ + _ls_nmerges = 0; \ + while (_ls_p) { \ + _ls_nmerges++; \ + _ls_q = _ls_p; \ + _ls_psize = 0; \ + for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \ + _ls_psize++; \ + UTLIST_SV(_ls_q,list); _ls_q = UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); \ + if (!_ls_q) break; \ + } \ + _ls_qsize = _ls_insize; \ + while (_ls_psize > 0 || (_ls_qsize > 0 && _ls_q)) { \ + if (_ls_psize == 0) { \ + _ls_e = _ls_q; UTLIST_SV(_ls_q,list); _ls_q = \ + UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); _ls_qsize--; \ + } else if (_ls_qsize == 0 || !_ls_q) { \ + _ls_e = _ls_p; UTLIST_SV(_ls_p,list); _ls_p = \ + UTLIST_NEXT(_ls_p,list,next); UTLIST_RS(list); _ls_psize--; \ + } else if (cmp(_ls_p,_ls_q) <= 0) { \ + _ls_e = _ls_p; UTLIST_SV(_ls_p,list); _ls_p = \ + UTLIST_NEXT(_ls_p,list,next); UTLIST_RS(list); _ls_psize--; \ + } else { \ + _ls_e = _ls_q; UTLIST_SV(_ls_q,list); _ls_q = \ + UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); _ls_qsize--; \ + } \ + if (_ls_tail) { \ + UTLIST_SV(_ls_tail,list); UTLIST_NEXTASGN(_ls_tail,list,_ls_e,next); UTLIST_RS(list); \ + } else { \ + UTLIST_CASTASGN(list,_ls_e); \ + } \ + _ls_tail = _ls_e; \ + } \ + _ls_p = _ls_q; \ + } \ + if (_ls_tail) { \ + UTLIST_SV(_ls_tail,list); UTLIST_NEXTASGN(_ls_tail,list,NULL,next); UTLIST_RS(list); \ + } \ + if (_ls_nmerges <= 1) { \ + _ls_looping=0; \ + } \ + _ls_insize *= 2; \ + } \ + } \ +} while (0) + + +#define DL_SORT(list, cmp) \ + DL_SORT2(list, cmp, prev, next) + +#define DL_SORT2(list, cmp, prev, next) \ +do { \ + LDECLTYPE(list) _ls_p; \ + LDECLTYPE(list) _ls_q; \ + LDECLTYPE(list) _ls_e; \ + LDECLTYPE(list) _ls_tail; \ + IF_NO_DECLTYPE(LDECLTYPE(list) _tmp;) \ + int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \ + if (list) { \ + _ls_insize = 1; \ + _ls_looping = 1; \ + while (_ls_looping) { \ + UTLIST_CASTASGN(_ls_p,list); \ + (list) = NULL; \ + _ls_tail = NULL; \ + _ls_nmerges = 0; \ + while (_ls_p) { \ + _ls_nmerges++; \ + _ls_q = _ls_p; \ + _ls_psize = 0; \ + for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \ + _ls_psize++; \ + UTLIST_SV(_ls_q,list); _ls_q = UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); \ + if (!_ls_q) break; \ + } \ + _ls_qsize = _ls_insize; \ + while ((_ls_psize > 0) || ((_ls_qsize > 0) && _ls_q)) { \ + if (_ls_psize == 0) { \ + _ls_e = _ls_q; UTLIST_SV(_ls_q,list); _ls_q = \ + UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); _ls_qsize--; \ + } else if ((_ls_qsize == 0) || (!_ls_q)) { \ + _ls_e = _ls_p; UTLIST_SV(_ls_p,list); _ls_p = \ + UTLIST_NEXT(_ls_p,list,next); UTLIST_RS(list); _ls_psize--; \ + } else if (cmp(_ls_p,_ls_q) <= 0) { \ + _ls_e = _ls_p; UTLIST_SV(_ls_p,list); _ls_p = \ + UTLIST_NEXT(_ls_p,list,next); UTLIST_RS(list); _ls_psize--; \ + } else { \ + _ls_e = _ls_q; UTLIST_SV(_ls_q,list); _ls_q = \ + UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); _ls_qsize--; \ + } \ + if (_ls_tail) { \ + UTLIST_SV(_ls_tail,list); UTLIST_NEXTASGN(_ls_tail,list,_ls_e,next); UTLIST_RS(list); \ + } else { \ + UTLIST_CASTASGN(list,_ls_e); \ + } \ + UTLIST_SV(_ls_e,list); UTLIST_PREVASGN(_ls_e,list,_ls_tail,prev); UTLIST_RS(list); \ + _ls_tail = _ls_e; \ + } \ + _ls_p = _ls_q; \ + } \ + UTLIST_CASTASGN((list)->prev, _ls_tail); \ + UTLIST_SV(_ls_tail,list); UTLIST_NEXTASGN(_ls_tail,list,NULL,next); UTLIST_RS(list); \ + if (_ls_nmerges <= 1) { \ + _ls_looping=0; \ + } \ + _ls_insize *= 2; \ + } \ + } \ +} while (0) + +#define CDL_SORT(list, cmp) \ + CDL_SORT2(list, cmp, prev, next) + +#define CDL_SORT2(list, cmp, prev, next) \ +do { \ + LDECLTYPE(list) _ls_p; \ + LDECLTYPE(list) _ls_q; \ + LDECLTYPE(list) _ls_e; \ + LDECLTYPE(list) _ls_tail; \ + LDECLTYPE(list) _ls_oldhead; \ + LDECLTYPE(list) _tmp; \ + int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \ + if (list) { \ + _ls_insize = 1; \ + _ls_looping = 1; \ + while (_ls_looping) { \ + UTLIST_CASTASGN(_ls_p,list); \ + UTLIST_CASTASGN(_ls_oldhead,list); \ + (list) = NULL; \ + _ls_tail = NULL; \ + _ls_nmerges = 0; \ + while (_ls_p) { \ + _ls_nmerges++; \ + _ls_q = _ls_p; \ + _ls_psize = 0; \ + for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) { \ + _ls_psize++; \ + UTLIST_SV(_ls_q,list); \ + if (UTLIST_NEXT(_ls_q,list,next) == _ls_oldhead) { \ + _ls_q = NULL; \ + } else { \ + _ls_q = UTLIST_NEXT(_ls_q,list,next); \ + } \ + UTLIST_RS(list); \ + if (!_ls_q) break; \ + } \ + _ls_qsize = _ls_insize; \ + while (_ls_psize > 0 || (_ls_qsize > 0 && _ls_q)) { \ + if (_ls_psize == 0) { \ + _ls_e = _ls_q; UTLIST_SV(_ls_q,list); _ls_q = \ + UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); _ls_qsize--; \ + if (_ls_q == _ls_oldhead) { _ls_q = NULL; } \ + } else if (_ls_qsize == 0 || !_ls_q) { \ + _ls_e = _ls_p; UTLIST_SV(_ls_p,list); _ls_p = \ + UTLIST_NEXT(_ls_p,list,next); UTLIST_RS(list); _ls_psize--; \ + if (_ls_p == _ls_oldhead) { _ls_p = NULL; } \ + } else if (cmp(_ls_p,_ls_q) <= 0) { \ + _ls_e = _ls_p; UTLIST_SV(_ls_p,list); _ls_p = \ + UTLIST_NEXT(_ls_p,list,next); UTLIST_RS(list); _ls_psize--; \ + if (_ls_p == _ls_oldhead) { _ls_p = NULL; } \ + } else { \ + _ls_e = _ls_q; UTLIST_SV(_ls_q,list); _ls_q = \ + UTLIST_NEXT(_ls_q,list,next); UTLIST_RS(list); _ls_qsize--; \ + if (_ls_q == _ls_oldhead) { _ls_q = NULL; } \ + } \ + if (_ls_tail) { \ + UTLIST_SV(_ls_tail,list); UTLIST_NEXTASGN(_ls_tail,list,_ls_e,next); UTLIST_RS(list); \ + } else { \ + UTLIST_CASTASGN(list,_ls_e); \ + } \ + UTLIST_SV(_ls_e,list); UTLIST_PREVASGN(_ls_e,list,_ls_tail,prev); UTLIST_RS(list); \ + _ls_tail = _ls_e; \ + } \ + _ls_p = _ls_q; \ + } \ + UTLIST_CASTASGN((list)->prev,_ls_tail); \ + UTLIST_CASTASGN(_tmp,list); \ + UTLIST_SV(_ls_tail,list); UTLIST_NEXTASGN(_ls_tail,list,_tmp,next); UTLIST_RS(list); \ + if (_ls_nmerges <= 1) { \ + _ls_looping=0; \ + } \ + _ls_insize *= 2; \ + } \ + } \ +} while (0) + +/****************************************************************************** + * singly linked list macros (non-circular) * + *****************************************************************************/ +#define LL_PREPEND(head,add) \ + LL_PREPEND2(head,add,next) + +#define LL_PREPEND2(head,add,next) \ +do { \ + (add)->next = (head); \ + (head) = (add); \ +} while (0) + +#define LL_CONCAT(head1,head2) \ + LL_CONCAT2(head1,head2,next) + +#define LL_CONCAT2(head1,head2,next) \ +do { \ + LDECLTYPE(head1) _tmp; \ + if (head1) { \ + _tmp = (head1); \ + while (_tmp->next) { _tmp = _tmp->next; } \ + _tmp->next=(head2); \ + } else { \ + (head1)=(head2); \ + } \ +} while (0) + +#define LL_APPEND(head,add) \ + LL_APPEND2(head,add,next) + +#define LL_APPEND2(head,add,next) \ +do { \ + LDECLTYPE(head) _tmp; \ + (add)->next=NULL; \ + if (head) { \ + _tmp = (head); \ + while (_tmp->next) { _tmp = _tmp->next; } \ + _tmp->next=(add); \ + } else { \ + (head)=(add); \ + } \ +} while (0) + +#define LL_INSERT_INORDER(head,add,cmp) \ + LL_INSERT_INORDER2(head,add,cmp,next) + +#define LL_INSERT_INORDER2(head,add,cmp,next) \ +do { \ + LDECLTYPE(head) _tmp; \ + if (head) { \ + LL_LOWER_BOUND2(head, _tmp, add, cmp, next); \ + LL_APPEND_ELEM2(head, _tmp, add, next); \ + } else { \ + (head) = (add); \ + (head)->next = NULL; \ + } \ +} while (0) + +#define LL_LOWER_BOUND(head,elt,like,cmp) \ + LL_LOWER_BOUND2(head,elt,like,cmp,next) + +#define LL_LOWER_BOUND2(head,elt,like,cmp,next) \ + do { \ + if ((head) == NULL || (cmp(head, like)) >= 0) { \ + (elt) = NULL; \ + } else { \ + for ((elt) = (head); (elt)->next != NULL; (elt) = (elt)->next) { \ + if (cmp((elt)->next, like) >= 0) { \ + break; \ + } \ + } \ + } \ + } while (0) + +#define LL_DELETE(head,del) \ + LL_DELETE2(head,del,next) + +#define LL_DELETE2(head,del,next) \ +do { \ + LDECLTYPE(head) _tmp; \ + if ((head) == (del)) { \ + (head)=(head)->next; \ + } else { \ + _tmp = (head); \ + while (_tmp->next && (_tmp->next != (del))) { \ + _tmp = _tmp->next; \ + } \ + if (_tmp->next) { \ + _tmp->next = (del)->next; \ + } \ + } \ +} while (0) + +#define LL_COUNT(head,el,counter) \ + LL_COUNT2(head,el,counter,next) \ + +#define LL_COUNT2(head,el,counter,next) \ +do { \ + (counter) = 0; \ + LL_FOREACH2(head,el,next) { ++(counter); } \ +} while (0) + +#define LL_FOREACH(head,el) \ + LL_FOREACH2(head,el,next) + +#define LL_FOREACH2(head,el,next) \ + for ((el) = (head); el; (el) = (el)->next) + +#define LL_FOREACH_SAFE(head,el,tmp) \ + LL_FOREACH_SAFE2(head,el,tmp,next) + +#define LL_FOREACH_SAFE2(head,el,tmp,next) \ + for ((el) = (head); (el) && ((tmp) = (el)->next, 1); (el) = (tmp)) + +#define LL_SEARCH_SCALAR(head,out,field,val) \ + LL_SEARCH_SCALAR2(head,out,field,val,next) + +#define LL_SEARCH_SCALAR2(head,out,field,val,next) \ +do { \ + LL_FOREACH2(head,out,next) { \ + if ((out)->field == (val)) break; \ + } \ +} while (0) + +#define LL_SEARCH(head,out,elt,cmp) \ + LL_SEARCH2(head,out,elt,cmp,next) + +#define LL_SEARCH2(head,out,elt,cmp,next) \ +do { \ + LL_FOREACH2(head,out,next) { \ + if ((cmp(out,elt))==0) break; \ + } \ +} while (0) + +#define LL_REPLACE_ELEM2(head, el, add, next) \ +do { \ + LDECLTYPE(head) _tmp; \ + assert((head) != NULL); \ + assert((el) != NULL); \ + assert((add) != NULL); \ + (add)->next = (el)->next; \ + if ((head) == (el)) { \ + (head) = (add); \ + } else { \ + _tmp = (head); \ + while (_tmp->next && (_tmp->next != (el))) { \ + _tmp = _tmp->next; \ + } \ + if (_tmp->next) { \ + _tmp->next = (add); \ + } \ + } \ +} while (0) + +#define LL_REPLACE_ELEM(head, el, add) \ + LL_REPLACE_ELEM2(head, el, add, next) + +#define LL_PREPEND_ELEM2(head, el, add, next) \ +do { \ + if (el) { \ + LDECLTYPE(head) _tmp; \ + assert((head) != NULL); \ + assert((add) != NULL); \ + (add)->next = (el); \ + if ((head) == (el)) { \ + (head) = (add); \ + } else { \ + _tmp = (head); \ + while (_tmp->next && (_tmp->next != (el))) { \ + _tmp = _tmp->next; \ + } \ + if (_tmp->next) { \ + _tmp->next = (add); \ + } \ + } \ + } else { \ + LL_APPEND2(head, add, next); \ + } \ +} while (0) \ + +#define LL_PREPEND_ELEM(head, el, add) \ + LL_PREPEND_ELEM2(head, el, add, next) + +#define LL_APPEND_ELEM2(head, el, add, next) \ +do { \ + if (el) { \ + assert((head) != NULL); \ + assert((add) != NULL); \ + (add)->next = (el)->next; \ + (el)->next = (add); \ + } else { \ + LL_PREPEND2(head, add, next); \ + } \ +} while (0) \ + +#define LL_APPEND_ELEM(head, el, add) \ + LL_APPEND_ELEM2(head, el, add, next) + +#ifdef NO_DECLTYPE +/* Here are VS2008 / NO_DECLTYPE replacements for a few functions */ + +#undef LL_CONCAT2 +#define LL_CONCAT2(head1,head2,next) \ +do { \ + char *_tmp; \ + if (head1) { \ + _tmp = (char*)(head1); \ + while ((head1)->next) { (head1) = (head1)->next; } \ + (head1)->next = (head2); \ + UTLIST_RS(head1); \ + } else { \ + (head1)=(head2); \ + } \ +} while (0) + +#undef LL_APPEND2 +#define LL_APPEND2(head,add,next) \ +do { \ + if (head) { \ + (add)->next = head; /* use add->next as a temp variable */ \ + while ((add)->next->next) { (add)->next = (add)->next->next; } \ + (add)->next->next=(add); \ + } else { \ + (head)=(add); \ + } \ + (add)->next=NULL; \ +} while (0) + +#undef LL_INSERT_INORDER2 +#define LL_INSERT_INORDER2(head,add,cmp,next) \ +do { \ + if ((head) == NULL || (cmp(head, add)) >= 0) { \ + (add)->next = (head); \ + (head) = (add); \ + } else { \ + char *_tmp = (char*)(head); \ + while ((head)->next != NULL && (cmp((head)->next, add)) < 0) { \ + (head) = (head)->next; \ + } \ + (add)->next = (head)->next; \ + (head)->next = (add); \ + UTLIST_RS(head); \ + } \ +} while (0) + +#undef LL_DELETE2 +#define LL_DELETE2(head,del,next) \ +do { \ + if ((head) == (del)) { \ + (head)=(head)->next; \ + } else { \ + char *_tmp = (char*)(head); \ + while ((head)->next && ((head)->next != (del))) { \ + (head) = (head)->next; \ + } \ + if ((head)->next) { \ + (head)->next = ((del)->next); \ + } \ + UTLIST_RS(head); \ + } \ +} while (0) + +#undef LL_REPLACE_ELEM2 +#define LL_REPLACE_ELEM2(head, el, add, next) \ +do { \ + assert((head) != NULL); \ + assert((el) != NULL); \ + assert((add) != NULL); \ + if ((head) == (el)) { \ + (head) = (add); \ + } else { \ + (add)->next = head; \ + while ((add)->next->next && ((add)->next->next != (el))) { \ + (add)->next = (add)->next->next; \ + } \ + if ((add)->next->next) { \ + (add)->next->next = (add); \ + } \ + } \ + (add)->next = (el)->next; \ +} while (0) + +#undef LL_PREPEND_ELEM2 +#define LL_PREPEND_ELEM2(head, el, add, next) \ +do { \ + if (el) { \ + assert((head) != NULL); \ + assert((add) != NULL); \ + if ((head) == (el)) { \ + (head) = (add); \ + } else { \ + (add)->next = (head); \ + while ((add)->next->next && ((add)->next->next != (el))) { \ + (add)->next = (add)->next->next; \ + } \ + if ((add)->next->next) { \ + (add)->next->next = (add); \ + } \ + } \ + (add)->next = (el); \ + } else { \ + LL_APPEND2(head, add, next); \ + } \ +} while (0) \ + +#endif /* NO_DECLTYPE */ + +/****************************************************************************** + * doubly linked list macros (non-circular) * + *****************************************************************************/ +#define DL_PREPEND(head,add) \ + DL_PREPEND2(head,add,prev,next) + +#define DL_PREPEND2(head,add,prev,next) \ +do { \ + (add)->next = (head); \ + if (head) { \ + (add)->prev = (head)->prev; \ + (head)->prev = (add); \ + } else { \ + (add)->prev = (add); \ + } \ + (head) = (add); \ +} while (0) + +#define DL_APPEND(head,add) \ + DL_APPEND2(head,add,prev,next) + +#define DL_APPEND2(head,add,prev,next) \ +do { \ + if (head) { \ + (add)->prev = (head)->prev; \ + (head)->prev->next = (add); \ + (head)->prev = (add); \ + (add)->next = NULL; \ + } else { \ + (head)=(add); \ + (head)->prev = (head); \ + (head)->next = NULL; \ + } \ +} while (0) + +#define DL_INSERT_INORDER(head,add,cmp) \ + DL_INSERT_INORDER2(head,add,cmp,prev,next) + +#define DL_INSERT_INORDER2(head,add,cmp,prev,next) \ +do { \ + LDECLTYPE(head) _tmp; \ + if (head) { \ + DL_LOWER_BOUND2(head, _tmp, add, cmp, next); \ + DL_APPEND_ELEM2(head, _tmp, add, prev, next); \ + } else { \ + (head) = (add); \ + (head)->prev = (head); \ + (head)->next = NULL; \ + } \ +} while (0) + +#define DL_LOWER_BOUND(head,elt,like,cmp) \ + DL_LOWER_BOUND2(head,elt,like,cmp,next) + +#define DL_LOWER_BOUND2(head,elt,like,cmp,next) \ +do { \ + if ((head) == NULL || (cmp(head, like)) >= 0) { \ + (elt) = NULL; \ + } else { \ + for ((elt) = (head); (elt)->next != NULL; (elt) = (elt)->next) { \ + if ((cmp((elt)->next, like)) >= 0) { \ + break; \ + } \ + } \ + } \ +} while (0) + +#define DL_CONCAT(head1,head2) \ + DL_CONCAT2(head1,head2,prev,next) + +#define DL_CONCAT2(head1,head2,prev,next) \ +do { \ + LDECLTYPE(head1) _tmp; \ + if (head2) { \ + if (head1) { \ + UTLIST_CASTASGN(_tmp, (head2)->prev); \ + (head2)->prev = (head1)->prev; \ + (head1)->prev->next = (head2); \ + UTLIST_CASTASGN((head1)->prev, _tmp); \ + } else { \ + (head1)=(head2); \ + } \ + } \ +} while (0) + +#define DL_DELETE(head,del) \ + DL_DELETE2(head,del,prev,next) + +#define DL_DELETE2(head,del,prev,next) \ +do { \ + assert((head) != NULL); \ + assert((del)->prev != NULL); \ + if ((del)->prev == (del)) { \ + (head)=NULL; \ + } else if ((del) == (head)) { \ + assert((del)->next != NULL); \ + (del)->next->prev = (del)->prev; \ + (head) = (del)->next; \ + } else { \ + (del)->prev->next = (del)->next; \ + if ((del)->next) { \ + (del)->next->prev = (del)->prev; \ + } else { \ + (head)->prev = (del)->prev; \ + } \ + } \ +} while (0) + +#define DL_COUNT(head,el,counter) \ + DL_COUNT2(head,el,counter,next) \ + +#define DL_COUNT2(head,el,counter,next) \ +do { \ + (counter) = 0; \ + DL_FOREACH2(head,el,next) { ++(counter); } \ +} while (0) + +#define DL_FOREACH(head,el) \ + DL_FOREACH2(head,el,next) + +#define DL_FOREACH2(head,el,next) \ + for ((el) = (head); el; (el) = (el)->next) + +/* this version is safe for deleting the elements during iteration */ +#define DL_FOREACH_SAFE(head,el,tmp) \ + DL_FOREACH_SAFE2(head,el,tmp,next) + +#define DL_FOREACH_SAFE2(head,el,tmp,next) \ + for ((el) = (head); (el) && ((tmp) = (el)->next, 1); (el) = (tmp)) + +/* these are identical to their singly-linked list counterparts */ +#define DL_SEARCH_SCALAR LL_SEARCH_SCALAR +#define DL_SEARCH LL_SEARCH +#define DL_SEARCH_SCALAR2 LL_SEARCH_SCALAR2 +#define DL_SEARCH2 LL_SEARCH2 + +#define DL_REPLACE_ELEM2(head, el, add, prev, next) \ +do { \ + assert((head) != NULL); \ + assert((el) != NULL); \ + assert((add) != NULL); \ + if ((head) == (el)) { \ + (head) = (add); \ + (add)->next = (el)->next; \ + if ((el)->next == NULL) { \ + (add)->prev = (add); \ + } else { \ + (add)->prev = (el)->prev; \ + (add)->next->prev = (add); \ + } \ + } else { \ + (add)->next = (el)->next; \ + (add)->prev = (el)->prev; \ + (add)->prev->next = (add); \ + if ((el)->next == NULL) { \ + (head)->prev = (add); \ + } else { \ + (add)->next->prev = (add); \ + } \ + } \ +} while (0) + +#define DL_REPLACE_ELEM(head, el, add) \ + DL_REPLACE_ELEM2(head, el, add, prev, next) + +#define DL_PREPEND_ELEM2(head, el, add, prev, next) \ +do { \ + if (el) { \ + assert((head) != NULL); \ + assert((add) != NULL); \ + (add)->next = (el); \ + (add)->prev = (el)->prev; \ + (el)->prev = (add); \ + if ((head) == (el)) { \ + (head) = (add); \ + } else { \ + (add)->prev->next = (add); \ + } \ + } else { \ + DL_APPEND2(head, add, prev, next); \ + } \ +} while (0) \ + +#define DL_PREPEND_ELEM(head, el, add) \ + DL_PREPEND_ELEM2(head, el, add, prev, next) + +#define DL_APPEND_ELEM2(head, el, add, prev, next) \ +do { \ + if (el) { \ + assert((head) != NULL); \ + assert((add) != NULL); \ + (add)->next = (el)->next; \ + (add)->prev = (el); \ + (el)->next = (add); \ + if ((add)->next) { \ + (add)->next->prev = (add); \ + } else { \ + (head)->prev = (add); \ + } \ + } else { \ + DL_PREPEND2(head, add, prev, next); \ + } \ +} while (0) \ + +#define DL_APPEND_ELEM(head, el, add) \ + DL_APPEND_ELEM2(head, el, add, prev, next) + +#ifdef NO_DECLTYPE +/* Here are VS2008 / NO_DECLTYPE replacements for a few functions */ + +#undef DL_INSERT_INORDER2 +#define DL_INSERT_INORDER2(head,add,cmp,prev,next) \ +do { \ + if ((head) == NULL) { \ + (add)->prev = (add); \ + (add)->next = NULL; \ + (head) = (add); \ + } else if ((cmp(head, add)) >= 0) { \ + (add)->prev = (head)->prev; \ + (add)->next = (head); \ + (head)->prev = (add); \ + (head) = (add); \ + } else { \ + char *_tmp = (char*)(head); \ + while ((head)->next && (cmp((head)->next, add)) < 0) { \ + (head) = (head)->next; \ + } \ + (add)->prev = (head); \ + (add)->next = (head)->next; \ + (head)->next = (add); \ + UTLIST_RS(head); \ + if ((add)->next) { \ + (add)->next->prev = (add); \ + } else { \ + (head)->prev = (add); \ + } \ + } \ +} while (0) +#endif /* NO_DECLTYPE */ + +/****************************************************************************** + * circular doubly linked list macros * + *****************************************************************************/ +#define CDL_APPEND(head,add) \ + CDL_APPEND2(head,add,prev,next) + +#define CDL_APPEND2(head,add,prev,next) \ +do { \ + if (head) { \ + (add)->prev = (head)->prev; \ + (add)->next = (head); \ + (head)->prev = (add); \ + (add)->prev->next = (add); \ + } else { \ + (add)->prev = (add); \ + (add)->next = (add); \ + (head) = (add); \ + } \ +} while (0) + +#define CDL_PREPEND(head,add) \ + CDL_PREPEND2(head,add,prev,next) + +#define CDL_PREPEND2(head,add,prev,next) \ +do { \ + if (head) { \ + (add)->prev = (head)->prev; \ + (add)->next = (head); \ + (head)->prev = (add); \ + (add)->prev->next = (add); \ + } else { \ + (add)->prev = (add); \ + (add)->next = (add); \ + } \ + (head) = (add); \ +} while (0) + +#define CDL_INSERT_INORDER(head,add,cmp) \ + CDL_INSERT_INORDER2(head,add,cmp,prev,next) + +#define CDL_INSERT_INORDER2(head,add,cmp,prev,next) \ +do { \ + LDECLTYPE(head) _tmp; \ + if (head) { \ + CDL_LOWER_BOUND2(head, _tmp, add, cmp, next); \ + CDL_APPEND_ELEM2(head, _tmp, add, prev, next); \ + } else { \ + (head) = (add); \ + (head)->next = (head); \ + (head)->prev = (head); \ + } \ +} while (0) + +#define CDL_LOWER_BOUND(head,elt,like,cmp) \ + CDL_LOWER_BOUND2(head,elt,like,cmp,next) + +#define CDL_LOWER_BOUND2(head,elt,like,cmp,next) \ +do { \ + if ((head) == NULL || (cmp(head, like)) >= 0) { \ + (elt) = NULL; \ + } else { \ + for ((elt) = (head); (elt)->next != (head); (elt) = (elt)->next) { \ + if ((cmp((elt)->next, like)) >= 0) { \ + break; \ + } \ + } \ + } \ +} while (0) + +#define CDL_DELETE(head,del) \ + CDL_DELETE2(head,del,prev,next) + +#define CDL_DELETE2(head,del,prev,next) \ +do { \ + if (((head)==(del)) && ((head)->next == (head))) { \ + (head) = NULL; \ + } else { \ + (del)->next->prev = (del)->prev; \ + (del)->prev->next = (del)->next; \ + if ((del) == (head)) (head)=(del)->next; \ + } \ +} while (0) + +#define CDL_COUNT(head,el,counter) \ + CDL_COUNT2(head,el,counter,next) \ + +#define CDL_COUNT2(head, el, counter,next) \ +do { \ + (counter) = 0; \ + CDL_FOREACH2(head,el,next) { ++(counter); } \ +} while (0) + +#define CDL_FOREACH(head,el) \ + CDL_FOREACH2(head,el,next) + +#define CDL_FOREACH2(head,el,next) \ + for ((el)=(head);el;(el)=(((el)->next==(head)) ? NULL : (el)->next)) + +#define CDL_FOREACH_SAFE(head,el,tmp1,tmp2) \ + CDL_FOREACH_SAFE2(head,el,tmp1,tmp2,prev,next) + +#define CDL_FOREACH_SAFE2(head,el,tmp1,tmp2,prev,next) \ + for ((el) = (head), (tmp1) = (head) ? (head)->prev : NULL; \ + (el) && ((tmp2) = (el)->next, 1); \ + (el) = ((el) == (tmp1) ? NULL : (tmp2))) + +#define CDL_SEARCH_SCALAR(head,out,field,val) \ + CDL_SEARCH_SCALAR2(head,out,field,val,next) + +#define CDL_SEARCH_SCALAR2(head,out,field,val,next) \ +do { \ + CDL_FOREACH2(head,out,next) { \ + if ((out)->field == (val)) break; \ + } \ +} while (0) + +#define CDL_SEARCH(head,out,elt,cmp) \ + CDL_SEARCH2(head,out,elt,cmp,next) + +#define CDL_SEARCH2(head,out,elt,cmp,next) \ +do { \ + CDL_FOREACH2(head,out,next) { \ + if ((cmp(out,elt))==0) break; \ + } \ +} while (0) + +#define CDL_REPLACE_ELEM2(head, el, add, prev, next) \ +do { \ + assert((head) != NULL); \ + assert((el) != NULL); \ + assert((add) != NULL); \ + if ((el)->next == (el)) { \ + (add)->next = (add); \ + (add)->prev = (add); \ + (head) = (add); \ + } else { \ + (add)->next = (el)->next; \ + (add)->prev = (el)->prev; \ + (add)->next->prev = (add); \ + (add)->prev->next = (add); \ + if ((head) == (el)) { \ + (head) = (add); \ + } \ + } \ +} while (0) + +#define CDL_REPLACE_ELEM(head, el, add) \ + CDL_REPLACE_ELEM2(head, el, add, prev, next) + +#define CDL_PREPEND_ELEM2(head, el, add, prev, next) \ +do { \ + if (el) { \ + assert((head) != NULL); \ + assert((add) != NULL); \ + (add)->next = (el); \ + (add)->prev = (el)->prev; \ + (el)->prev = (add); \ + (add)->prev->next = (add); \ + if ((head) == (el)) { \ + (head) = (add); \ + } \ + } else { \ + CDL_APPEND2(head, add, prev, next); \ + } \ +} while (0) + +#define CDL_PREPEND_ELEM(head, el, add) \ + CDL_PREPEND_ELEM2(head, el, add, prev, next) + +#define CDL_APPEND_ELEM2(head, el, add, prev, next) \ +do { \ + if (el) { \ + assert((head) != NULL); \ + assert((add) != NULL); \ + (add)->next = (el)->next; \ + (add)->prev = (el); \ + (el)->next = (add); \ + (add)->next->prev = (add); \ + } else { \ + CDL_PREPEND2(head, add, prev, next); \ + } \ +} while (0) + +#define CDL_APPEND_ELEM(head, el, add) \ + CDL_APPEND_ELEM2(head, el, add, prev, next) + +#ifdef NO_DECLTYPE +/* Here are VS2008 / NO_DECLTYPE replacements for a few functions */ + +#undef CDL_INSERT_INORDER2 +#define CDL_INSERT_INORDER2(head,add,cmp,prev,next) \ +do { \ + if ((head) == NULL) { \ + (add)->prev = (add); \ + (add)->next = (add); \ + (head) = (add); \ + } else if ((cmp(head, add)) >= 0) { \ + (add)->prev = (head)->prev; \ + (add)->next = (head); \ + (add)->prev->next = (add); \ + (head)->prev = (add); \ + (head) = (add); \ + } else { \ + char *_tmp = (char*)(head); \ + while ((char*)(head)->next != _tmp && (cmp((head)->next, add)) < 0) { \ + (head) = (head)->next; \ + } \ + (add)->prev = (head); \ + (add)->next = (head)->next; \ + (add)->next->prev = (add); \ + (head)->next = (add); \ + UTLIST_RS(head); \ + } \ +} while (0) +#endif /* NO_DECLTYPE */ + +#endif /* UTLIST_H */ diff --git a/utlist.h b/utlist.h deleted file mode 100644 index 5655614..0000000 --- a/utlist.h +++ /dev/null @@ -1,1398 +0,0 @@ -/* -Copyright (c) 2007-2022, Troy D. Hanson https://troydhanson.github.io/uthash/ -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS -IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER -OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef UTLIST_H -#define UTLIST_H - -#define UTLIST_VERSION 2.3.0 - -#include - -/* - * This file contains macros to manipulate singly and doubly-linked lists. - * - * 1. LL_ macros: singly-linked lists. - * 2. DL_ macros: doubly-linked lists. - * 3. CDL_ macros: circular doubly-linked lists. - * - * To use singly-linked lists, your structure must have a "next" pointer. - * To use doubly-linked lists, your structure must "prev" and "next" pointers. - * Either way, the pointer to the head of the list must be initialized to NULL. - * - * ----------------.EXAMPLE ------------------------- - * struct item { - * int id; - * struct item *prev, *next; - * } - * - * struct item *list = NULL: - * - * int main() { - * struct item *item; - * ... allocate and populate item ... - * DL_APPEND(list, item); - * } - * -------------------------------------------------- - * - * For doubly-linked lists, the append and delete macros are O(1) - * For singly-linked lists, append and delete are O(n) but prepend is O(1) - * The sort macro is O(n log(n)) for all types of single/double/circular lists. - */ - -/* These macros use decltype or the earlier __typeof GNU extension. - As decltype is only available in newer compilers (VS2010 or gcc 4.3+ - when compiling c++ source) this code uses whatever method is needed - or, for VS2008 where neither is available, uses casting workarounds. */ -#if !defined(LDECLTYPE) && !defined(NO_DECLTYPE) -#if defined(_MSC_VER) /* MS compiler */ -#if _MSC_VER >= 1600 && defined(__cplusplus) /* VS2010 or newer in C++ mode */ -#define LDECLTYPE(x) decltype(x) -#else /* VS2008 or older (or VS2010 in C mode) */ -#define NO_DECLTYPE -#endif -#elif defined(__MCST__) /* Elbrus C Compiler */ -#define LDECLTYPE(x) __typeof(x) -#elif defined(__BORLANDC__) || defined(__ICCARM__) || defined(__LCC__) || defined(__WATCOMC__) -#define NO_DECLTYPE -#else /* GNU, Sun and other compilers */ -#define LDECLTYPE(x) __typeof(x) -#endif -#endif - -/* for VS2008 we use some workarounds to get around the lack of decltype, - * namely, we always reassign our tmp variable to the list head if we need - * to dereference its prev/next pointers, and save/restore the real head.*/ -#ifdef NO_DECLTYPE -#define IF_NO_DECLTYPE(x) x -#define LDECLTYPE(x) char * -#define UTLIST_SV(elt, list) \ - _tmp = (char *)(list); \ - { \ - char **_alias = (char **)&(list); \ - *_alias = (elt); \ - } -#define UTLIST_NEXT(elt, list, next) ((char *)((list)->next)) -#define UTLIST_NEXTASGN(elt, list, to, next) \ - { \ - char **_alias = (char **)&((list)->next); \ - *_alias = (char *)(to); \ - } -/* #define UTLIST_PREV(elt,list,prev) ((char*)((list)->prev)) */ -#define UTLIST_PREVASGN(elt, list, to, prev) \ - { \ - char **_alias = (char **)&((list)->prev); \ - *_alias = (char *)(to); \ - } -#define UTLIST_RS(list) \ - { \ - char **_alias = (char **)&(list); \ - *_alias = _tmp; \ - } -#define UTLIST_CASTASGN(a, b) \ - { \ - char **_alias = (char **)&(a); \ - *_alias = (char *)(b); \ - } -#else -#define IF_NO_DECLTYPE(x) -#define UTLIST_SV(elt, list) -#define UTLIST_NEXT(elt, list, next) ((elt)->next) -#define UTLIST_NEXTASGN(elt, list, to, next) ((elt)->next) = (to) -/* #define UTLIST_PREV(elt,list,prev) ((elt)->prev) */ -#define UTLIST_PREVASGN(elt, list, to, prev) ((elt)->prev) = (to) -#define UTLIST_RS(list) -#define UTLIST_CASTASGN(a, b) (a) = (b) -#endif - -/****************************************************************************** - * The sort macro is an adaptation of Simon Tatham's O(n log(n)) mergesort * - * Unwieldy variable names used here to avoid shadowing passed-in variables. * - *****************************************************************************/ -#define LL_SORT(list, cmp) LL_SORT2(list, cmp, next) - -#define LL_SORT2(list, cmp, next) \ - do \ - { \ - LDECLTYPE(list) _ls_p; \ - LDECLTYPE(list) _ls_q; \ - LDECLTYPE(list) _ls_e; \ - LDECLTYPE(list) _ls_tail; \ - IF_NO_DECLTYPE(LDECLTYPE(list) _tmp;) \ - int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \ - if (list) \ - { \ - _ls_insize = 1; \ - _ls_looping = 1; \ - while (_ls_looping) \ - { \ - UTLIST_CASTASGN(_ls_p, list); \ - (list) = NULL; \ - _ls_tail = NULL; \ - _ls_nmerges = 0; \ - while (_ls_p) \ - { \ - _ls_nmerges++; \ - _ls_q = _ls_p; \ - _ls_psize = 0; \ - for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) \ - { \ - _ls_psize++; \ - UTLIST_SV(_ls_q, list); \ - _ls_q = UTLIST_NEXT(_ls_q, list, next); \ - UTLIST_RS(list); \ - if (!_ls_q) \ - break; \ - } \ - _ls_qsize = _ls_insize; \ - while (_ls_psize > 0 || (_ls_qsize > 0 && _ls_q)) \ - { \ - if (_ls_psize == 0) \ - { \ - _ls_e = _ls_q; \ - UTLIST_SV(_ls_q, list); \ - _ls_q = UTLIST_NEXT(_ls_q, list, next); \ - UTLIST_RS(list); \ - _ls_qsize--; \ - } \ - else if (_ls_qsize == 0 || !_ls_q) \ - { \ - _ls_e = _ls_p; \ - UTLIST_SV(_ls_p, list); \ - _ls_p = UTLIST_NEXT(_ls_p, list, next); \ - UTLIST_RS(list); \ - _ls_psize--; \ - } \ - else if (cmp(_ls_p, _ls_q) <= 0) \ - { \ - _ls_e = _ls_p; \ - UTLIST_SV(_ls_p, list); \ - _ls_p = UTLIST_NEXT(_ls_p, list, next); \ - UTLIST_RS(list); \ - _ls_psize--; \ - } \ - else \ - { \ - _ls_e = _ls_q; \ - UTLIST_SV(_ls_q, list); \ - _ls_q = UTLIST_NEXT(_ls_q, list, next); \ - UTLIST_RS(list); \ - _ls_qsize--; \ - } \ - if (_ls_tail) \ - { \ - UTLIST_SV(_ls_tail, list); \ - UTLIST_NEXTASGN(_ls_tail, list, _ls_e, next); \ - UTLIST_RS(list); \ - } \ - else \ - { \ - UTLIST_CASTASGN(list, _ls_e); \ - } \ - _ls_tail = _ls_e; \ - } \ - _ls_p = _ls_q; \ - } \ - if (_ls_tail) \ - { \ - UTLIST_SV(_ls_tail, list); \ - UTLIST_NEXTASGN(_ls_tail, list, NULL, next); \ - UTLIST_RS(list); \ - } \ - if (_ls_nmerges <= 1) \ - { \ - _ls_looping = 0; \ - } \ - _ls_insize *= 2; \ - } \ - } \ - } while (0) - -#define DL_SORT(list, cmp) DL_SORT2(list, cmp, prev, next) - -#define DL_SORT2(list, cmp, prev, next) \ - do \ - { \ - LDECLTYPE(list) _ls_p; \ - LDECLTYPE(list) _ls_q; \ - LDECLTYPE(list) _ls_e; \ - LDECLTYPE(list) _ls_tail; \ - IF_NO_DECLTYPE(LDECLTYPE(list) _tmp;) \ - int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \ - if (list) \ - { \ - _ls_insize = 1; \ - _ls_looping = 1; \ - while (_ls_looping) \ - { \ - UTLIST_CASTASGN(_ls_p, list); \ - (list) = NULL; \ - _ls_tail = NULL; \ - _ls_nmerges = 0; \ - while (_ls_p) \ - { \ - _ls_nmerges++; \ - _ls_q = _ls_p; \ - _ls_psize = 0; \ - for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) \ - { \ - _ls_psize++; \ - UTLIST_SV(_ls_q, list); \ - _ls_q = UTLIST_NEXT(_ls_q, list, next); \ - UTLIST_RS(list); \ - if (!_ls_q) \ - break; \ - } \ - _ls_qsize = _ls_insize; \ - while ((_ls_psize > 0) || ((_ls_qsize > 0) && _ls_q)) \ - { \ - if (_ls_psize == 0) \ - { \ - _ls_e = _ls_q; \ - UTLIST_SV(_ls_q, list); \ - _ls_q = UTLIST_NEXT(_ls_q, list, next); \ - UTLIST_RS(list); \ - _ls_qsize--; \ - } \ - else if ((_ls_qsize == 0) || (!_ls_q)) \ - { \ - _ls_e = _ls_p; \ - UTLIST_SV(_ls_p, list); \ - _ls_p = UTLIST_NEXT(_ls_p, list, next); \ - UTLIST_RS(list); \ - _ls_psize--; \ - } \ - else if (cmp(_ls_p, _ls_q) <= 0) \ - { \ - _ls_e = _ls_p; \ - UTLIST_SV(_ls_p, list); \ - _ls_p = UTLIST_NEXT(_ls_p, list, next); \ - UTLIST_RS(list); \ - _ls_psize--; \ - } \ - else \ - { \ - _ls_e = _ls_q; \ - UTLIST_SV(_ls_q, list); \ - _ls_q = UTLIST_NEXT(_ls_q, list, next); \ - UTLIST_RS(list); \ - _ls_qsize--; \ - } \ - if (_ls_tail) \ - { \ - UTLIST_SV(_ls_tail, list); \ - UTLIST_NEXTASGN(_ls_tail, list, _ls_e, next); \ - UTLIST_RS(list); \ - } \ - else \ - { \ - UTLIST_CASTASGN(list, _ls_e); \ - } \ - UTLIST_SV(_ls_e, list); \ - UTLIST_PREVASGN(_ls_e, list, _ls_tail, prev); \ - UTLIST_RS(list); \ - _ls_tail = _ls_e; \ - } \ - _ls_p = _ls_q; \ - } \ - UTLIST_CASTASGN((list)->prev, _ls_tail); \ - UTLIST_SV(_ls_tail, list); \ - UTLIST_NEXTASGN(_ls_tail, list, NULL, next); \ - UTLIST_RS(list); \ - if (_ls_nmerges <= 1) \ - { \ - _ls_looping = 0; \ - } \ - _ls_insize *= 2; \ - } \ - } \ - } while (0) - -#define CDL_SORT(list, cmp) CDL_SORT2(list, cmp, prev, next) - -#define CDL_SORT2(list, cmp, prev, next) \ - do \ - { \ - LDECLTYPE(list) _ls_p; \ - LDECLTYPE(list) _ls_q; \ - LDECLTYPE(list) _ls_e; \ - LDECLTYPE(list) _ls_tail; \ - LDECLTYPE(list) _ls_oldhead; \ - LDECLTYPE(list) _tmp; \ - int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping; \ - if (list) \ - { \ - _ls_insize = 1; \ - _ls_looping = 1; \ - while (_ls_looping) \ - { \ - UTLIST_CASTASGN(_ls_p, list); \ - UTLIST_CASTASGN(_ls_oldhead, list); \ - (list) = NULL; \ - _ls_tail = NULL; \ - _ls_nmerges = 0; \ - while (_ls_p) \ - { \ - _ls_nmerges++; \ - _ls_q = _ls_p; \ - _ls_psize = 0; \ - for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) \ - { \ - _ls_psize++; \ - UTLIST_SV(_ls_q, list); \ - if (UTLIST_NEXT(_ls_q, list, next) == _ls_oldhead) \ - { \ - _ls_q = NULL; \ - } \ - else \ - { \ - _ls_q = UTLIST_NEXT(_ls_q, list, next); \ - } \ - UTLIST_RS(list); \ - if (!_ls_q) \ - break; \ - } \ - _ls_qsize = _ls_insize; \ - while (_ls_psize > 0 || (_ls_qsize > 0 && _ls_q)) \ - { \ - if (_ls_psize == 0) \ - { \ - _ls_e = _ls_q; \ - UTLIST_SV(_ls_q, list); \ - _ls_q = UTLIST_NEXT(_ls_q, list, next); \ - UTLIST_RS(list); \ - _ls_qsize--; \ - if (_ls_q == _ls_oldhead) \ - { \ - _ls_q = NULL; \ - } \ - } \ - else if (_ls_qsize == 0 || !_ls_q) \ - { \ - _ls_e = _ls_p; \ - UTLIST_SV(_ls_p, list); \ - _ls_p = UTLIST_NEXT(_ls_p, list, next); \ - UTLIST_RS(list); \ - _ls_psize--; \ - if (_ls_p == _ls_oldhead) \ - { \ - _ls_p = NULL; \ - } \ - } \ - else if (cmp(_ls_p, _ls_q) <= 0) \ - { \ - _ls_e = _ls_p; \ - UTLIST_SV(_ls_p, list); \ - _ls_p = UTLIST_NEXT(_ls_p, list, next); \ - UTLIST_RS(list); \ - _ls_psize--; \ - if (_ls_p == _ls_oldhead) \ - { \ - _ls_p = NULL; \ - } \ - } \ - else \ - { \ - _ls_e = _ls_q; \ - UTLIST_SV(_ls_q, list); \ - _ls_q = UTLIST_NEXT(_ls_q, list, next); \ - UTLIST_RS(list); \ - _ls_qsize--; \ - if (_ls_q == _ls_oldhead) \ - { \ - _ls_q = NULL; \ - } \ - } \ - if (_ls_tail) \ - { \ - UTLIST_SV(_ls_tail, list); \ - UTLIST_NEXTASGN(_ls_tail, list, _ls_e, next); \ - UTLIST_RS(list); \ - } \ - else \ - { \ - UTLIST_CASTASGN(list, _ls_e); \ - } \ - UTLIST_SV(_ls_e, list); \ - UTLIST_PREVASGN(_ls_e, list, _ls_tail, prev); \ - UTLIST_RS(list); \ - _ls_tail = _ls_e; \ - } \ - _ls_p = _ls_q; \ - } \ - UTLIST_CASTASGN((list)->prev, _ls_tail); \ - UTLIST_CASTASGN(_tmp, list); \ - UTLIST_SV(_ls_tail, list); \ - UTLIST_NEXTASGN(_ls_tail, list, _tmp, next); \ - UTLIST_RS(list); \ - if (_ls_nmerges <= 1) \ - { \ - _ls_looping = 0; \ - } \ - _ls_insize *= 2; \ - } \ - } \ - } while (0) - -/****************************************************************************** - * singly linked list macros (non-circular) * - *****************************************************************************/ -#define LL_PREPEND(head, add) LL_PREPEND2(head, add, next) - -#define LL_PREPEND2(head, add, next) \ - do \ - { \ - (add)->next = (head); \ - (head) = (add); \ - } while (0) - -#define LL_CONCAT(head1, head2) LL_CONCAT2(head1, head2, next) - -#define LL_CONCAT2(head1, head2, next) \ - do \ - { \ - LDECLTYPE(head1) _tmp; \ - if (head1) \ - { \ - _tmp = (head1); \ - while (_tmp->next) \ - { \ - _tmp = _tmp->next; \ - } \ - _tmp->next = (head2); \ - } \ - else \ - { \ - (head1) = (head2); \ - } \ - } while (0) - -#define LL_APPEND(head, add) LL_APPEND2(head, add, next) - -#define LL_APPEND2(head, add, next) \ - do \ - { \ - LDECLTYPE(head) _tmp; \ - (add)->next = NULL; \ - if (head) \ - { \ - _tmp = (head); \ - while (_tmp->next) \ - { \ - _tmp = _tmp->next; \ - } \ - _tmp->next = (add); \ - } \ - else \ - { \ - (head) = (add); \ - } \ - } while (0) - -#define LL_INSERT_INORDER(head, add, cmp) LL_INSERT_INORDER2(head, add, cmp, next) - -#define LL_INSERT_INORDER2(head, add, cmp, next) \ - do \ - { \ - LDECLTYPE(head) _tmp; \ - if (head) \ - { \ - LL_LOWER_BOUND2(head, _tmp, add, cmp, next); \ - LL_APPEND_ELEM2(head, _tmp, add, next); \ - } \ - else \ - { \ - (head) = (add); \ - (head)->next = NULL; \ - } \ - } while (0) - -#define LL_LOWER_BOUND(head, elt, like, cmp) LL_LOWER_BOUND2(head, elt, like, cmp, next) - -#define LL_LOWER_BOUND2(head, elt, like, cmp, next) \ - do \ - { \ - if ((head) == NULL || (cmp(head, like)) >= 0) \ - { \ - (elt) = NULL; \ - } \ - else \ - { \ - for ((elt) = (head); (elt)->next != NULL; (elt) = (elt)->next) \ - { \ - if (cmp((elt)->next, like) >= 0) \ - { \ - break; \ - } \ - } \ - } \ - } while (0) - -#define LL_DELETE(head, del) LL_DELETE2(head, del, next) - -#define LL_DELETE2(head, del, next) \ - do \ - { \ - LDECLTYPE(head) _tmp; \ - if ((head) == (del)) \ - { \ - (head) = (head)->next; \ - } \ - else \ - { \ - _tmp = (head); \ - while (_tmp->next && (_tmp->next != (del))) \ - { \ - _tmp = _tmp->next; \ - } \ - if (_tmp->next) \ - { \ - _tmp->next = (del)->next; \ - } \ - } \ - } while (0) - -#define LL_COUNT(head, el, counter) LL_COUNT2(head, el, counter, next) - -#define LL_COUNT2(head, el, counter, next) \ - do \ - { \ - (counter) = 0; \ - LL_FOREACH2(head, el, next) \ - { \ - ++(counter); \ - } \ - } while (0) - -#define LL_FOREACH(head, el) LL_FOREACH2(head, el, next) - -#define LL_FOREACH2(head, el, next) for ((el) = (head); el; (el) = (el)->next) - -#define LL_FOREACH_SAFE(head, el, tmp) LL_FOREACH_SAFE2(head, el, tmp, next) - -#define LL_FOREACH_SAFE2(head, el, tmp, next) for ((el) = (head); (el) && ((tmp) = (el)->next, 1); (el) = (tmp)) - -#define LL_SEARCH_SCALAR(head, out, field, val) LL_SEARCH_SCALAR2(head, out, field, val, next) - -#define LL_SEARCH_SCALAR2(head, out, field, val, next) \ - do \ - { \ - LL_FOREACH2(head, out, next) \ - { \ - if ((out)->field == (val)) \ - break; \ - } \ - } while (0) - -#define LL_SEARCH(head, out, elt, cmp) LL_SEARCH2(head, out, elt, cmp, next) - -#define LL_SEARCH2(head, out, elt, cmp, next) \ - do \ - { \ - LL_FOREACH2(head, out, next) \ - { \ - if ((cmp(out, elt)) == 0) \ - break; \ - } \ - } while (0) - -#define LL_REPLACE_ELEM2(head, el, add, next) \ - do \ - { \ - LDECLTYPE(head) _tmp; \ - assert((head) != NULL); \ - assert((el) != NULL); \ - assert((add) != NULL); \ - (add)->next = (el)->next; \ - if ((head) == (el)) \ - { \ - (head) = (add); \ - } \ - else \ - { \ - _tmp = (head); \ - while (_tmp->next && (_tmp->next != (el))) \ - { \ - _tmp = _tmp->next; \ - } \ - if (_tmp->next) \ - { \ - _tmp->next = (add); \ - } \ - } \ - } while (0) - -#define LL_REPLACE_ELEM(head, el, add) LL_REPLACE_ELEM2(head, el, add, next) - -#define LL_PREPEND_ELEM2(head, el, add, next) \ - do \ - { \ - if (el) \ - { \ - LDECLTYPE(head) _tmp; \ - assert((head) != NULL); \ - assert((add) != NULL); \ - (add)->next = (el); \ - if ((head) == (el)) \ - { \ - (head) = (add); \ - } \ - else \ - { \ - _tmp = (head); \ - while (_tmp->next && (_tmp->next != (el))) \ - { \ - _tmp = _tmp->next; \ - } \ - if (_tmp->next) \ - { \ - _tmp->next = (add); \ - } \ - } \ - } \ - else \ - { \ - LL_APPEND2(head, add, next); \ - } \ - } while (0) - -#define LL_PREPEND_ELEM(head, el, add) LL_PREPEND_ELEM2(head, el, add, next) - -#define LL_APPEND_ELEM2(head, el, add, next) \ - do \ - { \ - if (el) \ - { \ - assert((head) != NULL); \ - assert((add) != NULL); \ - (add)->next = (el)->next; \ - (el)->next = (add); \ - } \ - else \ - { \ - LL_PREPEND2(head, add, next); \ - } \ - } while (0) - -#define LL_APPEND_ELEM(head, el, add) LL_APPEND_ELEM2(head, el, add, next) - -#ifdef NO_DECLTYPE -/* Here are VS2008 / NO_DECLTYPE replacements for a few functions */ - -#undef LL_CONCAT2 -#define LL_CONCAT2(head1, head2, next) \ - do \ - { \ - char *_tmp; \ - if (head1) \ - { \ - _tmp = (char *)(head1); \ - while ((head1)->next) \ - { \ - (head1) = (head1)->next; \ - } \ - (head1)->next = (head2); \ - UTLIST_RS(head1); \ - } \ - else \ - { \ - (head1) = (head2); \ - } \ - } while (0) - -#undef LL_APPEND2 -#define LL_APPEND2(head, add, next) \ - do \ - { \ - if (head) \ - { \ - (add)->next = head; /* use add->next as a temp variable */ \ - while ((add)->next->next) \ - { \ - (add)->next = (add)->next->next; \ - } \ - (add)->next->next = (add); \ - } \ - else \ - { \ - (head) = (add); \ - } \ - (add)->next = NULL; \ - } while (0) - -#undef LL_INSERT_INORDER2 -#define LL_INSERT_INORDER2(head, add, cmp, next) \ - do \ - { \ - if ((head) == NULL || (cmp(head, add)) >= 0) \ - { \ - (add)->next = (head); \ - (head) = (add); \ - } \ - else \ - { \ - char *_tmp = (char *)(head); \ - while ((head)->next != NULL && (cmp((head)->next, add)) < 0) \ - { \ - (head) = (head)->next; \ - } \ - (add)->next = (head)->next; \ - (head)->next = (add); \ - UTLIST_RS(head); \ - } \ - } while (0) - -#undef LL_DELETE2 -#define LL_DELETE2(head, del, next) \ - do \ - { \ - if ((head) == (del)) \ - { \ - (head) = (head)->next; \ - } \ - else \ - { \ - char *_tmp = (char *)(head); \ - while ((head)->next && ((head)->next != (del))) \ - { \ - (head) = (head)->next; \ - } \ - if ((head)->next) \ - { \ - (head)->next = ((del)->next); \ - } \ - UTLIST_RS(head); \ - } \ - } while (0) - -#undef LL_REPLACE_ELEM2 -#define LL_REPLACE_ELEM2(head, el, add, next) \ - do \ - { \ - assert((head) != NULL); \ - assert((el) != NULL); \ - assert((add) != NULL); \ - if ((head) == (el)) \ - { \ - (head) = (add); \ - } \ - else \ - { \ - (add)->next = head; \ - while ((add)->next->next && ((add)->next->next != (el))) \ - { \ - (add)->next = (add)->next->next; \ - } \ - if ((add)->next->next) \ - { \ - (add)->next->next = (add); \ - } \ - } \ - (add)->next = (el)->next; \ - } while (0) - -#undef LL_PREPEND_ELEM2 -#define LL_PREPEND_ELEM2(head, el, add, next) \ - do \ - { \ - if (el) \ - { \ - assert((head) != NULL); \ - assert((add) != NULL); \ - if ((head) == (el)) \ - { \ - (head) = (add); \ - } \ - else \ - { \ - (add)->next = (head); \ - while ((add)->next->next && ((add)->next->next != (el))) \ - { \ - (add)->next = (add)->next->next; \ - } \ - if ((add)->next->next) \ - { \ - (add)->next->next = (add); \ - } \ - } \ - (add)->next = (el); \ - } \ - else \ - { \ - LL_APPEND2(head, add, next); \ - } \ - } while (0) - -#endif /* NO_DECLTYPE */ - -/****************************************************************************** - * doubly linked list macros (non-circular) * - *****************************************************************************/ -#define DL_PREPEND(head, add) DL_PREPEND2(head, add, prev, next) - -#define DL_PREPEND2(head, add, prev, next) \ - do \ - { \ - (add)->next = (head); \ - if (head) \ - { \ - (add)->prev = (head)->prev; \ - (head)->prev = (add); \ - } \ - else \ - { \ - (add)->prev = (add); \ - } \ - (head) = (add); \ - } while (0) - -#define DL_APPEND(head, add) DL_APPEND2(head, add, prev, next) - -#define DL_APPEND2(head, add, prev, next) \ - do \ - { \ - if (head) \ - { \ - (add)->prev = (head)->prev; \ - (head)->prev->next = (add); \ - (head)->prev = (add); \ - (add)->next = NULL; \ - } \ - else \ - { \ - (head) = (add); \ - (head)->prev = (head); \ - (head)->next = NULL; \ - } \ - } while (0) - -#define DL_INSERT_INORDER(head, add, cmp) DL_INSERT_INORDER2(head, add, cmp, prev, next) - -#define DL_INSERT_INORDER2(head, add, cmp, prev, next) \ - do \ - { \ - LDECLTYPE(head) _tmp; \ - if (head) \ - { \ - DL_LOWER_BOUND2(head, _tmp, add, cmp, next); \ - DL_APPEND_ELEM2(head, _tmp, add, prev, next); \ - } \ - else \ - { \ - (head) = (add); \ - (head)->prev = (head); \ - (head)->next = NULL; \ - } \ - } while (0) - -#define DL_LOWER_BOUND(head, elt, like, cmp) DL_LOWER_BOUND2(head, elt, like, cmp, next) - -#define DL_LOWER_BOUND2(head, elt, like, cmp, next) \ - do \ - { \ - if ((head) == NULL || (cmp(head, like)) >= 0) \ - { \ - (elt) = NULL; \ - } \ - else \ - { \ - for ((elt) = (head); (elt)->next != NULL; (elt) = (elt)->next) \ - { \ - if ((cmp((elt)->next, like)) >= 0) \ - { \ - break; \ - } \ - } \ - } \ - } while (0) - -#define DL_CONCAT(head1, head2) DL_CONCAT2(head1, head2, prev, next) - -#define DL_CONCAT2(head1, head2, prev, next) \ - do \ - { \ - LDECLTYPE(head1) _tmp; \ - if (head2) \ - { \ - if (head1) \ - { \ - UTLIST_CASTASGN(_tmp, (head2)->prev); \ - (head2)->prev = (head1)->prev; \ - (head1)->prev->next = (head2); \ - UTLIST_CASTASGN((head1)->prev, _tmp); \ - } \ - else \ - { \ - (head1) = (head2); \ - } \ - } \ - } while (0) - -#define DL_DELETE(head, del) DL_DELETE2(head, del, prev, next) - -#define DL_DELETE2(head, del, prev, next) \ - do \ - { \ - assert((head) != NULL); \ - assert((del)->prev != NULL); \ - if ((del)->prev == (del)) \ - { \ - (head) = NULL; \ - } \ - else if ((del) == (head)) \ - { \ - assert((del)->next != NULL); \ - (del)->next->prev = (del)->prev; \ - (head) = (del)->next; \ - } \ - else \ - { \ - (del)->prev->next = (del)->next; \ - if ((del)->next) \ - { \ - (del)->next->prev = (del)->prev; \ - } \ - else \ - { \ - (head)->prev = (del)->prev; \ - } \ - } \ - } while (0) - -#define DL_COUNT(head, el, counter) DL_COUNT2(head, el, counter, next) - -#define DL_COUNT2(head, el, counter, next) \ - do \ - { \ - (counter) = 0; \ - DL_FOREACH2(head, el, next) \ - { \ - ++(counter); \ - } \ - } while (0) - -#define DL_FOREACH(head, el) DL_FOREACH2(head, el, next) - -#define DL_FOREACH2(head, el, next) for ((el) = (head); el; (el) = (el)->next) - -/* this version is safe for deleting the elements during iteration */ -#define DL_FOREACH_SAFE(head, el, tmp) DL_FOREACH_SAFE2(head, el, tmp, next) - -#define DL_FOREACH_SAFE2(head, el, tmp, next) for ((el) = (head); (el) && ((tmp) = (el)->next, 1); (el) = (tmp)) - -/* these are identical to their singly-linked list counterparts */ -#define DL_SEARCH_SCALAR LL_SEARCH_SCALAR -#define DL_SEARCH LL_SEARCH -#define DL_SEARCH_SCALAR2 LL_SEARCH_SCALAR2 -#define DL_SEARCH2 LL_SEARCH2 - -#define DL_REPLACE_ELEM2(head, el, add, prev, next) \ - do \ - { \ - assert((head) != NULL); \ - assert((el) != NULL); \ - assert((add) != NULL); \ - if ((head) == (el)) \ - { \ - (head) = (add); \ - (add)->next = (el)->next; \ - if ((el)->next == NULL) \ - { \ - (add)->prev = (add); \ - } \ - else \ - { \ - (add)->prev = (el)->prev; \ - (add)->next->prev = (add); \ - } \ - } \ - else \ - { \ - (add)->next = (el)->next; \ - (add)->prev = (el)->prev; \ - (add)->prev->next = (add); \ - if ((el)->next == NULL) \ - { \ - (head)->prev = (add); \ - } \ - else \ - { \ - (add)->next->prev = (add); \ - } \ - } \ - } while (0) - -#define DL_REPLACE_ELEM(head, el, add) DL_REPLACE_ELEM2(head, el, add, prev, next) - -#define DL_PREPEND_ELEM2(head, el, add, prev, next) \ - do \ - { \ - if (el) \ - { \ - assert((head) != NULL); \ - assert((add) != NULL); \ - (add)->next = (el); \ - (add)->prev = (el)->prev; \ - (el)->prev = (add); \ - if ((head) == (el)) \ - { \ - (head) = (add); \ - } \ - else \ - { \ - (add)->prev->next = (add); \ - } \ - } \ - else \ - { \ - DL_APPEND2(head, add, prev, next); \ - } \ - } while (0) - -#define DL_PREPEND_ELEM(head, el, add) DL_PREPEND_ELEM2(head, el, add, prev, next) - -#define DL_APPEND_ELEM2(head, el, add, prev, next) \ - do \ - { \ - if (el) \ - { \ - assert((head) != NULL); \ - assert((add) != NULL); \ - (add)->next = (el)->next; \ - (add)->prev = (el); \ - (el)->next = (add); \ - if ((add)->next) \ - { \ - (add)->next->prev = (add); \ - } \ - else \ - { \ - (head)->prev = (add); \ - } \ - } \ - else \ - { \ - DL_PREPEND2(head, add, prev, next); \ - } \ - } while (0) - -#define DL_APPEND_ELEM(head, el, add) DL_APPEND_ELEM2(head, el, add, prev, next) - -#ifdef NO_DECLTYPE -/* Here are VS2008 / NO_DECLTYPE replacements for a few functions */ - -#undef DL_INSERT_INORDER2 -#define DL_INSERT_INORDER2(head, add, cmp, prev, next) \ - do \ - { \ - if ((head) == NULL) \ - { \ - (add)->prev = (add); \ - (add)->next = NULL; \ - (head) = (add); \ - } \ - else if ((cmp(head, add)) >= 0) \ - { \ - (add)->prev = (head)->prev; \ - (add)->next = (head); \ - (head)->prev = (add); \ - (head) = (add); \ - } \ - else \ - { \ - char *_tmp = (char *)(head); \ - while ((head)->next && (cmp((head)->next, add)) < 0) \ - { \ - (head) = (head)->next; \ - } \ - (add)->prev = (head); \ - (add)->next = (head)->next; \ - (head)->next = (add); \ - UTLIST_RS(head); \ - if ((add)->next) \ - { \ - (add)->next->prev = (add); \ - } \ - else \ - { \ - (head)->prev = (add); \ - } \ - } \ - } while (0) -#endif /* NO_DECLTYPE */ - -/****************************************************************************** - * circular doubly linked list macros * - *****************************************************************************/ -#define CDL_APPEND(head, add) CDL_APPEND2(head, add, prev, next) - -#define CDL_APPEND2(head, add, prev, next) \ - do \ - { \ - if (head) \ - { \ - (add)->prev = (head)->prev; \ - (add)->next = (head); \ - (head)->prev = (add); \ - (add)->prev->next = (add); \ - } \ - else \ - { \ - (add)->prev = (add); \ - (add)->next = (add); \ - (head) = (add); \ - } \ - } while (0) - -#define CDL_PREPEND(head, add) CDL_PREPEND2(head, add, prev, next) - -#define CDL_PREPEND2(head, add, prev, next) \ - do \ - { \ - if (head) \ - { \ - (add)->prev = (head)->prev; \ - (add)->next = (head); \ - (head)->prev = (add); \ - (add)->prev->next = (add); \ - } \ - else \ - { \ - (add)->prev = (add); \ - (add)->next = (add); \ - } \ - (head) = (add); \ - } while (0) - -#define CDL_INSERT_INORDER(head, add, cmp) CDL_INSERT_INORDER2(head, add, cmp, prev, next) - -#define CDL_INSERT_INORDER2(head, add, cmp, prev, next) \ - do \ - { \ - LDECLTYPE(head) _tmp; \ - if (head) \ - { \ - CDL_LOWER_BOUND2(head, _tmp, add, cmp, next); \ - CDL_APPEND_ELEM2(head, _tmp, add, prev, next); \ - } \ - else \ - { \ - (head) = (add); \ - (head)->next = (head); \ - (head)->prev = (head); \ - } \ - } while (0) - -#define CDL_LOWER_BOUND(head, elt, like, cmp) CDL_LOWER_BOUND2(head, elt, like, cmp, next) - -#define CDL_LOWER_BOUND2(head, elt, like, cmp, next) \ - do \ - { \ - if ((head) == NULL || (cmp(head, like)) >= 0) \ - { \ - (elt) = NULL; \ - } \ - else \ - { \ - for ((elt) = (head); (elt)->next != (head); (elt) = (elt)->next) \ - { \ - if ((cmp((elt)->next, like)) >= 0) \ - { \ - break; \ - } \ - } \ - } \ - } while (0) - -#define CDL_DELETE(head, del) CDL_DELETE2(head, del, prev, next) - -#define CDL_DELETE2(head, del, prev, next) \ - do \ - { \ - if (((head) == (del)) && ((head)->next == (head))) \ - { \ - (head) = NULL; \ - } \ - else \ - { \ - (del)->next->prev = (del)->prev; \ - (del)->prev->next = (del)->next; \ - if ((del) == (head)) \ - (head) = (del)->next; \ - } \ - } while (0) - -#define CDL_COUNT(head, el, counter) CDL_COUNT2(head, el, counter, next) - -#define CDL_COUNT2(head, el, counter, next) \ - do \ - { \ - (counter) = 0; \ - CDL_FOREACH2(head, el, next) \ - { \ - ++(counter); \ - } \ - } while (0) - -#define CDL_FOREACH(head, el) CDL_FOREACH2(head, el, next) - -#define CDL_FOREACH2(head, el, next) for ((el) = (head); el; (el) = (((el)->next == (head)) ? NULL : (el)->next)) - -#define CDL_FOREACH_SAFE(head, el, tmp1, tmp2) CDL_FOREACH_SAFE2(head, el, tmp1, tmp2, prev, next) - -#define CDL_FOREACH_SAFE2(head, el, tmp1, tmp2, prev, next) \ - for ((el) = (head), (tmp1) = (head) ? (head)->prev : NULL; (el) && ((tmp2) = (el)->next, 1); \ - (el) = ((el) == (tmp1) ? NULL : (tmp2))) - -#define CDL_SEARCH_SCALAR(head, out, field, val) CDL_SEARCH_SCALAR2(head, out, field, val, next) - -#define CDL_SEARCH_SCALAR2(head, out, field, val, next) \ - do \ - { \ - CDL_FOREACH2(head, out, next) \ - { \ - if ((out)->field == (val)) \ - break; \ - } \ - } while (0) - -#define CDL_SEARCH(head, out, elt, cmp) CDL_SEARCH2(head, out, elt, cmp, next) - -#define CDL_SEARCH2(head, out, elt, cmp, next) \ - do \ - { \ - CDL_FOREACH2(head, out, next) \ - { \ - if ((cmp(out, elt)) == 0) \ - break; \ - } \ - } while (0) - -#define CDL_REPLACE_ELEM2(head, el, add, prev, next) \ - do \ - { \ - assert((head) != NULL); \ - assert((el) != NULL); \ - assert((add) != NULL); \ - if ((el)->next == (el)) \ - { \ - (add)->next = (add); \ - (add)->prev = (add); \ - (head) = (add); \ - } \ - else \ - { \ - (add)->next = (el)->next; \ - (add)->prev = (el)->prev; \ - (add)->next->prev = (add); \ - (add)->prev->next = (add); \ - if ((head) == (el)) \ - { \ - (head) = (add); \ - } \ - } \ - } while (0) - -#define CDL_REPLACE_ELEM(head, el, add) CDL_REPLACE_ELEM2(head, el, add, prev, next) - -#define CDL_PREPEND_ELEM2(head, el, add, prev, next) \ - do \ - { \ - if (el) \ - { \ - assert((head) != NULL); \ - assert((add) != NULL); \ - (add)->next = (el); \ - (add)->prev = (el)->prev; \ - (el)->prev = (add); \ - (add)->prev->next = (add); \ - if ((head) == (el)) \ - { \ - (head) = (add); \ - } \ - } \ - else \ - { \ - CDL_APPEND2(head, add, prev, next); \ - } \ - } while (0) - -#define CDL_PREPEND_ELEM(head, el, add) CDL_PREPEND_ELEM2(head, el, add, prev, next) - -#define CDL_APPEND_ELEM2(head, el, add, prev, next) \ - do \ - { \ - if (el) \ - { \ - assert((head) != NULL); \ - assert((add) != NULL); \ - (add)->next = (el)->next; \ - (add)->prev = (el); \ - (el)->next = (add); \ - (add)->next->prev = (add); \ - } \ - else \ - { \ - CDL_PREPEND2(head, add, prev, next); \ - } \ - } while (0) - -#define CDL_APPEND_ELEM(head, el, add) CDL_APPEND_ELEM2(head, el, add, prev, next) - -#ifdef NO_DECLTYPE -/* Here are VS2008 / NO_DECLTYPE replacements for a few functions */ - -#undef CDL_INSERT_INORDER2 -#define CDL_INSERT_INORDER2(head, add, cmp, prev, next) \ - do \ - { \ - if ((head) == NULL) \ - { \ - (add)->prev = (add); \ - (add)->next = (add); \ - (head) = (add); \ - } \ - else if ((cmp(head, add)) >= 0) \ - { \ - (add)->prev = (head)->prev; \ - (add)->next = (head); \ - (add)->prev->next = (add); \ - (head)->prev = (add); \ - (head) = (add); \ - } \ - else \ - { \ - char *_tmp = (char *)(head); \ - while ((char *)(head)->next != _tmp && (cmp((head)->next, add)) < 0) \ - { \ - (head) = (head)->next; \ - } \ - (add)->prev = (head); \ - (add)->next = (head)->next; \ - (add)->next->prev = (add); \ - (head)->next = (add); \ - UTLIST_RS(head); \ - } \ - } while (0) -#endif /* NO_DECLTYPE */ - -#endif /* UTLIST_H */