1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Workspace Handler Support |
---|
5 | * @ingroup RTEMSScoreWorkspace |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2009. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/score/wkspace.h> |
---|
22 | #include <rtems/score/assert.h> |
---|
23 | #include <rtems/score/heapimpl.h> |
---|
24 | #include <rtems/score/interr.h> |
---|
25 | #include <rtems/score/threadimpl.h> |
---|
26 | #include <rtems/posix/pthread.h> |
---|
27 | #include <rtems/config.h> |
---|
28 | #include <rtems/sysinit.h> |
---|
29 | |
---|
30 | #include <string.h> |
---|
31 | |
---|
32 | /* #define DEBUG_WORKSPACE */ |
---|
33 | #if defined(DEBUG_WORKSPACE) |
---|
34 | #include <rtems/bspIo.h> |
---|
35 | #endif |
---|
36 | |
---|
37 | Heap_Control _Workspace_Area; |
---|
38 | |
---|
39 | static void _Workspace_Initialize( void ) |
---|
40 | { |
---|
41 | _Workspace_Handler_initialization( _Memory_Get(), _Heap_Extend ); |
---|
42 | } |
---|
43 | |
---|
44 | RTEMS_SYSINIT_ITEM( |
---|
45 | _Workspace_Initialize, |
---|
46 | RTEMS_SYSINIT_WORKSPACE, |
---|
47 | RTEMS_SYSINIT_ORDER_MIDDLE |
---|
48 | ); |
---|
49 | |
---|
50 | void _Workspace_Handler_initialization( |
---|
51 | const Memory_Information *mem, |
---|
52 | Heap_Initialization_or_extend_handler extend |
---|
53 | ) |
---|
54 | { |
---|
55 | Heap_Initialization_or_extend_handler init_or_extend; |
---|
56 | uintptr_t remaining; |
---|
57 | bool unified; |
---|
58 | uintptr_t page_size; |
---|
59 | uintptr_t overhead; |
---|
60 | size_t i; |
---|
61 | |
---|
62 | page_size = CPU_HEAP_ALIGNMENT; |
---|
63 | remaining = rtems_configuration_get_work_space_size(); |
---|
64 | init_or_extend = _Heap_Initialize; |
---|
65 | unified = rtems_configuration_get_unified_work_area(); |
---|
66 | overhead = _Heap_Area_overhead( page_size ); |
---|
67 | |
---|
68 | for ( i = 0; i < _Memory_Get_count( mem ); ++i ) { |
---|
69 | Memory_Area *area; |
---|
70 | uintptr_t free_size; |
---|
71 | |
---|
72 | area = _Memory_Get_area( mem, i ); |
---|
73 | free_size = _Memory_Get_free_size( area ); |
---|
74 | |
---|
75 | if ( free_size > overhead ) { |
---|
76 | uintptr_t space_available; |
---|
77 | uintptr_t size; |
---|
78 | |
---|
79 | if ( unified ) { |
---|
80 | size = free_size; |
---|
81 | } else { |
---|
82 | if ( remaining > 0 ) { |
---|
83 | size = remaining < free_size - overhead ? |
---|
84 | remaining + overhead : free_size; |
---|
85 | } else { |
---|
86 | size = 0; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | space_available = ( *init_or_extend )( |
---|
91 | &_Workspace_Area, |
---|
92 | _Memory_Get_free_begin( area ), |
---|
93 | size, |
---|
94 | page_size |
---|
95 | ); |
---|
96 | |
---|
97 | _Memory_Consume( area, size ); |
---|
98 | |
---|
99 | if ( space_available < remaining ) { |
---|
100 | remaining -= space_available; |
---|
101 | } else { |
---|
102 | remaining = 0; |
---|
103 | } |
---|
104 | |
---|
105 | init_or_extend = extend; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | if ( remaining > 0 ) { |
---|
110 | _Internal_error( INTERNAL_ERROR_TOO_LITTLE_WORKSPACE ); |
---|
111 | } |
---|
112 | |
---|
113 | _Heap_Protection_set_delayed_free_fraction( &_Workspace_Area, 1 ); |
---|
114 | } |
---|
115 | |
---|
116 | void *_Workspace_Allocate( |
---|
117 | size_t size |
---|
118 | ) |
---|
119 | { |
---|
120 | void *memory; |
---|
121 | |
---|
122 | memory = _Heap_Allocate( &_Workspace_Area, size ); |
---|
123 | #if defined(DEBUG_WORKSPACE) |
---|
124 | printk( |
---|
125 | "Workspace_Allocate(%d) from %p/%p -> %p\n", |
---|
126 | size, |
---|
127 | __builtin_return_address( 0 ), |
---|
128 | __builtin_return_address( 1 ), |
---|
129 | memory |
---|
130 | ); |
---|
131 | #endif |
---|
132 | return memory; |
---|
133 | } |
---|
134 | |
---|
135 | void *_Workspace_Allocate_aligned( size_t size, size_t alignment ) |
---|
136 | { |
---|
137 | return _Heap_Allocate_aligned( &_Workspace_Area, size, alignment ); |
---|
138 | } |
---|
139 | |
---|
140 | /* |
---|
141 | * _Workspace_Free |
---|
142 | */ |
---|
143 | void _Workspace_Free( |
---|
144 | void *block |
---|
145 | ) |
---|
146 | { |
---|
147 | #if defined(DEBUG_WORKSPACE) |
---|
148 | printk( |
---|
149 | "Workspace_Free(%p) from %p/%p\n", |
---|
150 | block, |
---|
151 | __builtin_return_address( 0 ), |
---|
152 | __builtin_return_address( 1 ) |
---|
153 | ); |
---|
154 | #endif |
---|
155 | _Heap_Free( &_Workspace_Area, block ); |
---|
156 | } |
---|