diff --git a/.clang-format b/.clang-format deleted file mode 100644 index 017c104..0000000 --- a/.clang-format +++ /dev/null @@ -1,245 +0,0 @@ ---- -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 -... - diff --git a/.gitignore b/.gitignore index 46ca31e..d27f564 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ *.o *.s build -driver diff --git a/driver.c b/driver.c index 30fbf78..2bfd934 100644 --- a/driver.c +++ b/driver.c @@ -6,16 +6,14 @@ #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 6039967..bf9b85b 100644 --- a/ringbuf.c +++ b/ringbuf.c @@ -1,17 +1,9 @@ /* SPDX-License-Identifier: MIT */ -#include -#include -#include - #include "ringbuf.h" -#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) -{ +void rb_init(struct RingBuf *rb, rb_size_t capacity, void *(*alloc)(rb_size_t), + rb_size_t struct_size) { rb->struct_size = struct_size; rb->capacity = capacity; rb->write_idx = 0; @@ -19,37 +11,21 @@ rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T, rb_size_t struct_size) 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, MEMCPY_T) -{ - if(rb->write_idx + amount >= rb->capacity) { +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) { return CollisionError; } - 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); - + for (rb_size_t i = 0; i < amount; 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 8ef2569..9c28559 100644 --- a/ringbuf.h +++ b/ringbuf.h @@ -14,17 +14,18 @@ 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); +// void *rb_read(struct RingBuf *rb, int amount); \ No newline at end of file