The Pedigree Project 0.1
utility.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef KERNEL_UTILITY_H
21#define KERNEL_UTILITY_H
22#include "pedigree/kernel/compiler.h"
23#include "pedigree/kernel/processor/types.h"
24
25#include <config.h>
26
27// Export memcpy et al
28#include "pedigree/kernel/utilities/cpp.h" // IWYU pragma: export
29#include "pedigree/kernel/utilities/lib.h" // IWYU pragma: export
30
31#ifdef __cplusplus
32#include <bit>
33#endif
34
38#ifdef __cplusplus
39constexpr uint8_t BS8(uint8_t value) noexcept {
40 return value;
41}
42
43constexpr uint16_t BS16(uint16_t value) noexcept {
44 return std::byteswap(value);
45}
46
47constexpr uint32_t BS32(uint32_t value) noexcept {
48 return std::byteswap(value);
49}
50
51constexpr uint64_t BS64(uint64_t value) noexcept {
52 return std::byteswap(value);
53}
54#else
55#define BS8(x) ((uint8_t)(x))
56#define BS16(x) __builtin_bswap16((uint16_t)(x))
57#define BS32(x) __builtin_bswap32((uint32_t)(x))
58#define BS64(x) __builtin_bswap64((uint64_t)(x))
59#endif
60
61#if TARGET_IS_LITTLE_ENDIAN
62
63#define LITTLE_TO_HOST8(x) (x)
64#define LITTLE_TO_HOST16(x) (x)
65#define LITTLE_TO_HOST32(x) (x)
66#define LITTLE_TO_HOST64(x) (x)
67
68#define HOST_TO_LITTLE8(x) (x)
69#define HOST_TO_LITTLE16(x) (x)
70#define HOST_TO_LITTLE32(x) (x)
71#define HOST_TO_LITTLE64(x) (x)
72
73#define BIG_TO_HOST8(x) BS8((x))
74#define BIG_TO_HOST16(x) BS16((x))
75#define BIG_TO_HOST32(x) BS32((x))
76#define BIG_TO_HOST64(x) BS64((x))
77
78#define HOST_TO_BIG8(x) BS8((x))
79#define HOST_TO_BIG16(x) BS16((x))
80#define HOST_TO_BIG32(x) BS32((x))
81#define HOST_TO_BIG64(x) BS64((x))
82
83#else // else Big endian
84
85#define BIG_TO_HOST8(x) (x)
86#define BIG_TO_HOST16(x) (x)
87#define BIG_TO_HOST32(x) (x)
88#define BIG_TO_HOST64(x) (x)
89
90#define HOST_TO_BIG8(x) (x)
91#define HOST_TO_BIG16(x) (x)
92#define HOST_TO_BIG32(x) (x)
93#define HOST_TO_BIG64(x) (x)
94
95#define LITTLE_TO_HOST8(x) BS8((x))
96#define LITTLE_TO_HOST16(x) BS16((x))
97#define LITTLE_TO_HOST32(x) BS32((x))
98#define LITTLE_TO_HOST64(x) BS64((x))
99
100#define HOST_TO_LITTLE8(x) BS8((x))
101#define HOST_TO_LITTLE16(x) BS16((x))
102#define HOST_TO_LITTLE32(x) BS32((x))
103#define HOST_TO_LITTLE64(x) BS64((x))
104
105#endif
106
107#define MAX_FUNCTION_NAME 128
108#define MAX_PARAMS 32
109#define MAX_PARAM_LENGTH 64
110
112EXPORTED_PUBLIC void* page_align(void* p) PURE;
113
114#ifdef __cplusplus
119template <typename T1, typename T2 = T1>
120T2* adjust_pointer(T1* pointer, ssize_t offset) {
121 return reinterpret_cast<T2*>(reinterpret_cast<intptr_t>(pointer) + offset);
122}
123
124template <typename T>
125void swap(T a, T b) {
126 T t = a;
127 a = b;
128 b = t;
129}
130
132template <typename T1, typename T2>
133intptr_t pointer_diff(const T1* a, const T2* b) {
134 return reinterpret_cast<uintptr_t>(b) - reinterpret_cast<uintptr_t>(a);
135}
136
137template <typename T1, typename T2>
138constexpr intptr_t pointer_diff_const(T1* a, T2* b) {
139 return reinterpret_cast<uintptr_t>(b) - reinterpret_cast<uintptr_t>(a);
140}
141
143template <typename T1, typename T2>
144uintptr_t abs_difference(T1 a, T2 b) {
145 intptr_t value = b - a;
146 if (value < 0)
147 value *= -1;
148 return value;
149}
150#endif
151
154#endif
EXPORTED_PUBLIC void * page_align(void *p) PURE
Definition utility.cc:29