From 870fc99ff6e62e6af5594b6b9791e9fc755b90bb Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Thu, 27 Jun 2024 00:05:01 +0200 Subject: [PATCH 1/3] Clang format config --- .clang-format | 245 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 245 insertions(+) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..017c104 --- /dev/null +++ b/.clang-format @@ -0,0 +1,245 @@ +--- +Language: Cpp +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: true + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveMacros: + Enabled: true + 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: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: All +AlwaysBreakAfterReturnType: AllDefinitions +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: true +BitFieldColonSpacing: Both +BraceWrapping: + AfterCaseLabel: true + AfterClass: true + AfterControlStatement: false + AfterEnum: true + AfterExternBlock: true + AfterFunction: true + AfterNamespace: true + AfterObjCDeclaration: true + AfterStruct: true + AfterUnion: true + BeforeCatch: true + BeforeElse: true + BeforeLambdaBody: false + BeforeWhile: true + IndentBraces: true + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakAdjacentStringLiterals: true +BreakAfterAttributes: Leave +BreakAfterJavaFieldAnnotations: false +BreakArrays: true +BreakBeforeBinaryOperators: All +BreakBeforeConceptDeclarations: Always +BreakBeforeBraces: Linux +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +ColumnLimit: 120 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: false +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: false +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: 2 +IndentWrappedFunctionNames: false +InsertBraces: false +InsertNewlineAtEOF: true +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: 60 +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: false # Original: CaseInsensitive, breaks build +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: false +SpaceBeforeParensOptions: + AfterControlStatements: false + AfterForeachMacros: false + AfterFunctionDefinitionName: false + AfterFunctionDeclarationName: false + AfterIfMacros: false + AfterOverloadedOperator: false + AfterPlacementOperator: false + AfterRequiresInClause: false + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: false +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: c++03 +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseTab: Never +VerilogBreakBetweenInstancePorts: true +WhitespaceSensitiveMacros: + - BOOST_PP_STRINGIZE + - CF_SWIFT_NAME + - NS_SWIFT_NAME + - PP_STRINGIZE + - STRINGIZE +... + From 80c442e22d0d51b5e69b3f8dde558519f3472c2b Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Thu, 27 Jun 2024 00:05:21 +0200 Subject: [PATCH 2/3] Ignore driver program --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index d27f564..46ca31e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.o *.s build +driver From ef54b50d5bb24579e46e926df79def110f8f4edb Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Thu, 27 Jun 2024 00:05:49 +0200 Subject: [PATCH 3/3] Reformat and macro aliasing --- driver.c | 8 +++++--- ringbuf.c | 44 ++++++++++++++++++++++++++++++++++---------- ringbuf.h | 7 +++---- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/driver.c b/driver.c index 2bfd934..30fbf78 100644 --- a/driver.c +++ b/driver.c @@ -6,14 +6,16 @@ #define rb_size_t size_t #include "ringbuf.h" -int main(void) { +int +main(void) +{ struct RingBuf rb; rb_init(&rb, 10, malloc, sizeof(int)); - int data[] = {1, 2, 3, 4, 5}; + int data[] = { 1, 2, 3, 4, 5 }; rb_push(&rb, (void **)data, 5, memcpy); // rb_destroy(&rb, free); return 0; -} \ No newline at end of file +} diff --git a/ringbuf.c b/ringbuf.c index bf9b85b..6039967 100644 --- a/ringbuf.c +++ b/ringbuf.c @@ -1,9 +1,17 @@ /* SPDX-License-Identifier: MIT */ +#include +#include +#include + #include "ringbuf.h" -void rb_init(struct RingBuf *rb, rb_size_t capacity, void *(*alloc)(rb_size_t), - rb_size_t struct_size) { +#define ALLOC_T void *(*alloc)(rb_size_t) +#define MEMCPY_T void *(*memcpy)(void *, const void *, rb_size_t) + +void +rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T, rb_size_t struct_size) +{ rb->struct_size = struct_size; rb->capacity = capacity; rb->write_idx = 0; @@ -11,21 +19,37 @@ void rb_init(struct RingBuf *rb, rb_size_t capacity, void *(*alloc)(rb_size_t), rb->buffer = alloc(capacity * struct_size); /* Calloc? */ } -void rb_destroy(struct RingBuf *rb, rb_size_t(free)(void *)) { +void +rb_destroy(struct RingBuf *rb, rb_size_t(free)(void *)) +{ free(rb->buffer); } -enum WriteResult rb_push(struct RingBuf *rb, void *data[], rb_size_t amount, - void *(*memcpy)(void *, const void *, rb_size_t)) { - if (rb->write_idx + amount >= rb->capacity) { +enum WriteResult +rb_push(struct RingBuf *rb, void *data[], rb_size_t amount, MEMCPY_T) +{ + if(rb->write_idx + amount >= rb->capacity) { return CollisionError; } - for (rb_size_t i = 0; i < amount; i++) { - memcpy(rb->buffer + rb->write_idx * rb->struct_size, data[i], - rb->struct_size); + printf("write_idx: %d\n", rb->write_idx); + printf("amount: %d\n", amount); + + for(rb_size_t i = 0; i < amount; i++) { + printf("memcpy: %d, write_idx: %d\n", i, rb->write_idx); + // write to the buffer + rb->buffer[rb->write_idx] = data[i]; + // memcpy(rb->buffer + rb->write_idx * rb->struct_size, data[i], rb->struct_size); + rb->write_idx = (rb->write_idx + 1) % rb->capacity; } + // Read it back to stdout + for(rb_size_t i = 0; i < amount; i++) { + printf("read_idx: %d\n", rb->read_idx); + printf("data: %d\n", *(int *)rb->buffer[rb->read_idx]); + rb->read_idx = (rb->read_idx + 1) % rb->capacity; + } + return Ok; -} \ No newline at end of file +} diff --git a/ringbuf.h b/ringbuf.h index 9c28559..8ef2569 100644 --- a/ringbuf.h +++ b/ringbuf.h @@ -14,18 +14,17 @@ struct RingBuf { rb_size_t capacity; /* The physical capacity of the entire ringbuf */ rb_size_t write_idx; /* The write head */ rb_size_t read_idx; /* THe read head */ - void **buffer; /* The actual data */ + void **buffer; /* The actual data */ }; enum WriteResult { CollisionError, Ok }; /** Initialize the ring buffer */ -void rb_init(struct RingBuf *rb, rb_size_t capacity, void *(*alloc)(rb_size_t), - rb_size_t struct_size); +void rb_init(struct RingBuf *rb, rb_size_t capacity, void *(*alloc)(rb_size_t), rb_size_t struct_size); /** Insert data to the ring buffer */ enum WriteResult rb_push(struct RingBuf *rb, void *data[], rb_size_t amount, void *(*memcpy)(void *, const void *, rb_size_t)); /** Read data from the ring buffer */ -// void *rb_read(struct RingBuf *rb, int amount); \ No newline at end of file +// void *rb_read(struct RingBuf *rb, int amount);