Skip to content

Commit 465b2fb

Browse files
committed
Merge branch 'develop'
2 parents d773928 + d8cf3b2 commit 465b2fb

File tree

24 files changed

+943
-232
lines changed

24 files changed

+943
-232
lines changed

.clang-format

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ AlignConsecutiveBitFields:
1616
AlignConsecutiveDeclarations: None
1717
AlignEscapedNewlines: Right
1818
AlignOperands: Align
19-
SortIncludes: false
19+
SortIncludes: true
2020
InsertBraces: true # Control statements must have curly brackets
2121
AlignTrailingComments: true
2222
AllowAllArgumentsOnNextLine: true
2323
AllowAllParametersOfDeclarationOnNextLine: true
2424
AllowShortEnumsOnASingleLine: true
2525
AllowShortBlocksOnASingleLine: Empty
26-
AllowShortCaseLabelsOnASingleLine: false
26+
AllowShortCaseLabelsOnASingleLine: true
2727
AllowShortFunctionsOnASingleLine: All
2828
AllowShortLambdasOnASingleLine: All
2929
AllowShortIfStatementsOnASingleLine: Never

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
url = https://github.com/arsenm/sanitizers-cmake.git
77
[submodule "third_party/cmake-modules"]
88
path = third_party/cmake-modules
9-
url = https://github.com/bilke/cmake-modules.git
9+
url = https://github.com/bilke/cmake-modules.git

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"string.h": "c",
77
"lwevt_opt.h": "c",
88
"stdatomic.h": "c",
9-
"lwrb.h": "c"
9+
"lwrb.h": "c",
10+
"*.tcc": "c"
1011
},
1112
"esbonio.sphinx.confDir": ""
1213
}

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22

33
## Develop
44

5+
## v3.0.0
6+
7+
- Added macros for optional STDATOMIC. Global `-DLWRB_DISABLE_ATOMIC` macro will disable C11 `<stdatomic.h>` functionality.
8+
- Add `lwrb_move` and `lwrb_overwrite`
9+
- Fix `lwrb_find` which failed to properly search for tokens at corner cases
10+
511
## v3.0.0-RC1
612

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

1219
## v2.0.3
1320

@@ -53,4 +60,4 @@
5360

5461
## v1.0.0
5562

56-
- First stable release
63+
- First stable release

CMakeLists.txt

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,41 @@
11
cmake_minimum_required(VERSION 3.22)
22

33
# Setup project
4-
project(LwLibPROJECT)
5-
6-
# -------------------------------------------------
7-
# This CMakeLists.txt is used only if it is a top-level file.
8-
# Purpose of it is to be able to compile project in standalone way only
9-
#
10-
# When library sources are to be included in another project
11-
# user shall use /lwrb/CMakeLists.txt instead
12-
if (NOT PROJECT_IS_TOP_LEVEL)
13-
message(FATAL_ERROR "This CMakeLists.txt can only be used as top-level. Use /lwrb/CMakeLists.txt for library include purpose")
14-
endif()
15-
16-
# Set as executable
17-
add_executable(${PROJECT_NAME})
18-
19-
# Add key executable block
20-
target_sources(${PROJECT_NAME} PUBLIC
21-
${CMAKE_CURRENT_LIST_DIR}/dev/main.c
22-
)
23-
24-
# Add key include paths
25-
target_include_directories(${PROJECT_NAME} PUBLIC
26-
${CMAKE_CURRENT_LIST_DIR}/dev
27-
)
28-
29-
# Compilation definition information
30-
target_compile_definitions(${PROJECT_NAME} PUBLIC
31-
WIN32
32-
_DEBUG
33-
CONSOLE
34-
LWRB_DEV
35-
)
36-
37-
# Compiler options
38-
target_compile_options(${PROJECT_NAME} PRIVATE
39-
-Wall
40-
-Wextra
41-
-Wpedantic
42-
)
43-
44-
# Add subdir with lwrb and link to project
45-
add_subdirectory("lwrb" lwrb)
46-
target_link_libraries(${PROJECT_NAME} lwrb)
4+
project(LwLibPROJECT C)
5+
6+
if(NOT PROJECT_IS_TOP_LEVEL)
7+
add_subdirectory(lwrb)
8+
else()
9+
# Set as executable
10+
add_executable(${PROJECT_NAME})
11+
12+
# Add key executable block
13+
target_sources(${PROJECT_NAME} PUBLIC
14+
${CMAKE_CURRENT_LIST_DIR}/dev/main.c
15+
)
16+
17+
# Add key include paths
18+
target_include_directories(${PROJECT_NAME} PUBLIC
19+
${CMAKE_CURRENT_LIST_DIR}/dev
20+
)
21+
22+
# Compilation definition information
23+
target_compile_definitions(${PROJECT_NAME} PUBLIC
24+
WIN32
25+
_DEBUG
26+
CONSOLE
27+
LWRB_DEV
28+
)
29+
30+
# Compiler options
31+
target_compile_options(${PROJECT_NAME} PRIVATE
32+
-Wall
33+
-Wextra
34+
-Wpedantic
35+
)
36+
37+
# Add subdir with lwrb and link to project
38+
add_subdirectory(lwrb)
39+
target_link_libraries(${PROJECT_NAME} lwrb)
40+
target_link_libraries(${PROJECT_NAME} lwrb_ex)
41+
endif()

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 Tilen MAJERLE
3+
Copyright (c) 2023 Tilen MAJERLE
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Library provides generic FIFO ring buffer implementation.
66

77
## Features
88

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

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

3030
Alternatively you may:

dev/main.c

Lines changed: 141 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,11 @@ uint8_t tmp[8];
1414
void
1515
my_buff_evt_fn(lwrb_t* buff, lwrb_evt_type_t type, size_t len) {
1616
(void)buff;
17+
(void)len;
1718
switch (type) {
18-
case LWRB_EVT_RESET:
19-
printf("[EVT] Buffer reset event!\r\n");
20-
break;
21-
case LWRB_EVT_READ:
22-
printf("[EVT] Buffer read event: %d byte(s)!\r\n", (int)len);
23-
break;
24-
case LWRB_EVT_WRITE:
25-
printf("[EVT] Buffer write event: %d byte(s)!\r\n", (int)len);
26-
break;
19+
case LWRB_EVT_RESET: printf("[EVT] Buffer reset event!\r\n"); break;
20+
case LWRB_EVT_READ: printf("[EVT] Buffer read event: %d byte(s)!\r\n", (int)len); break;
21+
case LWRB_EVT_WRITE: printf("[EVT] Buffer write event: %d byte(s)!\r\n", (int)len); break;
2722
default: break;
2823
}
2924
}
@@ -34,31 +29,150 @@ main() {
3429

3530
/* Init buffer */
3631
lwrb_init(&buff, lwrb_data, sizeof(lwrb_data));
37-
lwrb_set_evt_fn(&buff, my_buff_evt_fn);
3832

39-
lwrb_write(&buff, "abc", 3);
40-
lwrb_write(&buff, "abc", 3);
41-
lwrb_write(&buff, "abc", 3);
42-
len = lwrb_read(&buff, tmp, 9);
33+
printf("Read/Write test\r\n");
34+
{
35+
uint8_t rw_buff[8];
36+
37+
#define RW_TEST(_w_exp_, _r_exp_, _rw_len_, _rw_exp_len_) \
38+
do { \
39+
printf("W ptr: %u, R ptr: %u, R/W len: %u, as_expected: %u\r\n", (unsigned)buff.w, (unsigned)buff.r, \
40+
(unsigned)(_rw_len_), \
41+
(unsigned)(buff.w == (_w_exp_) && buff.r == (_r_exp_) && (_rw_len_) == (_rw_exp_len_))); \
42+
} while (0)
43+
44+
lwrb_reset(&buff);
45+
len = lwrb_write(&buff, "abc", 3); /* Write 3 bytes */
46+
RW_TEST(3, 0, len, 3);
47+
len = lwrb_write(&buff, "abc", 3); /* Write 3 bytes */
48+
RW_TEST(6, 0, len, 3);
49+
len = lwrb_read(&buff, rw_buff, 3); /* Read 3 bytes */
50+
RW_TEST(6, 3, len, 3);
51+
len = lwrb_read(&buff, rw_buff, 4); /* Read 4 bytes */
52+
RW_TEST(6, 6, len, 3);
53+
54+
len = lwrb_write(&buff, "abc", 3); /* Write 3 bytes -> buffer should go over */
55+
RW_TEST(0, 6, len, 3);
56+
57+
#undef RW_TEST
58+
}
59+
60+
printf("Overwrite test\r\n");
61+
{
62+
#define OVERWRITE_TEST(_exp_content_, _exp_len_) \
63+
do { \
64+
len = lwrb_peek(&buff, 0, tmp, buff.size); \
65+
printf("overwrite data read: %.*s, len: %u, as_expected: %u\r\n", (int)len, tmp, (unsigned)len, \
66+
(unsigned)(strncmp((_exp_content_), (const void*)tmp, len) == 0 && len == (_exp_len_))); \
67+
} while (0)
4368

44-
buff.r = 0;
45-
buff.w = 0;
46-
memset(lwrb_get_linear_block_write_address(&buff), 'A', lwrb_get_linear_block_write_length(&buff));
47-
lwrb_advance(&buff, lwrb_get_linear_block_write_length(&buff));
69+
/* Test overwrite */
70+
lwrb_reset(&buff);
71+
lwrb_write(&buff, "abcdef", 6); /* Initial data */
72+
OVERWRITE_TEST("abcdef", 6);
4873

49-
buff.r = 2;
50-
buff.w = 0;
51-
memset(lwrb_get_linear_block_write_address(&buff), 'B', lwrb_get_linear_block_write_length(&buff));
52-
lwrb_advance(&buff, lwrb_get_linear_block_write_length(&buff));
74+
lwrb_overwrite(&buff, "0", 1);
75+
OVERWRITE_TEST("abcdef0", 7);
76+
77+
lwrb_overwrite(&buff, "1", 1);
78+
OVERWRITE_TEST("abcdef01", 8);
79+
80+
lwrb_overwrite(&buff, "2", 1);
81+
OVERWRITE_TEST("bcdef012", 8);
82+
83+
lwrb_overwrite(&buff, "3", 1);
84+
OVERWRITE_TEST("cdef0123", 8);
85+
86+
lwrb_overwrite(&buff, "4", 1);
87+
OVERWRITE_TEST("def01234", 8);
88+
89+
lwrb_overwrite(&buff, "5", 1);
90+
OVERWRITE_TEST("ef012345", 8);
91+
92+
/* Bigger write which will completely change the buffer structure */
93+
lwrb_overwrite(&buff, "lwrb_new_test_structure", 23);
94+
OVERWRITE_TEST("tructure", 8);
95+
#undef OVERWRITE_TEST
96+
}
5397

54-
buff.r = 3;
55-
buff.w = 3;
56-
memset(lwrb_get_linear_block_write_address(&buff), 'C', lwrb_get_linear_block_write_length(&buff));
57-
lwrb_advance(&buff, lwrb_get_linear_block_write_length(&buff));
98+
printf("Move test\r\n");
99+
{
100+
#define MOVE_TEST(_exp_content_, _exp_move_len_, _exp_buff_len_) \
101+
do { \
102+
size_t move_len; \
103+
move_len = lwrb_move(&dst, &src); \
104+
len = lwrb_peek(&dst, 0, tmp, dst.size); \
105+
printf("move data: len: %d, dest data: %.*s, as_expected: %u\r\n", (int)len, (int)len, tmp, \
106+
(unsigned)(strncmp((_exp_content_), (const void*)tmp, len) == 0 && move_len == (_exp_move_len_) \
107+
&& len == (_exp_buff_len_))); \
108+
} while (0)
58109

59-
lwrb_reset(&buff);
110+
lwrb_t src, dst;
111+
uint8_t src_data[16], dst_data[8];
112+
lwrb_init(&src, src_data, sizeof(src_data));
113+
lwrb_init(&dst, dst_data, sizeof(dst_data));
114+
115+
lwrb_reset(&src);
116+
lwrb_reset(&dst);
117+
lwrb_write(&src, "012345", 6);
118+
MOVE_TEST("012345", 6, 6);
119+
120+
lwrb_reset(&src);
121+
lwrb_reset(&dst);
122+
lwrb_write(&src, "0123456789ABCDEF", 16);
123+
MOVE_TEST("0123456", 7, 7);
124+
125+
lwrb_reset(&src);
126+
lwrb_reset(&dst);
127+
lwrb_write(&src, "0123456789ABCDEF", 16);
128+
lwrb_write(&dst, "TT", 2);
129+
MOVE_TEST("TT01234", 5, 7);
130+
131+
#undef MOVE_TEST
132+
}
60133

61134
(void)len;
62135

136+
printf("Find test\r\n");
137+
{
138+
#define FIND_TEST(_bts_, _bts_len_, _start_offset_, _exp_result_) \
139+
do { \
140+
size_t found_idx; \
141+
uint8_t found; \
142+
found = lwrb_find(&buff, (_bts_), (_bts_len_), (_start_offset_), &found_idx); \
143+
printf("Find \"%s\" (len %d), start_offset: %d, found_index: %d; Found: %d; As expected: %d\r\n", (_bts_), \
144+
(_bts_len_), (_start_offset_), (int)found_idx, (int)found, (int)(!!found == !!(_exp_result_))); \
145+
} while (0)
146+
147+
/* Prepare buffer and write data */
148+
lwrb_reset(&buff);
149+
lwrb_write(&buff, "12345678", 8);
150+
151+
FIND_TEST("123", 3, 0, 1); /* Must find it */
152+
FIND_TEST("456", 3, 0, 1); /* Must find it */
153+
FIND_TEST("123", 3, 1, 0); /* Must not find it - start offset is later */
154+
FIND_TEST("678", 3, 0, 1);
155+
156+
/* Restart by setting write and read as empty with offset */
157+
buff.w = 6;
158+
buff.r = 6;
159+
lwrb_write(&buff, "12345678", 8);
160+
161+
FIND_TEST("123", 3, 0, 1); /* Must find it */
162+
FIND_TEST("456", 3, 0, 1); /* Must find it */
163+
FIND_TEST("123", 3, 1, 0); /* Must not find it - start offset is later */
164+
165+
/* Restart by setting write and read as empty with offset */
166+
/* This should generate data for search in overflow mode */
167+
buff.w = 8;
168+
buff.r = 8;
169+
lwrb_write(&buff, "12345678", 8);
170+
171+
FIND_TEST("1234", 3, 0, 1); /* Must find it */
172+
FIND_TEST("4567", 3, 0, 1); /* Must find it */
173+
FIND_TEST("1234", 3, 1, 0); /* Must not find it - start offset is later */
174+
175+
#undef FIND_TEST
176+
}
63177
return 0;
64178
}

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# -- Project information -----------------------------------------------------
2424

2525
project = 'LwRB'
26-
copyright = '2022, Tilen MAJERLE'
26+
copyright = '2023, Tilen MAJERLE'
2727
author = 'Tilen MAJERLE'
2828

2929
# Try to get branch at which this is running

docs/get-started/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Next step is to add the library to the project, by means of source files to comp
6262

6363
* Copy ``lwrb`` folder to your project, it contains library files
6464
* 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
65-
* Add source files from ``lwrb/src/`` folder to toolchain build. These files are built by `C/C++` compilery
65+
* 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
6666
* Build the project
6767

6868
Minimal example code

0 commit comments

Comments
 (0)