Cast NSEC_PER_SEC to long to resolve a compiler warning about comparison
between signed and unsigned integer expression.
Signed-off-by: Tim Pambor <tim.pambor@codewrights.de>
Introduce a binary min-heap implementation as a generic data structure
for use in kernel and application code. A min-heap always maintains
the smallest element at the root, making insertion and removal of the
minimum element efficient (O(log n)).
The API allows both static and dynamic initialization, supports custom
comparators.
Signed-off-by: Sayooj K Karun <sayooj@aerlync.com>
Remove static struct modifier from SPSC_DECLARE macro.
This solves a warning emitted when compiling for native_sim.
Signed-off-by: Luca Arato <luca.arato@secomind.com>
Same error had to be fixed in the branch where optimizations were done.
This is probably a good example of where using `if (IS_ENABLED()) ...`
is a better choice than using `#ifdef`. Because the compiler would have
caught this error last time as well.
Anyway, same problem - different line(s)!
A warning was propogated to error when building for
native_sim/native.
```
timeutil.h:682:17: error: result of comparison of constant \
-9223372036854775808 with expression of type '__time_t' (aka 'long') \
is always false [-Werror,-Wtautological-constant-out-of-range-compare]
timeutil.h:680:17: error: result of comparison of constant \
-9223372036854775808 with expression of type '__time_t' (aka 'long') \
is always false [-Werror,-Wtautological-constant-out-of-range-compare]
```
This is due to the issue described in #90029.
Add workarounds for `timespec_to_timeout()` and `timespec_negate()`
until 90029 is resolved.
Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
A warning was propogated to error when building for
native_sim/native.
```
timeutil.h:519:17: error: result of comparison of constant \
-9223372036854775808 with expression of type '__time_t' (aka 'long') \
is always false [-Werror,-Wtautological-constant-out-of-range-compare]
```
This is due to the issue described in #90029.
Add workarounds for `timespec_to_timeout()` and `timespec_negate()`
until 90029 is resolved.
Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Add a number of utility functions for manipulating struct timespec.
* timespec_add()
* timespec_compare()
* timespec_equal()
* timespec_is_valid()
* timespec_negate()
* timespec_normalize()
* timespec_sub()
* timespec_from_timeout()
* timespec_to_timeout()
If the `__builtin_add_overflow()` function is available, then the
API is mostly branchless, which should provide decent performance on
systems with an instruction cache and branch prediction. Otherwise,
manually written alternatives exist that are also perhaps more
readable.
The two functions at the end convert time durations between
representation as `struct timespec` and `k_timeout_t`.
Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Add missing curly braces in if/while/for statements.
This is a style guideline we have that was not enforced in CI. All
issues fixed here were detected by sonarqube SCA.
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
In uuid.h the function uuid_generate_v5 had a 'namespace'
parameter which is a reserved C++ keywork.
Renamed to 'ns'.
Signed-off-by: Daniel Nicoletti <dantti12@gmail.com>
Allow passing structs with sys_dlist_t members as const without
discarding qualifiers if the API is used without modifying it.
Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
Allow passing structs with sys_slist_t/sys_sflist_t members as const
without discarding qualifiers if the API is used without modifying it.
Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
- The issue is caused by the MIN() macro, which expands to
(a)<(b)?(a):(b), where ring_buf_space_get()/ring_buf_size_get()
is used as 'b' and is evaluated twice. The issue occurs when
the (a)<(b) condition evaluates such that (b) is selected,
but the value of (b) changes between evaluations, resulting
in a possibly larger value than (a).
- Fixes the potential incorrect behavior by storing the result
of ring_buf_space_get()/ring_buf_size_get() in a variable
before using it in the MIN macro.
Signed-off-by: Andrej Butok <andrey.butok@nxp.com>
`util_eq` compare two memory areas and their length. It returns true if
they are equal, else false.
`util_memeq` the first n bytes of two memory areas. It returns true if
they are equal, else false.
Signed-off-by: Théo Battrel <theo.battrel@nordicsemi.no>
This adds the best HD=4 CRC32 polynomial. The discovery
is the result of research by Philip Koopman of Carnegie
Mellon University, and is well documented at
https://users.ece.cmu.edu/~koopman/crc/.
The user is given the option of trading 1024B of RAM to
improve the execution speed. The unit tests are parameterized
with this KConfig option.
Signed-off-by: JP Hutchins <jp@intercreate.io>
When sys_heap_realloc() is expressed in terms of sys_heap_aligned_realloc()
it invokes a longer aligned allocation code path with an extra runtime
overhead even though no alignment is necessary.
Let's reference and invoke the aligned allocation code path only when an
actual aligned allocation is requested. This opens the possibility for
the linker to garbage-collect the aligning code otherwise.
Improve realloc documentation while at it.
Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
When k_heap_alloc() is expressed in terms of k_heap_aligned_alloc()
it invokes a longer aligned allocation code path with an extra runtime
overhead even though no alignment is necessary.
Let's reference and invoke the aligned allocation code path only when an
actual aligned allocation is requested. This opens the possibility for
the linker to garbage-collect the aligning code otherwise.
Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
Convert z_clock_hw_cycles_per_sec to unsigned int to increase
supported frequency range.
Signed-off-by: Anisetti Avinash Krishna <anisetti.avinash.krishna@intel.com>
Type case CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC to uint32_t
while defining sys_clock_hw_cycles_per_sec_runtime_get()
to extend the range of frequency to 0xffffffff.
Signed-off-by: Anisetti Avinash Krishna <anisetti.avinash.krishna@intel.com>
Use compiler trick to allow operations with all types of arguments.
Previous solution was using arg+0 with suppressing compiler warning
about pointer arithmetic operations but it was not covering for
opaque struct pointers.
Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
The TOOLCHAIN_DISABLE_WARNING/TOOLCHAIN_ENABLE_WARNING macros are easier
to read and compiler agnostic.
Signed-off-by: Tom Hughes <tomhughes@chromium.org>
Fixing the precedence of the cast in z_tmcvt_int_div_32 and
z_tmcvt_int_mul_32 functions so the final result is of type
uint32_t.
Fixes#87226
Signed-off-by: Anisetti Avinash Krishna <anisetti.avinash.krishna@intel.com>
Adds a generic function that will count the number of bits set in
a value.
It uses POPCOUNT (e.g. __builtin_popcount for GCC) if available,
or else it will use Brian Kernighan’s Algorithm to count bits.
POPCOUNT will likely always support unsigned ints, but the function
was implemented to use it with uint8_t for the sake of simplicity
and compatibility with Brian Kernighan’s Algorithm.
A generic solution was chosen rather than a macro/function per
type (e.g. uint8_t, uint16_t, etc.) as that is easier to maintain
and also supports array types (e.g. counting the number of bits
in 128 or 256 octet arrays).
Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
Ifdef guard of CONFIG_SYS_HEAP_RUNTIME_STATS prevents using IS_ENABLED
macro, preferred way of guarding code. This commit fixes that by
removing ifdef macro.
Signed-off-by: Evgenii Kosenko <Evgenii.Kosenko@silabs.com>
Documentation comments describes sys_heap_runtime_stats_get function to
have stats_t argument, hoever function has stats. The commit fixes typo
introduces in a71cd8790f
Signed-off-by: Evgenii Kosenko <Evgenii.Kosenko@silabs.com>
Make struct ring_buf 12 bytes smaller by default. This comes with a 32KB
buffer size limit which covers almost all cases. The previous limit of
2GB can be restored with CONFIG_RING_BUFFER_LARGE=y.
Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
Add an optional handler to the p4wq to give the submitting code
(e.g. rtio workq) a possibility execute code after the work was
succesfully executed.
Signed-off-by: Florian Weber <Florian.Weber@live.de>
Within the K_THREAD_DEFINE macro, a nested set of macros is expanded to
populate the values of a global _static_thread_data structure.
The K_THREAD_DEFINE macro expands to statements including the
Z_THREAD_COMMON_DEFINE macro, which expands to statements including the
Z_THREAD_INITIALIZER macro, which expands to an initialization expression
of a _static_thread_data struct. The init_delay member of that struct is
then initialized with the Z_THREAD_INIT_DELAY_INITIALIZER macro, which
expands to an expression including the SYS_TIMEOUT_MS macro which
converts a number of milliseconds to a number of ticks.
For example, take the following macro:
K_THREAD_DEFINE(
thread_X,
STACKSIZE,
thread_X_entry_point, NULL, NULL, NULL,
PRIORITY,
0,
0);
In abbreviated form, it expands as follows:
typedef struct {
int ticks;
} k_timeout_t;
struct _static_thread_data {
/* ... */
k_timeout_t init_delay;
};
struct _static_thread_data _k_thread_data_thread_X = {
/* ... */
.init_delay = (k_timeout_t){ .ticks = 0 }
};
However, in GCC versions before 5.1, the code fails to compile with the
error "initializer element is not constant", when compiled with the
following options:
gcc -std=gnu99 -c test.c
In the above code, the error can be corrected by replacing...
.init_delay = (k_timeout_t){ .ticks = 0 }
...with...
.init_delay = { .ticks = 0 }
...i.e. removing initialization with a compound literal.
In order to achieve this, this patch reworks the system of macros.
The Z_TIMEOUT_TICKS(t) macro is refactored into Z_TIMEOUT_TICKS_INIT(t)
which defines the initializer part: { .ticks = t }, and Z_TIMEOUT_TICKS(t)
which is defined as ((k_timeout_t) Z_TIMEOUT_TICKS_INIT(t)) .
For symmetry, Z_TIMEOUT_NO_WAIT_INIT is split out of Z_TIMEOUT_NO_WAIT.
Similarly, SYS_TIMEOUT_MS_INIT(ms) is split out of SYS_TIMEOUT_MS(ms) so
that the Z_THREAD_INIT_DELAY_INITIALIZER(ms) macro can use
SYS_TIMEOUT_MS_INIT(ms) which expands Z_TIMEOUT_TICKS_INIT(t) to initialize
init_delay without using a compound literal.
Signed-off-by: Joel Holdsworth <jholdsworth@nvidia.com>
To request heap statistics, a pointer to a heap structure is required.
This is straightforward for a user-defined heap. However, such a pointer
is not known for heaps created by other components or libraries, like
libc. Therefore, it is not possible to calculate the total heap memory.
The proposed solution is to use an array of pointers, which is filled in
on every sys_heap_init() call. One can then iterate through it to sum up
the total memory allocated for all heaps.
The array size is configurable. The default array size is zero,
which means the feature is disabled. Any other integer greater then zero
defines the array size and enables the feature.
A list of pointers instead of an array could be another approach,
but it requeres a heap, which is not always available.
Signed-off-by: Ivan Pankratov <ivan.pankratov@silabs.com>
The `k_pipe_*` API has been reworked to provide a more consistent and
intuitive interface. The new API aims to provide a simple to use byte
stream interface that is more in line with the POSIX pipe API.
The previous API has been deprecated and will be removed in a future
release.
Signed-off-by: Måns Ansgariusson <Mansgariusson@gmail.com>
Add endian specific buffer convert and copy functions.
This is helpful when you want to convert arbitrary data lengths
but only swapping byte order when needed.
Signed-off-by: Joakim Andersson <joerchan@gmail.com>
This macro is defined in a few places which leads to macro redefinition
error e.g. when compiling prometheus network sample for NPCX boards.
Provide one definition of IS_BIT_SET() in util_macro.h to fix the
problem.
Signed-off-by: Patryk Duda <patrykd@google.com>
Dequeuing from a doubly linked list is similar to removing an item
except that it does not re-initialize the dequeued node.
This comes in handy when sorting a doubly linked list (where the
node gets removed and re-added). In that circumstance, re-initializing
the node is required. Furthermore, the compiler does not always
'understand' this. Thus, when performance is critical, dequeuing
may be preferred to removing.
Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
LOG2CEIL(1) results in undefined behaviour,
as LOG2CEIL(1) expands to __builtin_clz(0),
which is undefined according to GCC docs.
Signed-off-by: Yanir Levin <yanir10@gmail.com>
Add type check to avoid comparing unsigned values with 0 which is always
true or false and can cause coverity issue.
Signed-off-by: Trung Hieu Le <trunghieu.le@nxp.com>
Obviously, everyone knows that there are 8 bits per byte, so
there isn't a lot of magic happening, per se, but it's also
helpful to clearly denote where the magic number 8 is referring
to the number of bits in a byte.
Occasionally, 8 will refer to a field size or offset in a
structure, MMR, or word. Occasionally, the number 8 will refer
to the number of bytes in a 64-bit value (which should probably
be replaced with `sizeof(uint64_t)`).
For converting bits to bytes, or vice-versa, let's use
`BITS_PER_BYTE` for clarity (or other appropriate `BITS_PER_*`
macros).
Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
Add support for realloc (and realloc_aligned) into the multi heap lib,
where the buffer sent in will either be reused (maybe shrinked),
or enlarged by allocating on any of the matching heaps of the multi heap.
Signed-off-by: Meir Komet <mskomet1@gmail.com>