Compare commits

..

No commits in common. "ef54b50d5bb24579e46e926df79def110f8f4edb" and "7eeb281a9d990423c01efb641bd34212499503f1" have entirely different histories.

5 changed files with 17 additions and 288 deletions

View file

@ -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
...

1
.gitignore vendored
View file

@ -1,4 +1,3 @@
*.o *.o
*.s *.s
build build
driver

View file

@ -6,9 +6,7 @@
#define rb_size_t size_t #define rb_size_t size_t
#include "ringbuf.h" #include "ringbuf.h"
int int main(void) {
main(void)
{
struct RingBuf rb; struct RingBuf rb;
rb_init(&rb, 10, malloc, sizeof(int)); rb_init(&rb, 10, malloc, sizeof(int));

View file

@ -1,17 +1,9 @@
/* SPDX-License-Identifier: MIT */ /* SPDX-License-Identifier: MIT */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "ringbuf.h" #include "ringbuf.h"
#define ALLOC_T void *(*alloc)(rb_size_t) void rb_init(struct RingBuf *rb, rb_size_t capacity, void *(*alloc)(rb_size_t),
#define MEMCPY_T void *(*memcpy)(void *, const void *, rb_size_t) rb_size_t struct_size) {
void
rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T, rb_size_t struct_size)
{
rb->struct_size = struct_size; rb->struct_size = struct_size;
rb->capacity = capacity; rb->capacity = capacity;
rb->write_idx = 0; 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? */ rb->buffer = alloc(capacity * struct_size); /* Calloc? */
} }
void void rb_destroy(struct RingBuf *rb, rb_size_t(free)(void *)) {
rb_destroy(struct RingBuf *rb, rb_size_t(free)(void *))
{
free(rb->buffer); free(rb->buffer);
} }
enum WriteResult enum WriteResult rb_push(struct RingBuf *rb, void *data[], rb_size_t amount,
rb_push(struct RingBuf *rb, void *data[], rb_size_t amount, MEMCPY_T) void *(*memcpy)(void *, const void *, rb_size_t)) {
{
if (rb->write_idx + amount >= rb->capacity) { if (rb->write_idx + amount >= rb->capacity) {
return CollisionError; return CollisionError;
} }
printf("write_idx: %d\n", rb->write_idx);
printf("amount: %d\n", amount);
for (rb_size_t i = 0; i < amount; i++) { for (rb_size_t i = 0; i < amount; i++) {
printf("memcpy: %d, write_idx: %d\n", i, rb->write_idx); memcpy(rb->buffer + rb->write_idx * rb->struct_size, data[i],
// write to the buffer rb->struct_size);
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; 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; return Ok;
} }

View file

@ -20,7 +20,8 @@ struct RingBuf {
enum WriteResult { CollisionError, Ok }; enum WriteResult { CollisionError, Ok };
/** Initialize the ring buffer */ /** 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 */ /** Insert data to the ring buffer */
enum WriteResult rb_push(struct RingBuf *rb, void *data[], rb_size_t amount, enum WriteResult rb_push(struct RingBuf *rb, void *data[], rb_size_t amount,