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 
23 #include "pedigree/kernel/compiler.h"
24 #include "pedigree/kernel/processor/types.h"
25 
26 // Export memcpy et al
27 #include "pedigree/kernel/utilities/cpp.h" // IWYU pragma: export
28 #include "pedigree/kernel/utilities/lib.h" // IWYU pragma: export
29 
30 #if defined(HOSTED) && !defined(UTILITY_LINUX)
31 // Override headers we are replacing.
32 #define _STRING_H 1
33 #endif
34 
38 // Endianness shizzle.
39 #define BS8(x) (x)
40 #define BS16(x) (((x & 0xFF00) >> 8) | ((x & 0x00FF) << 8))
41 #define BS32(x) \
42  (((x & 0xFF000000) >> 24) | ((x & 0x00FF0000) >> 8) | \
43  ((x & 0x0000FF00) << 8) | ((x & 0x000000FF) << 24))
44 #define BS64(x) (x)
45 
46 #ifdef TARGET_IS_LITTLE_ENDIAN
47 
48 #define LITTLE_TO_HOST8(x) (x)
49 #define LITTLE_TO_HOST16(x) (x)
50 #define LITTLE_TO_HOST32(x) (x)
51 #define LITTLE_TO_HOST64(x) (x)
52 
53 #define HOST_TO_LITTLE8(x) (x)
54 #define HOST_TO_LITTLE16(x) (x)
55 #define HOST_TO_LITTLE32(x) (x)
56 #define HOST_TO_LITTLE64(x) (x)
57 
58 #define BIG_TO_HOST8(x) BS8((x))
59 #define BIG_TO_HOST16(x) BS16((x))
60 #define BIG_TO_HOST32(x) BS32((x))
61 #define BIG_TO_HOST64(x) BS64((x))
62 
63 #define HOST_TO_BIG8(x) BS8((x))
64 #define HOST_TO_BIG16(x) BS16((x))
65 #define HOST_TO_BIG32(x) BS32((x))
66 #define HOST_TO_BIG64(x) BS64((x))
67 
68 #else // else Big endian
69 
70 #define BIG_TO_HOST8(x) (x)
71 #define BIG_TO_HOST16(x) (x)
72 #define BIG_TO_HOST32(x) (x)
73 #define BIG_TO_HOST64(x) (x)
74 
75 #define HOST_TO_BIG8(x) (x)
76 #define HOST_TO_BIG16(x) (x)
77 #define HOST_TO_BIG32(x) (x)
78 #define HOST_TO_BIG64(x) (x)
79 
80 #define LITTLE_TO_HOST8(x) BS8((x))
81 #define LITTLE_TO_HOST16(x) BS16((x))
82 #define LITTLE_TO_HOST32(x) BS32((x))
83 #define LITTLE_TO_HOST64(x) BS64((x))
84 
85 #define HOST_TO_LITTLE8(x) BS8((x))
86 #define HOST_TO_LITTLE16(x) BS16((x))
87 #define HOST_TO_LITTLE32(x) BS32((x))
88 #define HOST_TO_LITTLE64(x) BS64((x))
89 
90 #endif
91 
92 #define MAX_FUNCTION_NAME 128
93 #define MAX_PARAMS 32
94 #define MAX_PARAM_LENGTH 64
95 
97 EXPORTED_PUBLIC void *page_align(void *p) PURE;
98 
99 #ifdef __cplusplus
100 
104 template <typename T>
105 inline T *adjust_pointer(T *pointer, ssize_t offset)
106 {
107  return reinterpret_cast<T *>(reinterpret_cast<intptr_t>(pointer) + offset);
108 }
109 
110 template <typename T>
111 inline void swap(T a, T b)
112 {
113  T t = a;
114  a = b;
115  b = t;
116 }
117 
119 template <typename T1, typename T2>
120 inline intptr_t pointer_diff(const T1 *a, const T2 *b)
121 {
122  return reinterpret_cast<uintptr_t>(b) - reinterpret_cast<uintptr_t>(a);
123 }
124 
125 template <typename T1, typename T2>
126 constexpr intptr_t pointer_diff_const(T1 *a, T2 *b)
127 {
128  return reinterpret_cast<uintptr_t>(b) - reinterpret_cast<uintptr_t>(a);
129 }
130 
132 template <typename T1, typename T2>
133 inline uintptr_t abs_difference(T1 a, T2 b)
134 {
135  intptr_t value = b - a;
136  if (value < 0)
137  value *= -1;
138  return value;
139 }
140 #endif
141 
144 #endif
EXPORTED_PUBLIC void * page_align(void *p) PURE
Definition: utility.cc:28