source: rtems/cpukit/score/src/heapgreedy.c @ 62181b21

4.115
Last change on this file since 62181b21 was 317ee8d, checked in by Sebastian Huber <sebastian.huber@…>, on 07/17/12 at 08:19:16

score: Change greedy allocation API

  • Property mode set to 100644
File size: 1.7 KB
Line 
1/*
2 * Copyright (c) 2012 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Obere Lagerstr. 30
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.com/license/LICENSE.
13 */
14
15#if HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#include <rtems/score/heap.h>
20
21Heap_Block *_Heap_Greedy_allocate(
22  Heap_Control *heap,
23  const uintptr_t *block_sizes,
24  size_t block_count
25)
26{
27  Heap_Block *const free_list_tail = _Heap_Free_list_tail( heap );
28  Heap_Block *allocated_blocks = NULL;
29  Heap_Block *blocks = NULL;
30  Heap_Block *current;
31  size_t i;
32
33  for (i = 0; i < block_count; ++i) {
34    void *next = _Heap_Allocate( heap, block_sizes [i] );
35
36    if ( next != NULL ) {
37      Heap_Block *next_block = _Heap_Block_of_alloc_area(
38        (uintptr_t) next,
39        heap->page_size
40      );
41
42      next_block->next = allocated_blocks;
43      allocated_blocks = next_block;
44    }
45  }
46
47  while ( (current = _Heap_Free_list_first( heap )) != free_list_tail ) {
48    _Heap_Block_allocate(
49      heap,
50      current,
51      _Heap_Alloc_area_of_block( current ),
52      _Heap_Block_size( current ) - HEAP_BLOCK_HEADER_SIZE
53    );
54
55    current->next = blocks;
56    blocks = current;
57  }
58
59  while ( allocated_blocks != NULL ) {
60    current = allocated_blocks;
61    allocated_blocks = allocated_blocks->next;
62    _Heap_Free( heap, (void *) _Heap_Alloc_area_of_block( current ) );
63  }
64
65  return blocks;
66}
67
68void _Heap_Greedy_free(
69  Heap_Control *heap,
70  Heap_Block *blocks
71)
72{
73  while ( blocks != NULL ) {
74    Heap_Block *current = blocks;
75
76    blocks = blocks->next;
77    _Heap_Free( heap, (void *) _Heap_Alloc_area_of_block( current ) );
78  }
79}
Note: See TracBrowser for help on using the repository browser.