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
30L4_INLINE int l4util_human_readable_size(char *outstr, size_t outsize,
31 unsigned long long bytes)
32{
33 static char const *const unitstr = "BKMGT";
34
35 int idx = sizeof(unitstr) - 2;
36 int order;
37
38 for (order = idx * 10; order > 10; order -= 10, --idx)
39 if (bytes > (1ULL << order))
40 break;
41
42 unsigned long long value = bytes >> order;
43 unsigned long long fract = (bytes - (value << order))
44 / ((1ULL << order) / 10 + 1);
45
46 return snprintf(outstr, outsize, "%llu.%1llu %ciB",
47 value, fract, unitstr[idx]);
48}
L4 compiler related defines.
#define L4_INLINE
L4 Inline function attribute.
Definition compiler.h:51