Skip to content

Commit

Permalink
kernel: types: add k_off_t and k_ssize_t types
Browse files Browse the repository at this point in the history
Define k_off_t to have native support for measuring file offsets
within any kind of file that is supportable in Zephyr.

CONFIG_OFFSET_64BIT is added to allow 32-bit users to force
64-bit offsets, when trading space and speed for flexibility
makes sense for the application.

Define k_ssize_t to have native support for reporting file I/O
sizes along with negative error codes.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
  • Loading branch information
cfriedt committed Jul 9, 2024
1 parent c2047c0 commit 38d2805
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
38 changes: 38 additions & 0 deletions include/zephyr/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,44 @@ typedef union {
void (*thepfunc)(void);
} z_max_align_t;

/*
* Offset type
*
* k_off_t, much like the POSIX off_t, is a signed integer type used to represent file sizes and
* offsets.
*
* k_off_t effectively limits the size of files that can be handled by the system, it is
* therefore not tied to the word-size of the architecture.
*
* In order to overcome historical limitations of the off_t type, Zephyr may be configured to
* always define k_off_t as 64-bits.
*/
#ifdef CONFIG_OFFSET_64BIT
typedef int64_t k_off_t;
#else
typedef long k_off_t;
#endif

/*
* Signed size type
*
* k_ssize_t, much like the POSIX ssize_t, is a signed integer type that effectively limits the
* size of I/O operations on files while still supporting negative return values.
*
* k_ssize_t is usually tied to the word-size of the architecture.
*/
#ifdef __SIZE_TYPE__
#define unsigned signed /* parasoft-suppress MISRAC2012-RULE_20_4-a MISRAC2012-RULE_20_4-b */
typedef __SIZE_TYPE__ k_ssize_t;
#undef unsigned
#else
#ifdef CONFIG_64BIT
typedef long k_ssize_t;
#else
typedef int k_ssize_t;
#endif
#endif

#ifdef __cplusplus
/* Zephyr requires an int main(void) signature with C linkage for the application main if present */
extern int main(void);
Expand Down
17 changes: 17 additions & 0 deletions kernel/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,23 @@ config XIP
supply a linker command file when building your image. Enabling this
option increases both the code and data footprint of the image.

config OFFSET_64BIT
bool "Use 64-bit offsets"
help
Select 'y' here to ensure k_off_t is always 64-bits. This may be
useful on platforms with files, disks, and memories larger than 2 GiB.

If this option is not selected (the default), then the size of k_off_t
will match the architecture word size (like size_t does currently).

On 32-bit platforms, enabling this feature trades space and speed for
flexibiltity, since using 64-bit offsets may require more instructions
for a given operation. For example, a 64-bit load may require two 32-bit
loads, and a 64-bit divide may need to be completed with several
32-bit instructions (the operation is emulated via a compiler built-in
function).

Note: offsets have a signed integer representation.

menu "Security Options"

Expand Down

0 comments on commit 38d2805

Please sign in to comment.