commit 776f21257adea5750e2b9cd07b36f8d4d7f96e37 Author: retoor Date: Tue Jan 14 18:53:15 2025 +0100 New repository. diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..de7da58 --- /dev/null +++ b/.clang-format @@ -0,0 +1,192 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveMacros: None +AlignConsecutiveAssignments: None +AlignConsecutiveBitFields: None +AlignConsecutiveDeclarations: None +AlignEscapedNewlines: Right +AlignOperands: Align +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortEnumsOnASingleLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortLambdasOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: true +BreakBeforeBraces: Attach +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 140 +CommentPragmas: '^ IWYU pragma:' +QualifierAlignment: Leave +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DeriveLineEnding: true +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +PackConstructorInitializers: BinPack +BasedOnStyle: '' +ConstructorInitializerAllOnOneLineOrOnePerLine: false +AllowAllConstructorInitializersOnNextLine: true +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 +IndentCaseLabels: false +IndentCaseBlocks: false +IndentGotoLabels: true +IndentPPDirectives: None +IndentExternBlock: AfterExternBlock +IndentRequires: false +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertTrailingCommas: None +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +LambdaBodyIndentation: Signature +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PenaltyIndentedWhitespace: 0 +PointerAlignment: Right +PPIndentWidth: -1 +ReferenceAlignment: Pointer +ReflowComments: true +RemoveBracesLLVM: false +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDefinitionName: false + AfterFunctionDeclarationName: false + AfterIfMacros: true + AfterOverloadedOperator: false + BeforeNonEmptyParentheses: false +SpaceAroundPointerQualifiers: Default +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpaceBeforeSquareBrackets: false +BitFieldColonSpacing: Both +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseCRLF: false +UseTab: Never +WhitespaceSensitiveMacros: + - STRINGIZE + - PP_STRINGIZE + - BOOST_PP_STRINGIZE + - NS_SWIFT_NAME + - CF_SWIFT_NAME +... + diff --git a/.github/workflows/make-rlib-single-platform.yml b/.github/workflows/make-rlib-single-platform.yml new file mode 100644 index 0000000..884fa1e --- /dev/null +++ b/.github/workflows/make-rlib-single-platform.yml @@ -0,0 +1,29 @@ +name: Build and run rrex2 + + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +env: + BUILD_TYPE: Release + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Install dependencies + run: sudo apt update && sudo apt install clang-format -y + - name: Build + working-directory: ${{github.workspace}} + run: make + + - name: Install + working-directory: ${{github.workspace}} + run: make install + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..64d7627 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +a.out +.vscore +.bzr +.bzrignore +.vscode +C-Data-Structs +rasmd +*yur* +yur* +hashmap.h +primes.h +errors.h +*.tty +*.o +build +.history + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..773eb40 --- /dev/null +++ b/Makefile @@ -0,0 +1,213 @@ +# Compiler +CC = gcc + +# Compiler flags +CFLAGS = -Wall -Wextra -Werror -Ofast -std=c2x -lm -lrt -pthread -fPIC +LDFLAGS = -lm + +# -D_POSIX_C_SOURCE=199309L +all: clear test_rbuffer test_rliza test_rnet test_rmalloc test_rtemp test_rjson test_rrex4 test_rstring_list test_rhttp test_rtime test_arena test_rtree test_rstring test_rlexer test_rrex3 test_rio test_rhashtable test_rkeytable test_rterminal test_rmerge run_rmerge format_all build format_all test_rlib + + + + +format_all: format_rlib_h + clang-format *.c *.h *.cpp -i --sort-includes=false + +format_rlib_h: + clang-format rlib.h build/rlib.h -i --sort-includes --verbose + +clear: + -@rm -rf build + @mkdir build + @echo "Build directory emptied" + +rebuild: clear all + +test_rbuffer: build_rbuffer run_rbuffer +build_rbuffer: + $(CC) $(CFLAGS) rbuffer.c -o ./build/rbuffer +run_rbuffer: + ./build/rbuffer test + +test_rliza: build_rliza run_rliza +build_rliza: + $(CC) $(CFLAGS) rliza.c -o ./build/rliza +run_rliza: + ./build/rliza test + +test_rnet: build_rnet run_rnet +build_rnet: + $(CC) $(CFLAGS) rnet.c -o ./build/rnet +run_rnet: + ./build/rnet test + +test_rtemp: build_rtemp run_rtemp +build_rtemp: + $(CC) $(CFLAGS) rtemp.c -o ./build/rtemp +run_rtemp: + ./build/rtemp + +test_rjson: build_rjson run_rjson +build_rjson: + $(CC) $(CFLAGS) rjson.c -o ./build/rjson +run_rjson: + ./build/rjson + + +test_rstring_list: build_rstring_list run_rstring_list +build_rstring_list: + $(CC) $(CFLAGS) rstring_list.c -o ./build/rstring_list +run_rstring_list: + ./build/rstring_list + +test_rhttp: build_rhttp run_rhttp +build_rhttp: + $(CC) $(CFLAGS) rhttp.c -o ./build/rhttp +run_rhttp: + ./build/rhttp + +test_rautocomplete: build_rautocomplete run_rautocomplete +build_rautocomplete: + $(CC) $(CFLAGS) rautocomplete.c -o ./build/rautocomplete +run_rautocomplete: + ./build/rautocomplete + +test_rlexer: build_rlexer run_rlexer +build_rlexer: + $(CC) $(CFLAGS) rlexer.c -o ./build/rlexer +run_rlexer: + ./build/rlexer + + +test_rrex4: build_rrex4 run_rrex4 +build_rrex4: + $(CC) $(CFLAGS) rrex4.c -o ./build/rrex4 +run_rrex4: + ./build/rrex4 + +test_rrex3: build_rrex3 run_rrex3 +build_rrex3: + $(CC) $(CFLAGS) rrex3.c -o ./build/rrex3 +run_rrex3: + ./build/rrex3 + +test_rio: build_rio run_rio +build_rio: + $(CC) $(CFLAGS) rio.c -o ./build/rio +run_rio: + ./build/rio + + + +test_rterm: build_rterm run_rterm +build_rterm: + $(CC) $(CFLAGS) rterm.c -o ./build/rterm +run_rterm: + ./build/rterm + +test_rterminal: build_rterminal run_rterminal +build_rterminal: + $(CC) $(CFLAGS) rterminal.c -o ./build/rterminal +run_rterminal: + ./build/rterminal + +test_rmerge: build_rmerge run_rmerge +build_rmerge: + $(CC) $(CFLAGS) rmerge.c -o ./build/rmerge +run_rmerge: + ./build/rmerge _rlib.h > ./build/rlib.h + cp ./build/rlib.h ./rlib.h + +test_rprint: build_rprint run_rprint +build_rprint: + $(CC) $(CFLAGS) rprint.c -o ./build/rprint +run_rprint: + ./build/rprint + +test_rstring: build_rstring run_rstring +build_rstring: + $(CC) $(CFLAGS) rstring.c -o ./build/rstring +run_rstring: + ./build/rstring + +test_rbench: build_rbench run_rbench +build_rbench: + $(CC) $(CFLAGS) rbench.c -o ./build/rbench +run_rbench: + ./build/rbench + +test_rbench_cpp: build_rbench_cpp run_rbench_cpp +build_rbench_cpp: + g++ $(CFLAGS) ./C-Data-Structs/src/primes.c rbench.cpp -o ./build/rbench.cpp -I./C-Data-Structs/include +run_rbench_cpp: + ./build/rbench.cpp + +test_yurii_cpp: format_all build_yurii_cpp run_yurii_cpp +build_yurii_cpp: + rmerge rbench.cpp > yurii_hashmap.cpp + g++ C-Data-Structs/src/primes.c yurii_hashmap.cpp -o ./build/yurii_hashmap.cpp -IC-Data-Structs/include +run_yurii_cpp: + ./build/yurii_hashmap.cpp + +test_rmalloc: build_rmalloc run_rmalloc +build_rmalloc: + $(CC) $(CFLAGS) rmalloc.c -o ./build/rmalloc +run_rmalloc: + ./build/rmalloc + +test_rtime: build_rtime run_rtime +build_rtime: + $(CC) $(CFLAGS) rtime.c -o ./build/rtime +run_rtime: + ./build/rtime + +test_arena: build_arena run_arena +build_arena: + $(CC) $(CFLAGS) arena.c -o ./build/arena +run_arena: + ./build/arena + +test_rlib: build_rlib run_rlib +build_rlib: + $(CC) $(CFLAGS) rlib.c -o ./build/rlib +run_rlib: + ./build/rlib test + +test_rtree: build_rtree run_rtree +build_rtree: + $(CC) $(CFLAGS) rtree.c -o ./build/rtree +run_rtree: + ./build/rtree + +test_rhashtable: build_rhashtable run_rhashtable +build_rhashtable: + $(CC) $(CFLAGS) rhashtable.c -o ./build/rhashtable +run_rhashtable: + ./build/rhashtable + +test_rkeytable: build_rkeytable run_rkeytable +build_rkeytable: + $(CC) $(CFLAGS) rkeytable.c -o ./build/rkeytable +run_rkeytable: + ./build/rkeytable + +build: format_rlib_h + cp ./clean build/clean + cp rlib.h rlibrlibso.c + @gcc rlibrlibso.c $(CFLAGS) -shared -o ./build/librlib.so + @echo "Built a new rlib.so" + @gcc rlibso.c -L./build -Wl,-rpath=. -lrlib -o ./build/rlibso ${CFLAGS} + @cd ./build && ./rlibso + @echo "Build succesful" + +install: + sudo cp ./build/rlib.h /usr/include/rlib.h + sudo cp ./build/rmerge /usr/local/bin/rmerge + sudo cp ./build/clean /usr/local/bin/clean + sudo cp ./build/rlib /usr/local/bin/rlib + +publish: + brz add + brz commit + brz push lp:rlib diff --git a/README.md b/README.md new file mode 100644 index 0000000..31be88e --- /dev/null +++ b/README.md @@ -0,0 +1,94 @@ +# RLIB + +Collection of frequently used C functions by me. + +It contains + - An advanced benchmark system. + - An alternative to printf supporting printing of time between messages for e.g. benchmarking. + and colors. + - Super fast tree map (much faster than hash table). + - Stdout redirection. + - Terminal progress bar. + - Multi purpose lexer. + - Custom malloc for counting allocations and free's. + - Simple test library that works like assert that also checks memory if rmalloc is used and + creates summary and provides exit code. + - Several time functions supporting nano seconds. + - Several math functions for if not available by std (on termux for example). + - Arena blazing fast memory. + +## Extensive tested and benchmarked +All files get merged together to one file called `rlib.c` that can be used as a header file. +The result is a single header framework. +![Gif of build process](build.gif) + +## ENVIRONMENT VARIABLES + +### Disabling color +Set env RDISABLE_COLORS = 1 to disable color on stdout. stderr will still have colors for gui. + +## Compiler and Flags +- **`CC = gcc`**: Specifies the C compiler (GCC). +- **`CFLAGS = -Wall -Wextra -Ofast`**: Compiler flags: + - `-Wall`: Enable all warnings. + - `-Wextra`: Enable extra warnings. + - `-Ofast`: Optimize for speed. +- **`LDFLAGS = -lm`**: Linker flags to link with the math library. + +## Main Targets + +### `all:` +Runs the following steps in sequence: +- Testing (`test_*` targets). +- Formatting code (`format_all`). +- Building the project (`build`). +- Installing executables (`install`). + +### `format_all:` +Runs `clang-format` to format all `.c`, `.h`, and `.cpp` files in the directory. + +### `build:` +Builds the `rlib` shared library (`librlib.so`) and an executable `rlibso` that uses this library: +- Compiles `rlib.c` into `librlib.so`. +- Compiles `rlibso.c` linking it with `librlib.so`. +- Runs the `rlibso` executable. + +### `install:` +Installs the `rmerge` and `clean` executables to `/usr/bin` using `sudo`. + +## Testing Targets (`test_*`) + +Each `test_*` target follows these steps: + +### `build_*:` +Compiles a specific `.c` file into an executable in the `./build` directory. + +### `run_*:` +Runs the corresponding executable. + +Example for `rlexer`: +- **`build_rlexer:`** Compiles `rlexer.c` to `./build/rlexer`. +- **`run_rlexer:`** Runs `./build/rlexer`. + +## Backup + +### `backup:` +Compresses the source files, Makefile, and additional project files into a `rlib.rzip` archive. + +## Special Cases + +### `test_rbench_cpp:` +Builds and runs a C++ benchmark program: +- Compiles `rbench.cpp` along with other necessary files. +- Runs the resulting executable. + +## Other Targets + +### `format_rlib_c:` +Specifically formats the `rlib.c` file. + +### `build_*:` +Targets that compile source files into executables. + +### `run_*:` +Targets that run the compiled executables. \ No newline at end of file diff --git a/_rlib.h b/_rlib.h new file mode 100644 index 0000000..7dec69c --- /dev/null +++ b/_rlib.h @@ -0,0 +1,45 @@ +#include "license.h" +#ifndef RLIB_H +#define RLIB_H +// BEGIN OF RLIB + +/* + * Line below will be filtered by rmerge +