Commit Graph

41 Commits

Author SHA1 Message Date
Anas Nashif 009dee157d doc: fix doxygen grouping
Fix various grouping issues in doxygen and name groups correctly in some
cases.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-05-17 11:51:44 -04:00
Peter Bigot ca818d5fb9 sys: util: add BIT64_MASK
BIT/_MASK doesn't necessarily handle 64 bits, so provide comparable
functionality for BIT64/_MASK.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-11-13 06:38:01 -05:00
Trond Einar Snekvik ed1f75da74 sys: util: Add clamp macro
Adds CLAMP macro to complement the current min/max macros, as well as a
gcc specific Z_CLAMP macro for single-evaluation expansion.

CLAMP combines the functionality of MIN and MAX, eliminating the
bug-prone usage of MIN(MAX(value, FLOOR), CEIL) found throughout the
codebase in every possible combination.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2020-11-05 12:12:17 +01:00
Gerson Fernando Budke 6b05121e65 include: sys: util: Move macros from util to util_macro
The <sys/util.h>, in current form, can not be used with DTS as it
contains non-C pre-processor definitions which breake DTS interpreter.
This commit fixes the problem by moving most of preprocessor macros
from util.h to util_macro.h.  Since util_mcaro.h contains only
preprocessor macros, without include dependencies, it can be safely
included in DTS. It is similar way the
<dts/arm/<manufacturer>/pinctrl_<manufacturer>_<soc>.h files are
included.

This fix and align the extern "C" closing brack inside non assembly
block.

The issue was raised when try create a macro for pincrtl with a
variable length flag list.

Signed-off-by: Gerson Fernando Budke <nandojve@gmail.com>
2020-10-23 09:38:55 -05:00
Peter Bigot 3dd804fe91 sys: util: add identity macro
Several utility macros must be given the name of a macro to apply to
every element in a sequence to transform it.  When no transformation
is necessary a macro still needs to be provided.  That macro is
IDENTITY.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-10-19 18:14:11 +02:00
Aastha Grover 83b9f69755 code-guideline: Fixing code violation 10.4 Rule
Both operands of an operator in the arithmetic conversions
performed shall have the same essential type category.

Changes are related to converting the integer constants to the
unsigned integer constants

Signed-off-by: Aastha Grover <aastha.grover@intel.com>
2020-10-01 17:13:29 -04:00
Martí Bolívar bd86078376 sys/util.h: add FOR_EACH_NONEMPTY_TERM
This is an extension to the existing FOR_EACH family of macros that
has a couple of twists that make it more useful in certain contexts,
as explained in the docstring.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2020-08-24 20:29:11 -04:00
Peter Bigot 235a5c1771 sys/util: fix macro support when building with C++
true and false in a C environment are macros that are defined to 1 and
0 respectively.  In C++ they are keywords, and as such cause failures
when macro tests require they expand to a 1 or 0 token.  Use the
actual token values rather than the macro names when defining the
macros.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-08-12 16:39:37 -05:00
Daniel Leung cae8bd7408 sys: util: cast input of KB() to size_t for large number
Numbers in C macros are treated as signed integer. This causes
issues when using macro KB(x). If the result is larger than
(2^31 - 1), it is treated as a negative number downstream.
For example, the RAM size of up_squared is 2GB. The result of
KB(CONFIG_SRAM_SIZE) should be 2147483648, but due to being
casted as signed, the result is instead -2147483648. Any
calculations using this would be incorrectly. Fix it by
casting x in KB(x) to size_t first.

Fixes #27164

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2020-08-04 17:45:22 +02:00
Krzysztof Chruscinski 16fa71276a sys: util: Document limitation of IS_EMPTY macro
IS_EMPTY macro does not accept all type of arguments.
Added clarification.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2020-07-08 23:25:27 -04:00
Krzysztof Chruscinski b8781e27b0 sys: util: Deprecate GET_ARGS_LESS_1 macro
GET_ARGS_LESS_N must be used instead.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2020-06-22 15:18:07 +02:00
Krzysztof Chruscinski dd9da9c8c4 sys: util: Deprecate GET_ARG2 macro
GET_ARG_N should be used instead.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2020-06-22 15:18:07 +02:00
Krzysztof Chruscinski 0afb3445a5 sys: util: Deprecate GET_ARG1 macro
GET_ARG_N(1,...) can be used instead.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2020-06-22 15:18:07 +02:00
Krzysztof Chruscinski c58b80d805 sys: util: Add GET_ARG_N and GET_ARGS_LESS_N macros
Macro GET_ARG_N is geting nth argument from the variable list provided.
GET_ARGS_LESS_N returns argument list without n first arguments.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2020-06-22 15:18:07 +02:00
Martí Bolívar 1af73a66ab sys/util.h: remove deprecated MACRO_MAP()
Since this is an experimental API and MACRO_MAP() was deprecated in
favor of FOR_EACH() in zephyr v2.3.0, we are within our rights to just
remove it without notice now. Do so and mention it in the release
notes.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2020-06-18 19:38:10 +02:00
Martí Bolívar 04df8124ac api: promote sys/util.h to experimental
This file contains definitions for macros which are integral to
significant Zephyr use cases, such as CONTAINER_OF() and various
macros used by devicetree.h internally.

As such, in practice we expect at least advanced (if not intermediate)
users to understand it, so the fact that it's not formally documented
as an API with a stability level is a problem.

Fix that by giving the docstrings a once-over and adding new ones
where they are missing. Move all the remaining non-API macros to
util_internal.h.

Add a Sphinx API page for this header, and include it in the API
overview at "experimental" stability level.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2020-06-18 19:38:10 +02:00
Martí Bolívar 317eaa5a7e util.h: remove duplicate UTIL_EXPAND() definition
This is already defined (to the same expansion) higher up in the file.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2020-06-18 19:38:10 +02:00
Martí Bolívar 458ae3403c util.h: clean up UTIL_INC_x and UTIL_DEC_x
Move the repetitive definitions used to add and subtract 1 at
preprocessor time into their own file. Make the behavior consistent,
so that you can invoke UTIL_INC(x) for any x you can invoke
UTIL_DEC(x) on.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2020-06-18 19:38:10 +02:00
Peter Bigot fe74218956 sys: util.h: remove template implementation of ARRAY_SIZE
This was added because GNU G++ doesn't support the built-in function
used to produce a compile-time error when invalid parameters are
passed to the macro imlementation.  The template implementation does
not work on declarations like this:

  mytype array[] = { ... }

because it requires an explicit size for the template parameter.

Remove the template specialization, and support C++ with a version of
the macro that doesn't involve references to undefined builtins.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-06-10 12:02:45 +02:00
Krzysztof Chruscinski 1b4b9386e5 sys: util: Added separator to FOR_EACH_ macros
Added separator (e.g. comma or semicolon) parameter to FOR_EACH_ family.
Separator is added between macro execution for each argument and not at
the end.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2020-06-10 11:58:13 +02:00
Kumar Gala a1b77fd589 zephyr: replace zephyr integer types with C99 types
git grep -l 'u\(8\|16\|32\|64\)_t' | \
		xargs sed -i "s/u\(8\|16\|32\|64\)_t/uint\1_t/g"
	git grep -l 's\(8\|16\|32\|64\)_t' | \
		xargs sed -i "s/s\(8\|16\|32\|64\)_t/int\1_t/g"

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-06-08 08:23:57 -05:00
Dominik Ermel 6ba69f19d4 sys/util.h: Add IS_EMPTY and LIST_DROP_EMPTY macros
IS_EMPTY macro allows to check if defined name is empty, i.e.
does not contain replacement list.
LIST_DROP_EMPTY macro may be used to process __VA_ARGS__ type lists,
e.g. a,b,,c , and remove empty elements.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2020-05-08 17:50:25 +02:00
Alberto Escolar Piedras cd0659e36f util.h: Remove INLINE definition and user
INLINE is a very common macro, just like MAX or MIN.
Defining it always can easily collide with libraries or
application headers.
And option would be to add a ifdef guard around it,
But it was used in only 1 place in Zephyr, instead
of keeping it just for that, remove it.

Signed-off-by: Alberto Escolar Piedras <alpi@oticon.com>
2020-05-08 08:37:21 -04:00
Krzysztof Chruscinski fa3729fd11 sys: util: Add missing doxygen tags
Some macros were missing doxygen tag.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2020-05-06 14:30:18 +02:00
Krzysztof Chruscinski 32d9da7224 sys: util: Deprecate MACRO_MAP
MACRO_MAP has the same functionality as FOR_EACH macro. Removed macro
implementation and replaced with FOR_EACH call.

Deprecated macro to avoid having two macros with the same
functionality.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2020-05-06 14:30:18 +02:00
Krzysztof Chruscinski 69577eb619 sys: util: Update FOR_EACH and FOR_EACH_FIXED_ARG implementation
Updated implementation of FOR_EACH and FOR_EACH_FIXED_ARG to use same
engine as FOR_EACH_IDX macro.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2020-05-06 14:30:18 +02:00
Krzysztof Chruscinski 925540a112 sys: util: Add FOR_EACH_IDX and FOR_EACH_IDX_FIXED_ARG macros
Added macros which iterate over provided parameters and call specific
macro with this parameter, index and fixed argument
(FOR_EACH_IDX_FIXED_ARG).

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2020-05-06 14:30:18 +02:00
Kumar Gala 25c0eac109 sys/util.h: Make IS_ENABLED work for linker scripts
Z_IS_ENABLED2 uses true and false, however when we preprocess the linker
scripts we invoke the compiler with `-x assembler-with-cpp` so
_ASMLANGUAGE ends up being defined and thus stdbool.h wouldn't convert
'true' and 'false' to '1' and '0'.

Move the include of stdbool.h outside the #ifndef _ASMLANGUAGE check so
that 'true' and 'false' get converted correctly.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-04-24 06:29:01 -05:00
Steven Slupsky 4cd893021a include: util.h: add extern "C"
extern "C" is missing.
This commit adds conditional extern "C" for cpp to
the beginning of the file and brackets the cpp template with
extern "C++"

Signed-off-by: Steven Slupsky <sslupsky@gmail.com>
2020-04-20 15:46:00 +02:00
Martí Bolívar 1edc84f526 sys/util.h: add UTIL_OR and UTIL_AND
These are short-circuiting utility helpers that can save typing
in situations where avoiding evaluation of the not-taken branch
skips invalid expressions.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2020-03-25 13:21:28 +01:00
Martí Bolívar 7b2376b72e util.h: add MACRO_MAP_CAT() and MACRO_MAP_CAT_N()
This is like MACRO_MAP(), but it pastes the results together into a
single token. The result is kind of a fold of the ## operator.

I wasn't able to figure out a way to implement this using any of the
existing macros, so there's some more copy/pasting of handler macros
for different numbers of arguments.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2020-03-18 03:12:08 -06:00
Kumar Gala 28a88fc638 sys/util.h: allow UTIL_LISTIFY to pass args to repeat macro
Change UTIL_LISTIFY to support passing arguments to the F macro.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-03-13 07:33:57 -06:00
Krzysztof Chruscinski ffb8d479c7 sys: util: Add IF_ENABLED() macro
Added macro for code inserting based on configuration flag.
This macro is wrapper around COND_CODE_1().

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2019-12-18 08:14:52 -05:00
Peter A. Bigot 6ade720851 treewide: avoid use of unsupported C++ specifiers
constexpr and noexcept were introduced as specifiers in C++11.  Avoid
referencing them when compiling for earlier versions of the language.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2019-12-13 17:19:54 -06:00
Andrew Boie 31620b90e2 x86: refactor mmustructs.h
The struct definitions for pdpt, pd, and pt entries has been
removed:

 - Bitfield ordering in a struct is implementation dependent,
   it can be right-to-left or left-to-right
 - The two different structures for page directory entries were
   not being used consistently, or when the type of the PDE
   was unknown
 - Anonymous structs/unions are GCC extensions

Instead these are now u64_t, with bitwise operations used to
get/set fields.

A new set of inline functions for fetcing various page table
structures has been implemented, replacing the older macros.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-10-14 11:49:39 -07:00
Krzysztof Chruscinski bea966dea1 toolchain: Add single arguments evaluation macros for min and max
MAX() and MIN() were evaluating arguments twice. If arguments are
functions they were called twice which resulted in bigger code
and potential misbehavior.

Added alternative macros (Z_MAX, Z_MIN) which can be used instead.
Macros have usage limitations thus they are not replacements. They
are also relying on GCC extension thus placed in gcc.h

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2019-09-25 17:41:03 +02:00
Kim Sekkelund 0450263393 Bluetooth: Host: Remove printk dependency from settings
Some modules use snprintk to format the settings keys. Unfortunately
snprintk is tied with printk which is very large for some embedded
systems.
To be able to have settings enabled without also enabling printk
support, change creation of settings key strings to use bin2hex, strlen
and strcpy instead.
A utility function to make decimal presentation of a byte value is
added as u8_to_dec in lib/os/dec.c
Add new Kconfig setting BT_SETTINGS_USE_PRINTK

Signed-off-by: Kim Sekkelund <ksek@oticon.com>
2019-09-25 17:36:39 +02:00
Peter A. Bigot 765c06376c Revert "sys/util.h: helper macro to perform pointer difference"
This reverts commit 755cc644cc.

This approach is problematic in several ways.  First, `intptr_t` could
cause undefined behavior in the subtraction when the pointer converts to
a negative value.  Except in weird cases where the sign of the pointer
identifies a memory domain (like kernel vs userspace) I'm unaware of any
valid use of `intptr_t`.

Second, this macro was created to address a special need that cannot
rely on defined behavior: i.e. to ensure that data definitions are
placed in contiguous space and access is provided through linker-defined
symbols, for which the language required alignment and continuity is not
guaranteed.

A macro that calculates the span between linker symbols has very
different semantics than one that calculates the difference between
pointers.  Replace the global PTR_DIFF with a documented local macro
that tests what's necessary without risking integer overflow.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2019-07-17 03:44:03 -04:00
Joakim Andersson 7a93e948a9 kernel: lib: Add convert functions for hex strings and binary arrays
Move duplicate hex2bin and add bin2hex function so that application can
use the functions and avoid code duplication.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-07-16 12:44:18 +02:00
Nicolas Pitre 755cc644cc sys/util.h: helper macro to perform pointer difference
Some code casts pointers to ints in order to obtain their difference.
The compiler complains on 64-bit targets as an int is not wide enough
to hold a pointer.

Let's introduce the PTR_DIFF() helper macro to applies the proper cast
to pointers before performing a difference on them, and still return the
result as an int which should be large enough in practice.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2019-07-12 17:39:43 -07:00
Anas Nashif a2fd7d70ec cleanup: include/: move misc/util.h to sys/util.h
move misc/util.h to sys/util.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-27 22:55:49 -04:00