Skip to content
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

const correctness for platform write #121

Closed
pabigot opened this issue Apr 15, 2021 · 2 comments
Closed

const correctness for platform write #121

pabigot opened this issue Apr 15, 2021 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@pabigot
Copy link

pabigot commented Apr 15, 2021

Some development efforts have a policy that when a function promises not to modify data referenced by pointer, the referenced data should have a const qualifier in the function parameter declaration.

In that situation the declaration for stmdev_write_ptr may force the caller to cast away const to satisfy the current signature:

typedef int32_t (*stmdev_write_ptr)(void *, uint8_t, uint8_t *,
                                    uint16_t);

This casting would not be necessary if the signature were instead:

typedef int32_t (*stmdev_write_ptr)(void *, uint8_t, const uint8_t *,
                                    uint16_t);

It doesn't seem reasonable to expect any platform write implementation to be allowed to change data that it is transferring to the device, so tightening the declaration should be technically OK. But since the change would affect every existing platform_write implementation it would break existing code, so should probably be part of an update to version 2.x.

Just something to keep in mind.

@pabigot
Copy link
Author

pabigot commented Apr 19, 2021

As noted in zephyrproject-rtos/zephyr#34389 this would also apply to the stmdev_ctx_t *ctx argument passed to all driver functions. That should be const stmdev_ctx_t *ctx since the driver functions shouldn't be modifying its content.

@avisconti
Copy link
Contributor

As noted in zephyrproject-rtos/zephyr#34389 this would also apply to the stmdev_ctx_t *ctx argument passed to all driver functions. That should be const stmdev_ctx_t *ctx since the driver functions shouldn't be modifying its content.

Yes, that's correct. I confirm that it would be nice to add the 'const' to the fields. I already discussed internally, and if that would be changed in the STdC drivers I will align them in zephyr and would be able to remove the bad casting I had to insert when using the ctx handle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants