Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Aug 22, 2023
2 parents d773928 + d8cf3b2 commit 465b2fb
Show file tree
Hide file tree
Showing 24 changed files with 943 additions and 232 deletions.
4 changes: 2 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ AlignConsecutiveBitFields:
AlignConsecutiveDeclarations: None
AlignEscapedNewlines: Right
AlignOperands: Align
SortIncludes: false
SortIncludes: true
InsertBraces: true # Control statements must have curly brackets
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
url = https://github.com/arsenm/sanitizers-cmake.git
[submodule "third_party/cmake-modules"]
path = third_party/cmake-modules
url = https://github.com/bilke/cmake-modules.git
url = https://github.com/bilke/cmake-modules.git
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"string.h": "c",
"lwevt_opt.h": "c",
"stdatomic.h": "c",
"lwrb.h": "c"
"lwrb.h": "c",
"*.tcc": "c"
},
"esbonio.sphinx.confDir": ""
}
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

## Develop

## v3.0.0

- Added macros for optional STDATOMIC. Global `-DLWRB_DISABLE_ATOMIC` macro will disable C11 `<stdatomic.h>` functionality.
- Add `lwrb_move` and `lwrb_overwrite`
- Fix `lwrb_find` which failed to properly search for tokens at corner cases

## v3.0.0-RC1

- Split CMakeLists.txt files between library and executable
- Change license year to 2022
- Update code style with astyle
- Minimum required version is C11, with requirement of `stdatomic.h` library
- Add `.clang-format` draft

## v2.0.3

Expand Down Expand Up @@ -53,4 +60,4 @@

## v1.0.0

- First stable release
- First stable release
81 changes: 38 additions & 43 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,46 +1,41 @@
cmake_minimum_required(VERSION 3.22)

# Setup project
project(LwLibPROJECT)

# -------------------------------------------------
# This CMakeLists.txt is used only if it is a top-level file.
# Purpose of it is to be able to compile project in standalone way only
#
# When library sources are to be included in another project
# user shall use /lwrb/CMakeLists.txt instead
if (NOT PROJECT_IS_TOP_LEVEL)
message(FATAL_ERROR "This CMakeLists.txt can only be used as top-level. Use /lwrb/CMakeLists.txt for library include purpose")
endif()

# Set as executable
add_executable(${PROJECT_NAME})

# Add key executable block
target_sources(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_LIST_DIR}/dev/main.c
)

# Add key include paths
target_include_directories(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_LIST_DIR}/dev
)

# Compilation definition information
target_compile_definitions(${PROJECT_NAME} PUBLIC
WIN32
_DEBUG
CONSOLE
LWRB_DEV
)

# Compiler options
target_compile_options(${PROJECT_NAME} PRIVATE
-Wall
-Wextra
-Wpedantic
)

# Add subdir with lwrb and link to project
add_subdirectory("lwrb" lwrb)
target_link_libraries(${PROJECT_NAME} lwrb)
project(LwLibPROJECT C)

if(NOT PROJECT_IS_TOP_LEVEL)
add_subdirectory(lwrb)
else()
# Set as executable
add_executable(${PROJECT_NAME})

# Add key executable block
target_sources(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_LIST_DIR}/dev/main.c
)

# Add key include paths
target_include_directories(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_LIST_DIR}/dev
)

# Compilation definition information
target_compile_definitions(${PROJECT_NAME} PUBLIC
WIN32
_DEBUG
CONSOLE
LWRB_DEV
)

# Compiler options
target_compile_options(${PROJECT_NAME} PRIVATE
-Wall
-Wextra
-Wpedantic
)

# Add subdir with lwrb and link to project
add_subdirectory(lwrb)
target_link_libraries(${PROJECT_NAME} lwrb)
target_link_libraries(${PROJECT_NAME} lwrb_ex)
endif()
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Tilen MAJERLE
Copyright (c) 2023 Tilen MAJERLE

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Library provides generic FIFO ring buffer implementation.

## Features

* Written in ANSI C99, compatible with ``size_t`` for size data types
* Written in C (C11), compatible with ``size_t`` for size data types
* Platform independent default code - with restrictions for smaller CPU architectures (`< sizeof(size_t)`)
* FIFO (First In First Out) buffer implementation
* No dynamic memory allocation, data is static array
Expand All @@ -24,7 +24,7 @@ Library provides generic FIFO ring buffer implementation.
Fresh contributions are always welcome. Simple instructions to proceed::

1. Fork Github repository
2. Respect [C style & coding rules](https://github.com/MaJerle/c-code-style) used by the library
2. Follow [C style & coding rules](https://github.com/MaJerle/c-code-style) already used in the project
3. Create a pull request to develop branch with new features or bug fixes

Alternatively you may:
Expand Down
168 changes: 141 additions & 27 deletions dev/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ uint8_t tmp[8];
void
my_buff_evt_fn(lwrb_t* buff, lwrb_evt_type_t type, size_t len) {
(void)buff;
(void)len;
switch (type) {
case LWRB_EVT_RESET:
printf("[EVT] Buffer reset event!\r\n");
break;
case LWRB_EVT_READ:
printf("[EVT] Buffer read event: %d byte(s)!\r\n", (int)len);
break;
case LWRB_EVT_WRITE:
printf("[EVT] Buffer write event: %d byte(s)!\r\n", (int)len);
break;
case LWRB_EVT_RESET: printf("[EVT] Buffer reset event!\r\n"); break;
case LWRB_EVT_READ: printf("[EVT] Buffer read event: %d byte(s)!\r\n", (int)len); break;
case LWRB_EVT_WRITE: printf("[EVT] Buffer write event: %d byte(s)!\r\n", (int)len); break;
default: break;
}
}
Expand All @@ -34,31 +29,150 @@ main() {

/* Init buffer */
lwrb_init(&buff, lwrb_data, sizeof(lwrb_data));
lwrb_set_evt_fn(&buff, my_buff_evt_fn);

lwrb_write(&buff, "abc", 3);
lwrb_write(&buff, "abc", 3);
lwrb_write(&buff, "abc", 3);
len = lwrb_read(&buff, tmp, 9);
printf("Read/Write test\r\n");
{
uint8_t rw_buff[8];

#define RW_TEST(_w_exp_, _r_exp_, _rw_len_, _rw_exp_len_) \
do { \
printf("W ptr: %u, R ptr: %u, R/W len: %u, as_expected: %u\r\n", (unsigned)buff.w, (unsigned)buff.r, \
(unsigned)(_rw_len_), \
(unsigned)(buff.w == (_w_exp_) && buff.r == (_r_exp_) && (_rw_len_) == (_rw_exp_len_))); \
} while (0)

lwrb_reset(&buff);
len = lwrb_write(&buff, "abc", 3); /* Write 3 bytes */
RW_TEST(3, 0, len, 3);
len = lwrb_write(&buff, "abc", 3); /* Write 3 bytes */
RW_TEST(6, 0, len, 3);
len = lwrb_read(&buff, rw_buff, 3); /* Read 3 bytes */
RW_TEST(6, 3, len, 3);
len = lwrb_read(&buff, rw_buff, 4); /* Read 4 bytes */
RW_TEST(6, 6, len, 3);

len = lwrb_write(&buff, "abc", 3); /* Write 3 bytes -> buffer should go over */
RW_TEST(0, 6, len, 3);

#undef RW_TEST
}

printf("Overwrite test\r\n");
{
#define OVERWRITE_TEST(_exp_content_, _exp_len_) \
do { \
len = lwrb_peek(&buff, 0, tmp, buff.size); \
printf("overwrite data read: %.*s, len: %u, as_expected: %u\r\n", (int)len, tmp, (unsigned)len, \
(unsigned)(strncmp((_exp_content_), (const void*)tmp, len) == 0 && len == (_exp_len_))); \
} while (0)

buff.r = 0;
buff.w = 0;
memset(lwrb_get_linear_block_write_address(&buff), 'A', lwrb_get_linear_block_write_length(&buff));
lwrb_advance(&buff, lwrb_get_linear_block_write_length(&buff));
/* Test overwrite */
lwrb_reset(&buff);
lwrb_write(&buff, "abcdef", 6); /* Initial data */
OVERWRITE_TEST("abcdef", 6);

buff.r = 2;
buff.w = 0;
memset(lwrb_get_linear_block_write_address(&buff), 'B', lwrb_get_linear_block_write_length(&buff));
lwrb_advance(&buff, lwrb_get_linear_block_write_length(&buff));
lwrb_overwrite(&buff, "0", 1);
OVERWRITE_TEST("abcdef0", 7);

lwrb_overwrite(&buff, "1", 1);
OVERWRITE_TEST("abcdef01", 8);

lwrb_overwrite(&buff, "2", 1);
OVERWRITE_TEST("bcdef012", 8);

lwrb_overwrite(&buff, "3", 1);
OVERWRITE_TEST("cdef0123", 8);

lwrb_overwrite(&buff, "4", 1);
OVERWRITE_TEST("def01234", 8);

lwrb_overwrite(&buff, "5", 1);
OVERWRITE_TEST("ef012345", 8);

/* Bigger write which will completely change the buffer structure */
lwrb_overwrite(&buff, "lwrb_new_test_structure", 23);
OVERWRITE_TEST("tructure", 8);
#undef OVERWRITE_TEST
}

buff.r = 3;
buff.w = 3;
memset(lwrb_get_linear_block_write_address(&buff), 'C', lwrb_get_linear_block_write_length(&buff));
lwrb_advance(&buff, lwrb_get_linear_block_write_length(&buff));
printf("Move test\r\n");
{
#define MOVE_TEST(_exp_content_, _exp_move_len_, _exp_buff_len_) \
do { \
size_t move_len; \
move_len = lwrb_move(&dst, &src); \
len = lwrb_peek(&dst, 0, tmp, dst.size); \
printf("move data: len: %d, dest data: %.*s, as_expected: %u\r\n", (int)len, (int)len, tmp, \
(unsigned)(strncmp((_exp_content_), (const void*)tmp, len) == 0 && move_len == (_exp_move_len_) \
&& len == (_exp_buff_len_))); \
} while (0)

lwrb_reset(&buff);
lwrb_t src, dst;
uint8_t src_data[16], dst_data[8];
lwrb_init(&src, src_data, sizeof(src_data));
lwrb_init(&dst, dst_data, sizeof(dst_data));

lwrb_reset(&src);
lwrb_reset(&dst);
lwrb_write(&src, "012345", 6);
MOVE_TEST("012345", 6, 6);

lwrb_reset(&src);
lwrb_reset(&dst);
lwrb_write(&src, "0123456789ABCDEF", 16);
MOVE_TEST("0123456", 7, 7);

lwrb_reset(&src);
lwrb_reset(&dst);
lwrb_write(&src, "0123456789ABCDEF", 16);
lwrb_write(&dst, "TT", 2);
MOVE_TEST("TT01234", 5, 7);

#undef MOVE_TEST
}

(void)len;

printf("Find test\r\n");
{
#define FIND_TEST(_bts_, _bts_len_, _start_offset_, _exp_result_) \
do { \
size_t found_idx; \
uint8_t found; \
found = lwrb_find(&buff, (_bts_), (_bts_len_), (_start_offset_), &found_idx); \
printf("Find \"%s\" (len %d), start_offset: %d, found_index: %d; Found: %d; As expected: %d\r\n", (_bts_), \
(_bts_len_), (_start_offset_), (int)found_idx, (int)found, (int)(!!found == !!(_exp_result_))); \
} while (0)

/* Prepare buffer and write data */
lwrb_reset(&buff);
lwrb_write(&buff, "12345678", 8);

FIND_TEST("123", 3, 0, 1); /* Must find it */
FIND_TEST("456", 3, 0, 1); /* Must find it */
FIND_TEST("123", 3, 1, 0); /* Must not find it - start offset is later */
FIND_TEST("678", 3, 0, 1);

/* Restart by setting write and read as empty with offset */
buff.w = 6;
buff.r = 6;
lwrb_write(&buff, "12345678", 8);

FIND_TEST("123", 3, 0, 1); /* Must find it */
FIND_TEST("456", 3, 0, 1); /* Must find it */
FIND_TEST("123", 3, 1, 0); /* Must not find it - start offset is later */

/* Restart by setting write and read as empty with offset */
/* This should generate data for search in overflow mode */
buff.w = 8;
buff.r = 8;
lwrb_write(&buff, "12345678", 8);

FIND_TEST("1234", 3, 0, 1); /* Must find it */
FIND_TEST("4567", 3, 0, 1); /* Must find it */
FIND_TEST("1234", 3, 1, 0); /* Must not find it - start offset is later */

#undef FIND_TEST
}
return 0;
}
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# -- Project information -----------------------------------------------------

project = 'LwRB'
copyright = '2022, Tilen MAJERLE'
copyright = '2023, Tilen MAJERLE'
author = 'Tilen MAJERLE'

# Try to get branch at which this is running
Expand Down
2 changes: 1 addition & 1 deletion docs/get-started/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Next step is to add the library to the project, by means of source files to comp

* Copy ``lwrb`` folder to your project, it contains library files
* Add ``lwrb/src/include`` folder to `include path` of your toolchain. This is where `C/C++` compiler can find the files during compilation process. Usually using ``-I`` flag
* Add source files from ``lwrb/src/`` folder to toolchain build. These files are built by `C/C++` compilery
* Add source files from ``lwrb/src/`` folder to toolchain build. These files are built by `C/C++` compiler. CMake configuration comes with the library, allows users to include library in the project as **subdirectory** and **library**.y
* Build the project

Minimal example code
Expand Down
Loading

0 comments on commit 465b2fb

Please sign in to comment.