source: rtems/cpukit/score/src/heapgreedy.c @ fce900b5

5
Last change on this file since fce900b5 was d61c315, checked in by Sebastian Huber <sebastian.huber@…>, on 04/07/14 at 14:00:00

score: Fix for empty heap

  • Property mode set to 100644
File size: 2.3 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup ScoreHeap
5 *
6 * @brief Heap Handler API.
7 */
8
9/*
10 * Copyright (c) 2012 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Obere Lagerstr. 30
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * The license and distribution terms for this file may be
19 * found in the file LICENSE in this distribution or at
20 * http://www.rtems.org/license/LICENSE.
21 */
22
23#if HAVE_CONFIG_H
24  #include "config.h"
25#endif
26
27#include <rtems/score/heapimpl.h>
28
29Heap_Block *_Heap_Greedy_allocate(
30  Heap_Control *heap,
31  const uintptr_t *block_sizes,
32  size_t block_count
33)
34{
35  Heap_Block *const free_list_tail = _Heap_Free_list_tail( heap );
36  Heap_Block *allocated_blocks = NULL;
37  Heap_Block *blocks = NULL;
38  Heap_Block *current;
39  size_t i;
40
41  _Heap_Protection_free_all_delayed_blocks( heap );
42
43  for (i = 0; i < block_count; ++i) {
44    void *next = _Heap_Allocate( heap, block_sizes [i] );
45
46    if ( next != NULL ) {
47      Heap_Block *next_block = _Heap_Block_of_alloc_area(
48        (uintptr_t) next,
49        heap->page_size
50      );
51
52      next_block->next = allocated_blocks;
53      allocated_blocks = next_block;
54    }
55  }
56
57  while ( (current = _Heap_Free_list_first( heap )) != free_list_tail ) {
58    _Heap_Block_allocate(
59      heap,
60      current,
61      _Heap_Alloc_area_of_block( current ),
62      _Heap_Block_size( current ) - HEAP_BLOCK_HEADER_SIZE
63    );
64
65    current->next = blocks;
66    blocks = current;
67  }
68
69  while ( allocated_blocks != NULL ) {
70    current = allocated_blocks;
71    allocated_blocks = allocated_blocks->next;
72    _Heap_Free( heap, (void *) _Heap_Alloc_area_of_block( current ) );
73  }
74
75  return blocks;
76}
77
78Heap_Block *_Heap_Greedy_allocate_all_except_largest(
79  Heap_Control *heap,
80  uintptr_t *allocatable_size
81)
82{
83  Heap_Information info;
84
85  _Heap_Get_free_information( heap, &info );
86
87  if ( info.largest > 0 ) {
88    *allocatable_size = info.largest - HEAP_BLOCK_HEADER_SIZE + HEAP_ALLOC_BONUS;
89  } else {
90    *allocatable_size = 0;
91  }
92
93  return _Heap_Greedy_allocate( heap, allocatable_size, 1 );
94}
95
96void _Heap_Greedy_free(
97  Heap_Control *heap,
98  Heap_Block *blocks
99)
100{
101  while ( blocks != NULL ) {
102    Heap_Block *current = blocks;
103
104    blocks = blocks->next;
105    _Heap_Free( heap, (void *) _Heap_Alloc_area_of_block( current ) );
106  }
107}
Note: See TracBrowser for help on using the repository browser.