L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
printf_helpers.h
1/*
2 * Copyright (C) 2025 Kernkonzept GmbH.
3 * Author(s): Frank Mehnert <frank.mehnert@kernkonzept.com>
4 *
5 * License: see LICENSE.spdx (in this directory or the directories above)
6 */
7
8#pragma once
9
10#include <stddef.h>
11#include <stdio.h>
12#include <l4/sys/compiler.h>
13
31L4_INLINE int l4util_human_readable_size(char *outstr, size_t outsize,
32 unsigned long long bytes)
33{
34 static char const unitstr[7] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E' };
35
36 int idx = sizeof(unitstr) - 1;
37 int order;
38
39 for (order = idx * 10; order >= 10; order -= 10, --idx)
40 if (bytes > (1ULL << order))
41 break;
42
43 unsigned long long value = bytes >> order;
44 unsigned long long fract = (bytes - (value << order))
45 / ((1ULL << order) / 10 + 1);
46
47 if (idx > 0)
48 return snprintf(outstr, outsize, "%llu.%1llu %ciB",
49 value, fract, unitstr[idx]);
50 else
51 return snprintf(outstr, outsize, "%llu B ", value);
52}
53
75L4_INLINE int l4util_human_readable_size_region(char *outstr, size_t outsize,
76 unsigned long long begin,
77 unsigned long long end)
78{
79 if (end - begin == ~0UL)
80 return snprintf(outstr, outsize, "%s",
81 sizeof(long) == 4 ? "4.0 GiB" : "16.0 EiB");
82 else
83 return l4util_human_readable_size(outstr, outsize, end - begin + 1);
84}
L4 compiler related defines.
#define L4_INLINE
L4 Inline function attribute.
Definition compiler.h:51