Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
ring_buffer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Egorov, Sergey (PG/R - Comp Sci & Elec Eng)
ring_buffer
Commits
38f34a87
Commit
38f34a87
authored
3 years ago
by
solsta
Browse files
Options
Downloads
Patches
Plain Diff
Fixed get_free_slots_left_in_the_buffer method
parent
a20aa32e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.c
+29
-4
29 additions, 4 deletions
main.c
with
29 additions
and
4 deletions
main.c
+
29
−
4
View file @
38f34a87
...
@@ -32,6 +32,10 @@ struct cmd_and_next_index{
...
@@ -32,6 +32,10 @@ struct cmd_and_next_index{
unsigned
int
get_free_slots_left_in_the_buffer
(
struct
ring_buffer
*
rb
){
unsigned
int
get_free_slots_left_in_the_buffer
(
struct
ring_buffer
*
rb
){
/* There is no scenario where this can happen other than on the first iteration */
if
(
rb
->
head
==
rb
->
tail
&&
rb
->
head
==
0
){
return
rb
->
real_size
;
}
return
rb
->
head
>
rb
->
tail
?
rb
->
real_size
-
rb
->
head
+
rb
->
tail
:
rb
->
tail
-
rb
->
head
;
return
rb
->
head
>
rb
->
tail
?
rb
->
real_size
-
rb
->
head
+
rb
->
tail
:
rb
->
tail
-
rb
->
head
;
}
}
...
@@ -101,13 +105,13 @@ int insert(struct ring_buffer *rb, char *command){
...
@@ -101,13 +105,13 @@ int insert(struct ring_buffer *rb, char *command){
printf
(
"No space to insert this entry, dropping!
\n
"
);
printf
(
"No space to insert this entry, dropping!
\n
"
);
return
1
;
return
1
;
}
}
if
(
buffer_has_space_for_command
(
rb
,
sizeof
command
)
>
0
){
if
(
strlen
(
command
)
+
sizeof
(
struct
varied_length_command_info
)
>
get_free_slots_left_in_the_buffer
(
rb
)){
printf
(
"No space to insert this entry, dropping!
\n
"
);
return
1
;
}
insert_in_to_buffer
(
rb
,
command
);
insert_in_to_buffer
(
rb
,
command
);
rb
->
number_of_commands
+=
1
;
rb
->
number_of_commands
+=
1
;
return
0
;
return
0
;
}
printf
(
"No space to insert this entry, dropping!
\n
"
);
return
1
;
}
}
struct
cmd_and_next_index
*
retrieve_command_at_index
(
struct
ring_buffer
*
rb
,
int
index
){
struct
cmd_and_next_index
*
retrieve_command_at_index
(
struct
ring_buffer
*
rb
,
int
index
){
...
@@ -227,6 +231,12 @@ struct ring_buffer *initialise_ring_buffer(PMEMobjpool *pop){
...
@@ -227,6 +231,12 @@ struct ring_buffer *initialise_ring_buffer(PMEMobjpool *pop){
return
rb
;
return
rb
;
}
}
void
reset_ring_buffer
(
struct
ring_buffer
*
rb
){
rb
->
head
=
0
;
rb
->
tail
=
0
;
rb
->
number_of_commands
=
0
;
}
int
main
()
{
int
main
()
{
PMEMobjpool
*
pop
=
NULL
;
PMEMobjpool
*
pop
=
NULL
;
pop
=
mmap_pmem_object_pool
(
pop
);
pop
=
mmap_pmem_object_pool
(
pop
);
...
@@ -239,6 +249,21 @@ int main() {
...
@@ -239,6 +249,21 @@ int main() {
/* This must fail because command is too long */
/* This must fail because command is too long */
assert
(
insert
(
rb
,
"foo bar foo bar foo bar foo bar"
)
==
1
);
assert
(
insert
(
rb
,
"foo bar foo bar foo bar foo bar"
)
==
1
);
/* Testing inserting more than buffer can accomodate for */
assert
(
insert
(
rb
,
"abcdefghij"
)
==
0
);
assert
(
insert
(
rb
,
"abcdefghij"
)
==
0
);
assert
(
insert
(
rb
,
"abcdefghij"
)
==
1
);
reset_ring_buffer
(
rb
);
/* Testing a reset scenario */
assert
(
insert
(
rb
,
"abcdef"
)
==
0
);
assert
(
insert
(
rb
,
"abcdef"
)
==
0
);
assert
(
insert
(
rb
,
"abcdef"
)
==
1
);
assert
(
insert
(
rb
,
"abcdefg"
)
==
1
);
assert
(
insert
(
rb
,
"abcde"
)
==
0
);
assert
(
rb
->
head
==
rb
->
real_size
);
reset_ring_buffer
(
rb
);
char
seen_head_positions
[
rb
->
real_size
];
char
seen_head_positions
[
rb
->
real_size
];
char
seen_tail_positions
[
rb
->
real_size
];
char
seen_tail_positions
[
rb
->
real_size
];
for
(
int
i
=
0
;
i
<=
rb
->
real_size
;
++
i
){
for
(
int
i
=
0
;
i
<=
rb
->
real_size
;
++
i
){
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment