1 | /** |
---|
2 | * @file rtems/score/address.h |
---|
3 | * |
---|
4 | * @brief Information Required to Manipulate Physical Addresses |
---|
5 | * |
---|
6 | * This include file contains the information required to manipulate |
---|
7 | * physical addresses. |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * COPYRIGHT (c) 1989-2006. |
---|
12 | * On-Line Applications Research Corporation (OAR). |
---|
13 | * |
---|
14 | * The license and distribution terms for this file may be |
---|
15 | * found in the file LICENSE in this distribution or at |
---|
16 | * http://www.rtems.org/license/LICENSE. |
---|
17 | */ |
---|
18 | |
---|
19 | #ifndef _RTEMS_SCORE_ADDRESS_H |
---|
20 | #define _RTEMS_SCORE_ADDRESS_H |
---|
21 | |
---|
22 | #include <rtems/score/cpu.h> |
---|
23 | |
---|
24 | #ifdef __cplusplus |
---|
25 | extern "C" { |
---|
26 | #endif |
---|
27 | |
---|
28 | /** |
---|
29 | * @defgroup ScoreAddress Address Handler |
---|
30 | * |
---|
31 | * @ingroup Score |
---|
32 | * |
---|
33 | * This handler encapsulates functionality which abstracts address |
---|
34 | * manipulation in a portable manner. |
---|
35 | */ |
---|
36 | /**@{*/ |
---|
37 | |
---|
38 | /** |
---|
39 | * @brief Add offset to an address. |
---|
40 | * |
---|
41 | * This function is used to add an @a offset to a @a base address. |
---|
42 | * It returns the resulting address. This address is typically |
---|
43 | * converted to an access type before being used further. |
---|
44 | * |
---|
45 | * @param[in] base is the base address. |
---|
46 | * @param[in] offset is the offset to add to @a base. |
---|
47 | * |
---|
48 | * @return This method returns the resulting address. |
---|
49 | */ |
---|
50 | RTEMS_INLINE_ROUTINE void *_Addresses_Add_offset ( |
---|
51 | const void *base, |
---|
52 | uintptr_t offset |
---|
53 | ) |
---|
54 | { |
---|
55 | return (void *)((uintptr_t)base + offset); |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * @brief Subtract offset from offset. |
---|
60 | * |
---|
61 | * This function is used to subtract an @a offset from a @a base |
---|
62 | * address. It returns the resulting address. This address is |
---|
63 | * typically converted to an access type before being used further. |
---|
64 | * |
---|
65 | * @param[in] base is the base address. |
---|
66 | * @param[in] offset is the offset to subtract to @a base. |
---|
67 | * |
---|
68 | * @return This method returns the resulting address. |
---|
69 | */ |
---|
70 | |
---|
71 | RTEMS_INLINE_ROUTINE void *_Addresses_Subtract_offset ( |
---|
72 | const void *base, |
---|
73 | uintptr_t offset |
---|
74 | ) |
---|
75 | { |
---|
76 | return (void *)((uintptr_t)base - offset); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * @brief Subtract two addresses. |
---|
81 | * |
---|
82 | * This function is used to subtract two addresses. It returns the |
---|
83 | * resulting offset. |
---|
84 | * |
---|
85 | * @param[in] left is the address on the left hand side of the subtraction. |
---|
86 | * @param[in] right is the address on the right hand side of the subtraction. |
---|
87 | * |
---|
88 | * @return This method returns the resulting address. |
---|
89 | */ |
---|
90 | RTEMS_INLINE_ROUTINE intptr_t _Addresses_Subtract( |
---|
91 | const void *left, |
---|
92 | const void *right |
---|
93 | ) |
---|
94 | { |
---|
95 | return (intptr_t) ( (const char *) left - (const char *) right ); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * @brief Is address aligned. |
---|
100 | * |
---|
101 | * This function returns true if the given address is correctly |
---|
102 | * aligned for this processor and false otherwise. Proper alignment |
---|
103 | * is based on correctness and efficiency. |
---|
104 | * |
---|
105 | * @param[in] address is the address being checked for alignment. |
---|
106 | * |
---|
107 | * @retval true The @a address is aligned. |
---|
108 | * @retval false The @a address is not aligned. |
---|
109 | */ |
---|
110 | RTEMS_INLINE_ROUTINE bool _Addresses_Is_aligned( |
---|
111 | const void *address |
---|
112 | ) |
---|
113 | { |
---|
114 | return ( (uintptr_t) address % CPU_ALIGNMENT ) == 0; |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * @brief Is address in range. |
---|
119 | * |
---|
120 | * This function returns true if the given address is within the |
---|
121 | * memory range specified and false otherwise. base is the address |
---|
122 | * of the first byte in the memory range and limit is the address |
---|
123 | * of the last byte in the memory range. The base address is |
---|
124 | * assumed to be lower than the limit address. |
---|
125 | * |
---|
126 | * @param[in] address is the address to check. |
---|
127 | * @param[in] base is the lowest address of the range to check against. |
---|
128 | * @param[in] limit is the highest address of the range to check against. |
---|
129 | * |
---|
130 | * @retval true The @a address is within the memory range specified |
---|
131 | * @retval false The @a address is not within the memory range specified. |
---|
132 | */ |
---|
133 | RTEMS_INLINE_ROUTINE bool _Addresses_Is_in_range ( |
---|
134 | const void *address, |
---|
135 | const void *base, |
---|
136 | const void *limit |
---|
137 | ) |
---|
138 | { |
---|
139 | return (address >= base && address <= limit); |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * @brief Align address to nearest multiple of alignment, rounding up. |
---|
144 | * |
---|
145 | * This function returns the given address aligned to the given alignment. |
---|
146 | * If the address already is aligned, or if alignment is 0, the address is |
---|
147 | * returned as is. The returned address is greater than or equal to the |
---|
148 | * given address. |
---|
149 | * |
---|
150 | * @param[in] address is the address to align. |
---|
151 | * @param[in] alignment is the boundary for alignment and must be a power of 2 |
---|
152 | * |
---|
153 | * @return Returns the aligned address. |
---|
154 | */ |
---|
155 | RTEMS_INLINE_ROUTINE void *_Addresses_Align_up( |
---|
156 | void *address, |
---|
157 | size_t alignment |
---|
158 | ) |
---|
159 | { |
---|
160 | uintptr_t mask = alignment - (uintptr_t)1; |
---|
161 | return (void*)(((uintptr_t)address + mask) & ~mask); |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * @brief Align address to nearest multiple of alignment, truncating. |
---|
166 | * |
---|
167 | * This function returns the given address aligned to the given alignment. |
---|
168 | * If the address already is aligned, or if alignment is 0, the address is |
---|
169 | * returned as is. The returned address is less than or equal to the |
---|
170 | * given address. |
---|
171 | * |
---|
172 | * @param[in] address is the address to align. |
---|
173 | * @param[in] alignment is the boundary for alignment and must be a power of 2. |
---|
174 | * |
---|
175 | * @return Returns the aligned address. |
---|
176 | */ |
---|
177 | RTEMS_INLINE_ROUTINE void *_Addresses_Align_down( |
---|
178 | void *address, |
---|
179 | size_t alignment |
---|
180 | ) |
---|
181 | { |
---|
182 | uintptr_t mask = alignment - (uintptr_t)1; |
---|
183 | return (void*)((uintptr_t)address & ~mask); |
---|
184 | } |
---|
185 | |
---|
186 | /**@}*/ |
---|
187 | |
---|
188 | #ifdef __cplusplus |
---|
189 | } |
---|
190 | #endif |
---|
191 | |
---|
192 | #endif |
---|
193 | /* end of include file */ |
---|