summaryrefslogtreecommitdiffstats
path: root/cpukit/score/src/heap.c
blob: e6c9cd920c20f33f1956fe33034f723f7cb97ba0 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 *  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$
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems/system.h>
#include <rtems/score/sysstate.h>
#include <rtems/score/heap.h>

static uint32_t instance = 0;

/*PAGE
 *
 *  _Heap_Initialize
 *
 *  This kernel routine initializes a heap.
 *
 *  Input parameters:
 *    the_heap         - pointer to heap header
 *    starting_address - starting address of heap
 *    size             - size of heap
 *    page_size        - allocatable unit of memory
 *
 *  Output parameters:
 *    returns - maximum memory available if RTEMS_SUCCESSFUL
 *    0       - otherwise
 *
 *  This is what a heap looks like in memory immediately after initialization:
 *
 *
 *            +--------------------------------+ <- begin = starting_address
 *            |  unused space due to alignment |
 *            |       size < page_size         |
 *         0  +--------------------------------+ <- first block
 *            |  prev_size = page_size         |
 *         4  +--------------------------------+
 *            |  size = size0              | 1 |
 *         8  +---------------------+----------+ <- aligned on page_size
 *            |  next = HEAP_TAIL   |          |
 *        12  +---------------------+          |
 *            |  prev = HEAP_HEAD   |  memory  |
 *            +---------------------+          |
 *            |                     available  |
 *            |                                |
 *            |                for allocation  |
 *            |                                |
 *     size0  +--------------------------------+ <- last dummy block
 *            |  prev_size = size0             |
 *        +4  +--------------------------------+
 *            |  size = page_size          | 0 | <- prev block is free
 *        +8  +--------------------------------+ <- aligned on page_size
 *            |  unused space due to alignment |
 *            |       size < page_size         |
 *            +--------------------------------+ <- end = begin + size
 *
 *  This is what a heap looks like after first allocation of SIZE bytes.
 *  BSIZE stands for SIZE + 4 aligned up on 'page_size' boundary if allocation
 *  has been performed by _Heap_Allocate(). If allocation has been performed
 *  by _Heap_Allocate_aligned(), the block size BSIZE is defined differently
 *  (see 'heapallocatealigned.c' for details).
 *
 *            +--------------------------------+ <- begin = starting_address
 *            |  unused space due to alignment |
 *            |       size < page_size         |
 *         0  +--------------------------------+ <- used block
 *            |  prev_size = page_size         |
 *         4  +--------------------------------+
 *            |  size = BSIZE              | 1 | <- prev block is used
 *         8  +--------------------------------+ <- aligned on page_size
 *            |              .                 | Pointer returned to the user
 *            |              .                 | is 8 for _Heap_Allocate()
 *            |              .                 | and is in range
 * 8 +        |         user-accessible        | [8,8+page_size) for
 *  page_size +- - -                      - - -+ _Heap_Allocate_aligned()
 *            |             area               |
 *            |              .                 |
 *     BSIZE  +- - - - -     .        - - - - -+ <- free block
 *            |              .                 |
 * BSIZE  +4  +--------------------------------+
 *            |  size = S = size0 - BSIZE  | 1 | <- prev block is used
 * BSIZE  +8  +-------------------+------------+ <- aligned on page_size
 *            |  next = HEAP_TAIL |            |
 * BSIZE +12  +-------------------+            |
 *            |  prev = HEAP_HEAD |     memory |
 *            +-------------------+            |
 *            |                   .  available |
 *            |                   .            |
 *            |                   .        for |
 *            |                   .            |
 * BSIZE +S+0 +-------------------+ allocation + <- last dummy block
 *            |  prev_size = S    |            |
 *       +S+4 +-------------------+------------+
 *            |  size = page_size          | 0 | <- prev block is free
 *       +S+8 +--------------------------------+ <- aligned on page_size
 *            |  unused space due to alignment |
 *            |       size < page_size         |
 *            +--------------------------------+ <- end = begin + size
 *
 */

uint32_t   _Heap_Initialize(
  Heap_Control        *the_heap,
  void                *starting_address,
  uint32_t             size,
  uint32_t             page_size
)
{
  Heap_Block *the_block;
  uint32_t  the_size;
  _H_uptr_t   start;
  _H_uptr_t   aligned_start;
  uint32_t  overhead;
  Heap_Statistics *const stats = &the_heap->stats;

  if(page_size == 0)
    page_size = CPU_ALIGNMENT;
  else
    _Heap_Align_up( &page_size, CPU_ALIGNMENT );

  /* Calculate aligned_start so that aligned_start + HEAP_BLOCK_USER_OFFSET
     (value of user pointer) is aligned on 'page_size' boundary. Make sure
     resulting 'aligned_start' is not below 'starting_address'. */
  start = _H_p2u(starting_address);
  aligned_start = start + HEAP_BLOCK_USER_OFFSET;
  _Heap_Align_up_uptr ( &aligned_start, page_size );
  aligned_start -= HEAP_BLOCK_USER_OFFSET;

  /* Calculate 'min_block_size'. It's HEAP_MIN_BLOCK_SIZE aligned up to the
     nearest multiple of 'page_size'. */
  the_heap->min_block_size = HEAP_MIN_BLOCK_SIZE;
  _Heap_Align_up ( &the_heap->min_block_size, page_size );

  /* Calculate 'the_size' -- size of the first block so that there is enough
     space at the end for the permanent last block. It is equal to 'size'
     minus total overhead aligned down to the nearest multiple of
     'page_size'. */
  overhead = HEAP_OVERHEAD + (aligned_start - start);
  if ( size < overhead )
    return 0;   /* Too small area for the heap */
  the_size = size - overhead;
  _Heap_Align_down ( &the_size, page_size );
  if ( the_size == 0 )
    return 0;   /* Too small area for the heap */

  the_heap->page_size = page_size;
  the_heap->begin = starting_address;
  the_heap->end = starting_address + size;

  the_block = (Heap_Block *) aligned_start;

  the_block->prev_size = page_size;
  the_block->size = the_size | HEAP_PREV_USED;
  the_block->next = _Heap_Tail( the_heap );
  the_block->prev = _Heap_Head( the_heap );
  _Heap_Head(the_heap)->next = the_block;
  _Heap_Tail(the_heap)->prev = the_block;
  the_heap->start = the_block;

  _HAssert(_Heap_Is_aligned(the_heap->page_size, CPU_ALIGNMENT));
  _HAssert(_Heap_Is_aligned(the_heap->min_block_size, page_size));
  _HAssert(_Heap_Is_aligned_ptr(_Heap_User_area(the_block), page_size));

  the_block = _Heap_Block_at( the_block, the_size );
  the_heap->final = the_block;       /* Permanent final block of the heap */
  the_block->prev_size = the_size;   /* Previous block is free */
  the_block->size = page_size;

  stats->size = size;
  stats->free_size = the_size;
  stats->min_free_size = the_size;
  stats->free_blocks = 1;
  stats->max_free_blocks = 1;
  stats->used_blocks = 0;
  stats->max_search = 0;
  stats->allocs = 0;
  stats->searches = 0;
  stats->frees = 0;
  stats->resizes = 0;
  stats->instance = instance++;

  return ( the_size - HEAP_BLOCK_USED_OVERHEAD );
}

/*PAGE
 *
 * Internal routines shared by _Heap_Allocate() and _Heap_Allocate_aligned().
 *
 * Note: there is no reason to put them into a separate file(s) as they are
 * always required for heap to be usefull.
 *
 */

/*
 * Convert user requested 'size' of memory block to the block size.
 * Return block size on success, 0 if overflow occured
 */
uint32_t _Heap_Calc_block_size(
  uint32_t size,
  uint32_t page_size,
  uint32_t min_size)
{
  uint32_t block_size = size + HEAP_BLOCK_USED_OVERHEAD;
  _Heap_Align_up(&block_size, page_size);
  if(block_size < min_size) block_size = min_size;
  /* 'block_size' becomes <= 'size' if and only if overflow occured. */
  return (block_size > size) ? block_size : 0;
}

/*
 * Allocate block of size 'alloc_size' from 'the_block' belonging to
 * 'the_heap'. Split 'the_block' if possible, otherwise allocate it entirely.
 * When split, make the lower part used, and leave the upper part free.
 * Return the size of allocated block.
 */
unsigned32 _Heap_Block_allocate(
  Heap_Control* the_heap,
  Heap_Block* the_block,
  uint32_t alloc_size)
{
  Heap_Statistics *const stats = &the_heap->stats;
  uint32_t const block_size = _Heap_Block_size(the_block);
  uint32_t const the_rest = block_size - alloc_size;

  _HAssert(_Heap_Is_aligned(block_size, the_heap->page_size));
  _HAssert(_Heap_Is_aligned(alloc_size, the_heap->page_size));
  _HAssert(alloc_size <= block_size);
  _HAssert(_Heap_Is_prev_used(the_block));

  if(the_rest >= the_heap->min_block_size) {
    /* Split the block so that upper part is still free, and lower part
       becomes used. This is slightly less optimal than leaving lower part
       free as it requires replacing block in the free blocks list, but it
       makes it possible to reuse this code in the _Heap_Resize_block(). */
    Heap_Block *next_block = _Heap_Block_at(the_block, alloc_size);
    _Heap_Block_replace(the_block, next_block);
    the_block->size = alloc_size | HEAP_PREV_USED;
    next_block->size = the_rest | HEAP_PREV_USED;
    _Heap_Block_at(next_block, the_rest)->prev_size = the_rest;
  }
  else {
    /* Don't split the block as remainder is either zero or too small to be
       used as a separate free block. Change 'alloc_size' to the size of the
       block and remove the block from the list of free blocks. */
    _Heap_Block_remove(the_block);
    alloc_size = block_size;
    _Heap_Block_at(the_block, alloc_size)->size |= HEAP_PREV_USED;
    stats->free_blocks -= 1;
  }
  /* Update statistics */
  stats->free_size -= alloc_size;
  if(stats->min_free_size > stats->free_size)
    stats->min_free_size = stats->free_size;
  stats->used_blocks += 1;
  return alloc_size;
}