source: rtems/cpukit/libdl/rtl-indirect-ptr.h @ 8709aa04

5
Last change on this file since 8709aa04 was d4edbdbc, checked in by Sebastian Huber <sebastian.huber@…>, on 03/20/15 at 13:09:26

Replace www.rtems.com with www.rtems.org

  • Property mode set to 100644
File size: 5.7 KB
Line 
1/*
2 *  COPYRIGHT (c) 2012 Chris Johns <chrisj@rtems.org>
3 *
4 *  The license and distribution terms for this file may be
5 *  found in the file LICENSE in this distribution or at
6 *  http://www.rtems.org/license/LICENSE.
7 */
8/**
9 * @file
10 *
11 * @ingroup rtems_rtl
12 *
13 * @brief RTEMS Run-Time Linker Indirect Pointer Management allows memory
14 *        compaction in the allocator.
15 */
16
17#if !defined (_RTEMS_RTL_INDIRECT_PTR_H_)
18#define _RTEMS_RTL_INDIRECT_PTR_H_
19
20#ifdef __cplusplus
21extern "C" {
22#endif /* __cplusplus */
23
24#include <rtems/chain.h>
25
26/**
27 * The RTL Indirect pointer.
28 */
29struct rtems_rtl_ptr_s {
30  rtems_chain_node node;     /**< Indirect pointers are held on lists. */
31  void*            pointer;  /**< The actual pointer. */
32};
33
34typedef struct rtems_rtl_ptr_s rtems_rtl_ptr_t;
35
36/**
37 * The RTL Indirect size and pointer.
38 */
39struct rtems_rtl_sptr_s {
40  rtems_rtl_ptr_t  ptr;      /**< The indirect pointer. */
41  size_t           size;     /**< The size of the memory block. */
42};
43
44typedef struct rtems_rtl_sptr_s rtems_rtl_sptr_t;
45
46/**
47 * A chain of indirect pointers for users to chain in applications.
48 *
49 * @note The chain the pointer is on is internal to the allocator and cannot be
50 *       used by applications.
51 */
52struct rtems_rtl_ptr_chain_s {
53  rtems_chain_node node;  /**< Chain of indirect pointers. */
54  rtems_rtl_ptr_t  ptr;   /**< The indirect pointer. */
55};
56
57typedef struct rtems_rtl_ptr_chain_s rtems_rtl_ptr_chain_t;
58
59/**
60 * A chain of indirect sized pointers for users to chain in applications.
61 *
62 * @note The chain the pointer is on is internal to the allocator and cannot be
63 *       used by applications.
64 */
65struct rtems_rtl_sptr_chain_s {
66  rtems_chain_node node;  /**< Chain of indirect pointers. */
67  rtems_rtl_sptr_t  ptr;  /**< The indirect pointer. */
68};
69
70typedef struct rtems_rtl_sptr_chain_s rtems_rtl_sptr_chain_t;
71
72/**
73 * Get the pointer given an indirect handle.
74 *
75 * @param handle The handle the pointer is returned from.
76 * @return void* The pointer held in the handle.
77 */
78static inline void* rtems_rtl_ptr_get (rtems_rtl_ptr_t* handle)
79{
80  return handle->pointer;
81}
82
83/**
84 * Set the pointer given an indirect handle and the pointer.
85 *
86 * @param handle The handle the pointer is returned from.
87 * @param pointer The pointer to set in the handle.
88 */
89static inline void rtems_rtl_ptr_set (rtems_rtl_ptr_t* handle, void* pointer)
90{
91  handle->pointer = pointer;
92}
93
94/**
95 * Initialise the indirect handle.
96 *
97 * @param handle The handle to initialise.
98 */
99static inline void rtems_rtl_ptr_init (rtems_rtl_ptr_t* handle)
100{
101  rtems_chain_set_off_chain (&handle->node);
102  handle->pointer = NULL;
103}
104
105/**
106 * Is the indirect handle NULL ?
107 *
108 * @param handle The handle to test.
109 * @return bool True if the pointer is NULL.
110 */
111static inline bool rtems_rtl_ptr_null (rtems_rtl_ptr_t* handle)
112{
113  return handle->pointer == NULL;
114}
115
116/**
117 * Move the allocated pointer from one handle to another. The source handle is
118 * cleared and removed from the list of handles.
119 *
120 * @param src The source handle to move the pointer from.
121 * @param dst The destination handle to receive the pointer.
122 */
123static inline void rtems_rtl_ptr_move (rtems_rtl_ptr_t* dst, rtems_rtl_ptr_t* src)
124{
125  /*
126   * We do not know which chain the src handle resides on so insert the dst
127   * handle after the src handle then extract the src handle.
128   */
129  rtems_chain_insert_unprotected (&src->node, &dst->node);
130  rtems_chain_extract_unprotected (&src->node);
131  dst->pointer = src->pointer;
132  rtems_rtl_ptr_init (src);
133}
134
135/**
136 * Return the pointer as the type provided.
137 *
138 * @param _h The handle.
139 * @param _t The type.
140 */
141#define rtems_rtl_ptr_type_get(_h, _t) ((_t*) rtems_rtl_ptr_get (_h))
142
143/**
144 * Get the pointer given an indirect handle.
145 *
146 * @param handle The handle the pointer is returned from.
147 * @return void* The pointer held in the handle.
148 */
149static inline void* rtems_rtl_sptr_get (rtems_rtl_sptr_t* handle)
150{
151  return rtems_rtl_ptr_get (&handle->ptr);
152}
153
154/**
155 * Set the pointer given an indirect handle and the pointer.
156 *
157 * @param handle The handle the pointer is returned from.
158 * @param pointer The pointer to set in the handle.
159 */
160static inline void rtems_rtl_sptr_set (rtems_rtl_sptr_t* handle, void* pointer)
161{
162  rtems_rtl_ptr_set (&handle->ptr, pointer);
163}
164
165/**
166 * Initialise the indirect handle.
167 *
168 * @param handle The handle to initialise.
169 */
170static inline void rtems_rtl_sptr_init (rtems_rtl_sptr_t* handle)
171{
172  rtems_rtl_ptr_init (&handle->ptr);
173  handle->size = 0;
174}
175
176/**
177 * Is the indirect handle NULL ?
178 *
179 * @param handle The handle to test.
180 * @return bool True if the pointer is NULL.
181 */
182static inline bool rtems_rtl_sptr_null (rtems_rtl_sptr_t* handle)
183{
184  return rtems_rtl_ptr_null (&handle->ptr);
185}
186
187/**
188 * Move the allocated pointer from one handle to another. The source handle is
189 * cleared and removed from the list of handles.
190 *
191 * @param src The source handle to move the pointer from.
192 * @param dst The destination handle to receive the pointer.
193 */
194static inline void rtems_rtl_sptr_move (rtems_rtl_sptr_t* dst, rtems_rtl_sptr_t* src)
195{
196  rtems_rtl_ptr_move (&dst->ptr, &src->ptr);
197  dst->size = src->size;
198  src->size = 0;
199}
200
201/**
202 * Get the size.
203 *
204 * @param handle The handle to get the size from.
205 * @return size_t The size_t.
206 */
207static inline size_t rtems_rtl_sptr_get_size (rtems_rtl_sptr_t* handle)
208{
209  return handle->size;
210}
211
212/**
213 * Set the size.
214 *
215 * @param handle The handle to set the size.
216 * @param size The size to set..
217 */
218static inline void rtems_rtl_sptr_set_size (rtems_rtl_sptr_t* handle, size_t size)
219{
220  handle->size = size;
221}
222
223/**
224 * Return the pointer as the type provided.
225 *
226 * @param _h The handle.
227 * @param _t The type.
228 */
229#define rtems_rtl_sptr_type_get(_h, _t) ((_t*) rtems_rtl_sptr_get (_h))
230
231#ifdef __cplusplus
232}
233#endif /* __cplusplus */
234
235#endif
Note: See TracBrowser for help on using the repository browser.