-
Notifications
You must be signed in to change notification settings - Fork 7k
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
include: util: Add memeq
and bool_memcmp
#84159
Open
theob-pro
wants to merge
3
commits into
zephyrproject-rtos:main
Choose a base branch
from
theob-pro:add-memeq
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+90
−3
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
#include <zephyr/types.h> | ||
#include <stddef.h> | ||
#include <stdint.h> | ||
#include <string.h> | ||
|
||
/** @brief Number of bits that make up a type */ | ||
#define NUM_BITS(t) (sizeof(t) * BITS_PER_BYTE) | ||
|
@@ -783,6 +784,40 @@ static inline void mem_xor_128(uint8_t dst[16], const uint8_t src1[16], const ui | |
mem_xor_n(dst, src1, src2, 16); | ||
} | ||
|
||
/** | ||
* @brief Compare memory areas. The same way as `memcmp` it assume areas to be | ||
* the same length | ||
* | ||
* @param m1 First memory area to compare, cannot be NULL even if length is 0 | ||
* @param m2 Second memory area to compare, cannot be NULL even if length is 0 | ||
* @param n First n bytes of @p m1 and @p m2 to compares | ||
* | ||
* @returns true if the @p n first bytes of @p m1 and @p m2 are the same, else | ||
* false | ||
*/ | ||
static inline bool z_util_memeq(const void *m1, const void *m2, size_t n) | ||
{ | ||
return memcmp(m1, m2, n) == 0; | ||
} | ||
|
||
/** | ||
* @brief Compare memory areas and their length | ||
* | ||
* If the length are 0, return true. | ||
* | ||
* @param m1 First memory area to compare, cannot be NULL even if length is 0 | ||
* @param len1 Length of the first memory area to compare | ||
* @param m2 Second memory area to compare, cannot be NULL even if length is 0 | ||
* @param len2 Length of the second memory area to compare | ||
* | ||
* @returns true if both the length of the memory areas and their content are | ||
* equal else false | ||
*/ | ||
static inline bool z_util_eq(const void *m1, size_t len1, const void *m2, size_t len2) | ||
{ | ||
return len1 == len2 && z_util_memeq(m1, m2, len1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I recommend shorting the call to |
||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
z_ shall not be used for any public APIs, it is a intended to be used for internal/private APIs only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that documented anywhere?
The same file has
https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/sys/util.h#L393-L394
https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/sys/util.h#L731-L745
https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/sys/util.h#L826-L830
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
never made it in the docs, https://github.com/zephyrproject-rtos/zephyr/pull/62198/files. z_ was added long time ago to replace
_
usage in the tree, but unfortuantly it was seen by many as the zephyr designated prefix for APIs and we ended up with public APIs usingz_
....There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is something that must be documented. It seems that there is quite a bunch of public APIs now using the
z_
, is there good reasons to continue to have that hidden rule?To me it seems that without proper documentation some more will definitely be added and then I don't see the point of blocking it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nashif do you have alternative suggestions for naming?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just
util_memeq
?