source: rtems/cpukit/score/include/rtems/score/address.h @ 89f8eab5

4.115
Last change on this file since 89f8eab5 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 5.2 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 offsets.
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 * @note  The cast of an address to an uint32_t makes this code
91 *        dependent on an addresses being thirty two bits.
92 */
93RTEMS_INLINE_ROUTINE int32_t _Addresses_Subtract (
94  const void *left,
95  const void *right
96)
97{
98  return (int32_t) ((const char *) left - (const char *) right);
99}
100
101/**
102 * @brief Is address aligned.
103 *
104 * This function returns true if the given address is correctly
105 * aligned for this processor and false otherwise.  Proper alignment
106 * is based on correctness and efficiency.
107 *
108 * @param[in] address is the address being checked for alignment.
109 *
110 * @retval true The @a address is aligned.
111 * @retval false The @a address is not aligned.
112 */
113RTEMS_INLINE_ROUTINE bool _Addresses_Is_aligned (
114  const void *address
115)
116{
117#if (CPU_ALIGNMENT == 0)
118    return true;
119#else
120    return (((uintptr_t)address % CPU_ALIGNMENT) == 0);
121#endif
122}
123
124/**
125 * @brief Is address in range.
126 *
127 * This function returns true if the given address is within the
128 * memory range specified and false otherwise.  base is the address
129 * of the first byte in the memory range and limit is the address
130 * of the last byte in the memory range.  The base address is
131 * assumed to be lower than the limit address.
132 *
133 * @param[in] address is the address to check.
134 * @param[in] base is the lowest address of the range to check against.
135 * @param[in] limit is the highest address of the range to check against.
136 *
137 * @retval true The @a address is within the memory range specified
138 * @retval false The @a address is not within the memory range specified.
139 */
140RTEMS_INLINE_ROUTINE bool _Addresses_Is_in_range (
141  const void *address,
142  const void *base,
143  const void *limit
144)
145{
146  return (address >= base && address <= limit);
147}
148
149/**
150 * @brief Align address to nearest multiple of alignment, rounding up.
151 *
152 * This function returns the given address aligned to the given alignment.
153 * If the address already is aligned, or if alignment is 0, the address is
154 * returned as is. The returned address is greater than or equal to the
155 * given address.
156 *
157 * @param[in] address is the address to align.
158 * @param[in] alignment is the boundary for alignment and must be a power of 2
159 *
160 * @return Returns the aligned address.
161 */
162RTEMS_INLINE_ROUTINE void *_Addresses_Align_up(
163  void *address,
164  size_t alignment
165)
166{
167  uintptr_t mask = alignment - (uintptr_t)1;
168  return (void*)(((uintptr_t)address + mask) & ~mask);
169}
170
171/**
172 * @brief Align address to nearest multiple of alignment, truncating.
173 *
174 * This function returns the given address aligned to the given alignment.
175 * If the address already is aligned, or if alignment is 0, the address is
176 * returned as is. The returned address is less than or equal to the
177 * given address.
178 *
179 * @param[in] address is the address to align.
180 * @param[in] alignment is the boundary for alignment and must be a power of 2.
181 *
182 * @return Returns the aligned address.
183 */
184RTEMS_INLINE_ROUTINE void *_Addresses_Align_down(
185  void *address,
186  size_t alignment
187)
188{
189  uintptr_t mask = alignment - (uintptr_t)1;
190  return (void*)((uintptr_t)address & ~mask);
191}
192
193/**@}*/
194
195#ifdef __cplusplus
196}
197#endif
198
199#endif
200/* end of include file */
Note: See TracBrowser for help on using the repository browser.