source: rtems/cpukit/score/src/wkspace.c

Last change on this file was 1e62e15f, checked in by Sebastian Huber <sebastian.huber@…>, on Mar 27, 2021 at 5:08:13 PM

score: Add an assert to _Workspace_Free()

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup RTEMSScoreWorkspace
5 *
6 * @brief This source file contains the definition of ::_Workspace_Area, the
7 *   implementation of _Workspace_Handler_initialization(),
8 *   _Workspace_Allocate(), and _Workspace_Free(), and the Workspace Handler
9 *   system initialization.
10 */
11
12/*
13 *  COPYRIGHT (c) 1989-2009.
14 *  On-Line Applications Research Corporation (OAR).
15 *
16 *  The license and distribution terms for this file may be
17 *  found in the file LICENSE in this distribution or at
18 *  http://www.rtems.org/license/LICENSE.
19 */
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include <rtems/score/wkspace.h>
26#include <rtems/score/assert.h>
27#include <rtems/score/heapimpl.h>
28#include <rtems/score/interr.h>
29#include <rtems/config.h>
30#include <rtems/sysinit.h>
31
32Heap_Control _Workspace_Area;
33
34static void _Workspace_Initialize( void )
35{
36  _Workspace_Handler_initialization( _Memory_Get(), _Heap_Extend );
37}
38
39RTEMS_SYSINIT_ITEM(
40  _Workspace_Initialize,
41  RTEMS_SYSINIT_WORKSPACE,
42  RTEMS_SYSINIT_ORDER_MIDDLE
43);
44
45void _Workspace_Handler_initialization(
46  const Memory_Information              *mem,
47  Heap_Initialization_or_extend_handler  extend
48)
49{
50  Heap_Initialization_or_extend_handler init_or_extend;
51  uintptr_t                             remaining;
52  bool                                  unified;
53  uintptr_t                             page_size;
54  uintptr_t                             overhead;
55  size_t                                i;
56
57  page_size = CPU_HEAP_ALIGNMENT;
58  remaining = rtems_configuration_get_work_space_size();
59  init_or_extend = _Heap_Initialize;
60  unified = rtems_configuration_get_unified_work_area();
61  overhead = _Heap_Area_overhead( page_size );
62
63  for ( i = 0; i < _Memory_Get_count( mem ); ++i ) {
64    Memory_Area *area;
65    uintptr_t    free_size;
66
67    area = _Memory_Get_area( mem, i );
68    free_size = _Memory_Get_free_size( area );
69
70    if ( free_size > overhead ) {
71      uintptr_t space_available;
72      uintptr_t size;
73
74      if ( unified ) {
75        size = free_size;
76      } else {
77        if ( remaining > 0 ) {
78          size = remaining < free_size - overhead ?
79            remaining + overhead : free_size;
80        } else {
81          size = 0;
82        }
83      }
84
85      space_available = ( *init_or_extend )(
86        &_Workspace_Area,
87        _Memory_Get_free_begin( area ),
88        size,
89        page_size
90      );
91
92      _Memory_Consume( area, size );
93
94      if ( space_available < remaining ) {
95        remaining -= space_available;
96      } else {
97        remaining = 0;
98      }
99
100      init_or_extend = extend;
101    }
102  }
103
104  if ( remaining > 0 ) {
105    _Internal_error( INTERNAL_ERROR_TOO_LITTLE_WORKSPACE );
106  }
107
108  _Heap_Protection_set_delayed_free_fraction( &_Workspace_Area, 1 );
109}
110
111void *_Workspace_Allocate( size_t size )
112{
113  return _Heap_Allocate( &_Workspace_Area, size );
114}
115
116void _Workspace_Free( void *block )
117{
118  bool ok;
119
120  ok = _Heap_Free( &_Workspace_Area, block );
121  _Assert( ok );
122  (void) ok;
123}
Note: See TracBrowser for help on using the repository browser.