Compare commits

...

3 commits

Author SHA1 Message Date
Imbus
ef54b50d5b Reformat and macro aliasing 2024-06-27 00:05:49 +02:00
Imbus
80c442e22d Ignore driver program 2024-06-27 00:05:21 +02:00
Imbus
870fc99ff6 Clang format config 2024-06-27 00:05:01 +02:00
5 changed files with 288 additions and 17 deletions

245
.clang-format Normal file
View file

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

1
.gitignore vendored
View file

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

View file

@ -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;
}
}

View file

@ -1,9 +1,17 @@
/* SPDX-License-Identifier: MIT */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#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;
}
}

View file

@ -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);
// void *rb_read(struct RingBuf *rb, int amount);