Skip to content

CircularBuffer class modification and test #5578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 20, 2017

Conversation

mprse
Copy link
Contributor

@mprse mprse commented Nov 24, 2017

Description

This PR provides:

  • proposal of updating the CircularBuffer class (assertion for CounterType).
  • CircularBuffer test.

Status

READY

Migrations

NO

@@ -30,21 +30,25 @@ namespace mbed {
*
* @note Synchronization level: Interrupt safe
*/
template<typename T, uint32_t BufferSize, typename CounterType = uint32_t>
template<typename T, uint32_t BufferSize>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a breaking change; why not use a static assertion rather than removing the template parameter:

    MBED_STATIC_ASSERT(
        (sizeof(CounterType) >= sizeof(uint32_t)) ||
        (BufferSize < (((uint64_t) 1) << (sizeof(CounterType) * 8))),
        "Invalid BufferSize for the CounterType"
    );

Copy link
Contributor Author

@mprse mprse Nov 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assertion is an alternative to this solution. I decided to make a proposal with removed template parameter since I can't see any advantage of having it. Why do we need it?

Provided solution is not complete. Also signed counter types should be forbidden. The following declaration is also invalid and will not be caught by the static assertion:
CircularBuffer<int, 255, char> cb;

Copy link
Member

@pan- pan- Nov 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CounterType allows user of the class to choose the most appropriate variable type for inner counters of the object; it might save few bytes by CircularBuffer. It would have been better if the type selection was made automatically based on the buffer size but it is here now and we have to live with it.

Provided solution is not complete. Also signed counter types should be forbidden. The following declaration is also invalid and will not be caught by the static assertion:
CircularBuffer<int, 255, char> cb;

The static assertion can handle the sign of the CounterType if you define a type traits that determine if an integral type is signed or unsigned.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are committed to preserving backward compatibility for our user facing APIs for 5.x versions of Mbed OS.

@@ -30,21 +30,25 @@ namespace mbed {
*
* @note Synchronization level: Interrupt safe
*/
template<typename T, uint32_t BufferSize, typename CounterType = uint32_t>
template<typename T, uint32_t BufferSize>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are committed to preserving backward compatibility for our user facing APIs for 5.x versions of Mbed OS.

@@ -110,9 +118,10 @@ class CircularBuffer {
}

/** Get the number of elements currently stored in the circular_buffer */
CounterType size() const {
uint32_t size() const
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't change APIs.

@mprse mprse force-pushed the CircularBuffer_tests branch from 3100589 to 9e0d6b1 Compare November 27, 2017 13:26
@mprse
Copy link
Contributor Author

mprse commented Nov 27, 2017

Modified this patch. In current version CounterType template parameter stays, but it must be unsigned type consistent with the BufferSize. Test has been also adapted.

@deepikabhavnani
Copy link

Linking the issue: #5384

@@ -18,6 +18,20 @@

#include "platform/mbed_critical.h"

/* Detect if CounterType of the Circular buffer is of unsigned type. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you put the traits in a namespace named internal within the mbed namespace.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed as suggested.

CounterType is used to define type of _head and _tail counters. This may cause the following problems:
- counters are used as array indexes so only unsigned types should be used for counters,
- CounterType must be consistent with BufferSize and BufferSize is of uint32_t type (i.e. Circular Buffer with the following parameters: BufferSize = 1000, CounterType = unsigned char will not work properly).
@mprse mprse force-pushed the CircularBuffer_tests branch from 9e0d6b1 to a488424 Compare November 29, 2017 10:30
@0xc0170
Copy link
Contributor

0xc0170 commented Nov 30, 2017

@mprse What was updated, please state to let reviewers aware this is ready for another round

@mprse
Copy link
Contributor Author

mprse commented Nov 30, 2017

I provided responses to review comments. All change requests have been processed.

@0xc0170 0xc0170 requested a review from SenRamakri December 1, 2017 12:43
Copy link
Member

@bulislaw bulislaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0xc0170
Copy link
Contributor

0xc0170 commented Dec 7, 2017

/morph build

@mbed-ci
Copy link

mbed-ci commented Dec 7, 2017

Build : SUCCESS

Build number : 663
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/5578/

Triggering tests

/morph test
/morph uvisor-test
/morph export-build

@mbed-ci
Copy link

mbed-ci commented Dec 7, 2017

@mbed-ci
Copy link

mbed-ci commented Dec 7, 2017

@SeppoTakalo
Copy link
Contributor

This PR just broke all our Ci builds.

screen shot 2017-12-21 at 12 19 17

MBED_STATIC_ASSERT is a macro that is defined in platform/mbed_assert.h
If you include the header before any other header that is including the assert macros, then the build fails.

Provided fix in #5748

@mprse
Copy link
Contributor Author

mprse commented Dec 21, 2017

This PR just broke all our Ci builds.

Sorry for missing platform/mbed_assert.h include and thanks for provided fix.

@0xc0170
Copy link
Contributor

0xc0170 commented Dec 21, 2017

Thanks @SeppoTakalo . How come our all CI did not catch this one?

@SeppoTakalo
Copy link
Contributor

@0xc0170 The cliapp that is build in the PR contains copy&paste from CircularBuffer.

The build that broke, is my test branch (soon to be merged to master), and there I have removed the CircularBuffer and I use the one from Mbed OS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants