1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup RTEMSScoreFreechain |
---|
7 | * |
---|
8 | * @brief This header file provides interfaces of the |
---|
9 | * @ref RTEMSScoreBarrier which are only used by the implementation. |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
13 | * Copyright (c) 2013 Gedare Bloom. |
---|
14 | * |
---|
15 | * Redistribution and use in source and binary forms, with or without |
---|
16 | * modification, are permitted provided that the following conditions |
---|
17 | * are met: |
---|
18 | * 1. Redistributions of source code must retain the above copyright |
---|
19 | * notice, this list of conditions and the following disclaimer. |
---|
20 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
21 | * notice, this list of conditions and the following disclaimer in the |
---|
22 | * documentation and/or other materials provided with the distribution. |
---|
23 | * |
---|
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
34 | * POSSIBILITY OF SUCH DAMAGE. |
---|
35 | */ |
---|
36 | |
---|
37 | #ifndef _RTEMS_SCORE_FREECHAINIMPL_H |
---|
38 | #define _RTEMS_SCORE_FREECHAINIMPL_H |
---|
39 | |
---|
40 | #include <rtems/score/freechain.h> |
---|
41 | #include <rtems/score/basedefs.h> |
---|
42 | #include <rtems/score/chainimpl.h> |
---|
43 | |
---|
44 | #ifdef __cplusplus |
---|
45 | extern "C" { |
---|
46 | #endif |
---|
47 | |
---|
48 | /** |
---|
49 | * @addtogroup RTEMSScoreFreechain |
---|
50 | * |
---|
51 | * @{ |
---|
52 | */ |
---|
53 | |
---|
54 | /** |
---|
55 | * @brief Allocator function. |
---|
56 | */ |
---|
57 | typedef void *( *Freechain_Allocator )( size_t size ); |
---|
58 | |
---|
59 | /** |
---|
60 | * @brief Initializes a freechain. |
---|
61 | * |
---|
62 | * This routine initializes the freechain control structure to manage a chain |
---|
63 | * of nodes. In the case the freechain is empty the extend handler is called to |
---|
64 | * get more nodes. |
---|
65 | * |
---|
66 | * @param[out] freechain The freechain control to initialize. |
---|
67 | * @param[out] initial_nodes Array with the initial nodes. |
---|
68 | * @param number_nodes The initial number of nodes. |
---|
69 | * @param node_size The node size. |
---|
70 | */ |
---|
71 | RTEMS_INLINE_ROUTINE void _Freechain_Initialize( |
---|
72 | Freechain_Control *freechain, |
---|
73 | void *initial_nodes, |
---|
74 | size_t number_nodes, |
---|
75 | size_t node_size |
---|
76 | ) |
---|
77 | { |
---|
78 | _Chain_Initialize( |
---|
79 | &freechain->Free, |
---|
80 | initial_nodes, |
---|
81 | number_nodes, |
---|
82 | node_size |
---|
83 | ); |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * @brief Return true if the freechain is empty, otherwise false |
---|
88 | * |
---|
89 | * @param freechain The freechain control. |
---|
90 | */ |
---|
91 | RTEMS_INLINE_ROUTINE bool _Freechain_Is_empty( |
---|
92 | const Freechain_Control *freechain |
---|
93 | ) |
---|
94 | { |
---|
95 | return _Chain_Is_empty( &freechain->Free ); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * @brief Pops a node from the freechain. |
---|
100 | * |
---|
101 | * The freechain shall not be empty. |
---|
102 | * |
---|
103 | * @param freechain The freechain control. |
---|
104 | */ |
---|
105 | RTEMS_INLINE_ROUTINE void *_Freechain_Pop( Freechain_Control *freechain ) |
---|
106 | { |
---|
107 | return _Chain_Get_first_unprotected( &freechain->Free ); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * @brief Pushes a node back to the freechain. |
---|
112 | * |
---|
113 | * @param freechain The freechain control. |
---|
114 | * @param node The node to push back. The node shall not be @c NULL. |
---|
115 | */ |
---|
116 | void RTEMS_INLINE_ROUTINE _Freechain_Push( |
---|
117 | Freechain_Control *freechain, |
---|
118 | void *node |
---|
119 | ) |
---|
120 | { |
---|
121 | _Chain_Initialize_node( node ); |
---|
122 | _Chain_Prepend_unprotected( &freechain->Free, node ); |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * @brief Extend the freechain with new nodes. |
---|
127 | * |
---|
128 | * @param freechain The freechain control. |
---|
129 | * @param allocator The allocator function. |
---|
130 | * @param number_nodes_to_extend The number of nodes to extend. |
---|
131 | * @param node_size The node size. |
---|
132 | * |
---|
133 | * @retval NULL The extend operation failed. |
---|
134 | * @retval nodes Pointer to the new nodes. |
---|
135 | */ |
---|
136 | void *_Freechain_Extend( |
---|
137 | Freechain_Control *freechain, |
---|
138 | Freechain_Allocator allocator, |
---|
139 | size_t number_nodes_to_extend, |
---|
140 | size_t node_size |
---|
141 | ); |
---|
142 | |
---|
143 | /** |
---|
144 | * @brief Gets a node from the freechain. |
---|
145 | * |
---|
146 | * @param[in, out] freechain The freechain control. |
---|
147 | * @param allocator The allocator function. |
---|
148 | * @param number_nodes_to_extend The number of nodes in the case an extend is |
---|
149 | * necessary due to an empty freechain. |
---|
150 | * @param[in] node_size The node size. |
---|
151 | * |
---|
152 | * @retval NULL The freechain is empty and the extend operation failed. |
---|
153 | * @retval pointer Pointer to a node. The node ownership passes to the |
---|
154 | * caller. |
---|
155 | */ |
---|
156 | void *_Freechain_Get( |
---|
157 | Freechain_Control *freechain, |
---|
158 | Freechain_Allocator allocator, |
---|
159 | size_t number_nodes_to_extend, |
---|
160 | size_t node_size |
---|
161 | ); |
---|
162 | |
---|
163 | /** |
---|
164 | * @brief Puts a node back onto the freechain. |
---|
165 | * |
---|
166 | * @param[in, out] freechain The freechain control. |
---|
167 | * @param[out] node The node to put back. The node may be @c NULL, in this case |
---|
168 | * the function does nothing. |
---|
169 | */ |
---|
170 | void _Freechain_Put( |
---|
171 | Freechain_Control *freechain, |
---|
172 | void *node |
---|
173 | ); |
---|
174 | |
---|
175 | /** @} */ |
---|
176 | |
---|
177 | #ifdef __cplusplus |
---|
178 | } |
---|
179 | #endif |
---|
180 | |
---|
181 | #endif |
---|
182 | /* end of include file */ |
---|