source: rtems/cpukit/score/src/heapresizeblock.c @ 8a8b95aa

5
Last change on this file since 8a8b95aa was 4c20da4b, checked in by Sebastian Huber <sebastian.huber@…>, on 04/04/19 at 07:18:11

doxygen: Rename Score* groups in RTEMSScore*

Update #3706

  • Property mode set to 100644
File size: 2.8 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup RTEMSScoreHeap
5 *
6 * @brief Heap Handler implementation.
7 */
8
9/*
10 *  COPYRIGHT (c) 1989-1999.
11 *  On-Line Applications Research Corporation (OAR).
12 *
13 *  Copyright (c) 2009 embedded brains GmbH.
14 *
15 *  The license and distribution terms for this file may be
16 *  found in the file LICENSE in this distribution or at
17 *  http://www.rtems.org/license/LICENSE.
18 */
19
20#if HAVE_CONFIG_H
21#include "config.h"
22#endif
23
24#include <rtems/score/heapimpl.h>
25
26static Heap_Resize_status _Heap_Resize_block_checked(
27  Heap_Control *heap,
28  Heap_Block *block,
29  uintptr_t alloc_begin,
30  uintptr_t new_alloc_size,
31  uintptr_t *old_size,
32  uintptr_t *new_size
33)
34{
35  Heap_Statistics *const stats = &heap->stats;
36
37  uintptr_t const block_begin = (uintptr_t) block;
38  uintptr_t block_size = _Heap_Block_size( block );
39  uintptr_t block_end = block_begin + block_size;
40
41  uintptr_t alloc_size = block_end - alloc_begin + HEAP_ALLOC_BONUS;
42
43  Heap_Block *next_block = _Heap_Block_at( block, block_size );
44  uintptr_t next_block_size = _Heap_Block_size( next_block );
45  bool next_block_is_free = _Heap_Is_free( next_block );
46
47  _HAssert( _Heap_Is_block_in_heap( heap, next_block ) );
48  _HAssert( _Heap_Is_prev_used( next_block ) );
49
50  *old_size = alloc_size;
51
52  if ( next_block_is_free ) {
53    block_size += next_block_size;
54    alloc_size += next_block_size;
55  }
56
57  if ( new_alloc_size > alloc_size ) {
58    return HEAP_RESIZE_UNSATISFIED;
59  }
60
61  if ( next_block_is_free ) {
62    _Heap_Block_set_size( block, block_size );
63
64    _Heap_Free_list_remove( next_block );
65
66    next_block = _Heap_Block_at( block, block_size );
67    next_block->size_and_flag |= HEAP_PREV_BLOCK_USED;
68
69    /* Statistics */
70    --stats->free_blocks;
71    stats->free_size -= next_block_size;
72  }
73
74  block = _Heap_Block_allocate( heap, block, alloc_begin, new_alloc_size );
75
76  block_size = _Heap_Block_size( block );
77  next_block = _Heap_Block_at( block, block_size );
78  *new_size = (uintptr_t) next_block - alloc_begin + HEAP_ALLOC_BONUS;
79
80  /* Statistics */
81  ++stats->resizes;
82
83  return HEAP_RESIZE_SUCCESSFUL;
84}
85
86Heap_Resize_status _Heap_Resize_block(
87  Heap_Control *heap,
88  void *alloc_begin_ptr,
89  uintptr_t new_alloc_size,
90  uintptr_t *old_size,
91  uintptr_t *new_size
92)
93{
94  uintptr_t const page_size = heap->page_size;
95
96  uintptr_t const alloc_begin = (uintptr_t) alloc_begin_ptr;
97
98  Heap_Block *const block = _Heap_Block_of_alloc_area( alloc_begin, page_size );
99
100  *old_size = 0;
101  *new_size = 0;
102
103  if ( _Heap_Is_block_in_heap( heap, block ) ) {
104    _Heap_Protection_block_check( heap, block );
105
106    /* TODO: Free only the next block if necessary */
107    _Heap_Protection_free_all_delayed_blocks( heap );
108
109    return _Heap_Resize_block_checked(
110      heap,
111      block,
112      alloc_begin,
113      new_alloc_size,
114      old_size,
115      new_size
116    );
117  }
118  return HEAP_RESIZE_FATAL_ERROR;
119}
Note: See TracBrowser for help on using the repository browser.