summaryrefslogtreecommitdiffstats
path: root/cpukit/score/macros/rtems/score/heap.inl
blob: a7a7a11a44291a0d8b48200f9c9fe89fea971d68 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*  heap.inl
 *
 *  This file contains the macro implementation of the inlined
 *  routines from the heap handler.
 *
 *  COPYRIGHT (c) 1989-1999.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  $Id$
 */

#ifndef _RTEMS_SCORE_HEAP_INL
#define _RTEMS_SCORE_HEAP_INL

/*
 * WARNING: this file is only visually checked against
 * '../../../inline/rtems/score/heap.inl'. Use those file for reference
 * if encounter problems.
 */

#include <rtems/score/address.h>

/*PAGE
 *
 *  _Heap_Head
 */

#define _Heap_Head( _the_heap ) (&(_the_heap)->free_list)

/*PAGE
 *
 *  _Heap_Tail
 */

#define _Heap_Tail( _the_heap ) (&(_the_heap)->free_list)

/*PAGE
 *
 *  _Heap_First
 */

#define _Heap_First( _the_heap ) (_Heap_Head(_the_heap)->next)

/*PAGE
 *
 *  _Heap_Last
 */

#define _Heap_Last( _the_heap )  (_Heap_Tail(_the_heap)->prev)

/*PAGE
 *
 *  _Heap_Block_remove
 *
 */

#define _Heap_Block_remove( _the_block ) \
  do { \
    Heap_Block *block = (_the_block); \
    Heap_Block *next = block->next; \
    Heap_Block *prev = block->prev; \
    prev->next = next; \
    next->prev = prev; \
  } while(0)

/*PAGE
 *
 *  _Heap_Block_replace
 *
 */

#define _Heap_Block_replace( _old_block,  _new_block ) \
  do { \
    Heap_Block *block = (_old_block); \
    Heap_Block *next = block->next; \
    Heap_Block *prev = block->prev; \
    block = (_new_block); \
    block->next = next; \
    block->prev = prev; \
    next->prev = prev->next = block; \
  } while(0)

/*PAGE
 *
 *  _Heap_Block_insert_after
 *
 */

#define _Heap_Block_insert_after(  _prev_block, _the_block ) \
  do { \
    Heap_Block *prev = (_prev_block); \
    Heap_Block *block = (_the_block); \
    Heap_Block *next = prev->next; \
    block->next  = next; \
    block->prev  = prev; \
    next->prev = prev->next = block; \
  } while(0)

/*PAGE
 *
 *  _Heap_Is_aligned
 */

#define _Heap_Is_aligned( _value, _alignment ) \
  (((_value) % (_alignment)) == 0)

/*PAGE
 *
 *  _Heap_Align_up
 */

#define _Heap_Align_up( _value, _alignment ) \
  do { \
    unsigned32 v = *(_value); \
    unsigned32 a = (_alignment); \
    unsigned32 r = v % a; \
    *(_value) = r ? v - r + a : v; \
  } while(0)

/*PAGE
 *
 *  _Heap_Align_down
 */

#define _Heap_Align_down( _value, _alignment ) \
  do { \
    unsigned32 v = *(_value); \
    *(_value) = v - (v % (_alignment)); \
  } while(0)

/*PAGE
 *
 *  _Heap_Is_aligned_ptr
 */

#define _Heap_Is_aligned_ptr( _ptr, _alignment ) \
  ((_H_p2u(_ptr) % (_alignment)) == 0)

/*PAGE
 *
 *  _Heap_Align_up_uptr
 */

#define _Heap_Align_up_uptr( _value, _alignment ) \
  do { \
    _H_uptr_t v = *(_value); \
    unsigned32 a = (_alignment); \
    _H_uptr_t r = v % a; \
    *(_value) = r ? v - r + a : v; \
  } while(0)

/*PAGE
 *
 *  _Heap_Align_down_uptr
 */

#define _Heap_Align_down_uptr( _value, _alignment ) \
  do { \
    _H_uptr_t v = *(_value); \
    *(_value) = v - (v % (_alignment)); \
  } while(0)

/*PAGE
 *
 *  _Heap_Block_at
 */

#define _Heap_Block_at( _base, _offset ) \
  ( (Heap_Block *) _Addresses_Add_offset( (_base), (_offset) ) )

/*PAGE
 *
 *  _Heap_User_area
 */

#define _Heap_User_area( _the_block ) \
  ((void *) _Addresses_Add_offset( (_the_block), HEAP_BLOCK_USER_OFFSET ))

/*PAGE
 *
 *  _Heap_Start_of_block
 */
 
#define _Heap_Start_of_block( _the_heap, _base, _the_block_ptr ) \
  do { \
    _H_uptr_t addr = _H_p2u(_base); \
    _Heap_Align_down( &addr, (_the_heap)->page_size ); \
    *(_the_block_ptr) = (Heap_Block *)(addr - HEAP_BLOCK_USER_OFFSET); \
  } while(0)

/*PAGE
 *
 *  _Heap_Is_prev_used
 */

#define _Heap_Is_prev_used( _the_block ) \
  ((_the_block)->size & HEAP_PREV_USED)

/*PAGE
 *
 *  _Heap_Block_size
 */

#define _Heap_Block_size( _the_block )    \
  ((_the_block)->size & ~HEAP_PREV_USED)

/*PAGE
 *
 *  _Heap_Is_block_in
 */

#define _Heap_Is_block_in( _the_heap, _the_block ) \
  ( _Addresses_Is_in_range( (_the_block), \
    (_the_heap)->start, (_the_heap)->final ) )

#endif
/* end of include file */