source: rtems/cpukit/include/rtems/score/address.h @ 21275b58

Last change on this file since 21275b58 was b2de426, checked in by Sebastian Huber <sebastian.huber@…>, on Aug 6, 2018 at 9:53:42 AM

score: Fix _Addresses_Subtract()

Use architecture-specific integer type for an address difference.

Update #3486.

  • Property mode set to 100644
File size: 5.1 KB
Line 
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
25extern "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 */
50RTEMS_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
71RTEMS_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 */
90RTEMS_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 */
110RTEMS_INLINE_ROUTINE bool _Addresses_Is_aligned (
111  const void *address
112)
113{
114#if (CPU_ALIGNMENT == 0)
115    return true;
116#else
117    return (((uintptr_t)address % CPU_ALIGNMENT) == 0);
118#endif
119}
120
121/**
122 * @brief Is address in range.
123 *
124 * This function returns true if the given address is within the
125 * memory range specified and false otherwise.  base is the address
126 * of the first byte in the memory range and limit is the address
127 * of the last byte in the memory range.  The base address is
128 * assumed to be lower than the limit address.
129 *
130 * @param[in] address is the address to check.
131 * @param[in] base is the lowest address of the range to check against.
132 * @param[in] limit is the highest address of the range to check against.
133 *
134 * @retval true The @a address is within the memory range specified
135 * @retval false The @a address is not within the memory range specified.
136 */
137RTEMS_INLINE_ROUTINE bool _Addresses_Is_in_range (
138  const void *address,
139  const void *base,
140  const void *limit
141)
142{
143  return (address >= base && address <= limit);
144}
145
146/**
147 * @brief Align address to nearest multiple of alignment, rounding up.
148 *
149 * This function returns the given address aligned to the given alignment.
150 * If the address already is aligned, or if alignment is 0, the address is
151 * returned as is. The returned address is greater than or equal to the
152 * given address.
153 *
154 * @param[in] address is the address to align.
155 * @param[in] alignment is the boundary for alignment and must be a power of 2
156 *
157 * @return Returns the aligned address.
158 */
159RTEMS_INLINE_ROUTINE void *_Addresses_Align_up(
160  void *address,
161  size_t alignment
162)
163{
164  uintptr_t mask = alignment - (uintptr_t)1;
165  return (void*)(((uintptr_t)address + mask) & ~mask);
166}
167
168/**
169 * @brief Align address to nearest multiple of alignment, truncating.
170 *
171 * This function returns the given address aligned to the given alignment.
172 * If the address already is aligned, or if alignment is 0, the address is
173 * returned as is. The returned address is less than or equal to the
174 * given address.
175 *
176 * @param[in] address is the address to align.
177 * @param[in] alignment is the boundary for alignment and must be a power of 2.
178 *
179 * @return Returns the aligned address.
180 */
181RTEMS_INLINE_ROUTINE void *_Addresses_Align_down(
182  void *address,
183  size_t alignment
184)
185{
186  uintptr_t mask = alignment - (uintptr_t)1;
187  return (void*)((uintptr_t)address & ~mask);
188}
189
190/**@}*/
191
192#ifdef __cplusplus
193}
194#endif
195
196#endif
197/* end of include file */
Note: See TracBrowser for help on using the repository browser.