1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief RTEMS Get Region Segment |
---|
5 | * @ingroup ClassicRegion |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2007. |
---|
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/rtems/regionimpl.h> |
---|
22 | #include <rtems/rtems/optionsimpl.h> |
---|
23 | #include <rtems/score/apimutex.h> |
---|
24 | #include <rtems/score/threadqimpl.h> |
---|
25 | |
---|
26 | rtems_status_code rtems_region_get_segment( |
---|
27 | rtems_id id, |
---|
28 | uintptr_t size, |
---|
29 | rtems_option option_set, |
---|
30 | rtems_interval timeout, |
---|
31 | void **segment |
---|
32 | ) |
---|
33 | { |
---|
34 | Thread_Control *executing; |
---|
35 | Objects_Locations location; |
---|
36 | rtems_status_code return_status; |
---|
37 | Region_Control *the_region; |
---|
38 | void *the_segment; |
---|
39 | |
---|
40 | if ( !segment ) |
---|
41 | return RTEMS_INVALID_ADDRESS; |
---|
42 | |
---|
43 | *segment = NULL; |
---|
44 | |
---|
45 | if ( size == 0 ) |
---|
46 | return RTEMS_INVALID_SIZE; |
---|
47 | |
---|
48 | _RTEMS_Lock_allocator(); |
---|
49 | |
---|
50 | executing = _Thread_Executing; |
---|
51 | the_region = _Region_Get( id, &location ); |
---|
52 | switch ( location ) { |
---|
53 | |
---|
54 | case OBJECTS_LOCAL: |
---|
55 | if ( size > the_region->maximum_segment_size ) |
---|
56 | return_status = RTEMS_INVALID_SIZE; |
---|
57 | |
---|
58 | else { |
---|
59 | _Region_Debug_Walk( the_region, 1 ); |
---|
60 | |
---|
61 | the_segment = _Region_Allocate_segment( the_region, size ); |
---|
62 | |
---|
63 | _Region_Debug_Walk( the_region, 2 ); |
---|
64 | |
---|
65 | if ( the_segment ) { |
---|
66 | the_region->number_of_used_blocks += 1; |
---|
67 | *segment = the_segment; |
---|
68 | return_status = RTEMS_SUCCESSFUL; |
---|
69 | } else if ( _Options_Is_no_wait( option_set ) ) { |
---|
70 | return_status = RTEMS_UNSATISFIED; |
---|
71 | } else { |
---|
72 | /* |
---|
73 | * Switch from using the memory allocation mutex to using a |
---|
74 | * dispatching disabled critical section. We have to do this |
---|
75 | * because this thread is going to block. |
---|
76 | */ |
---|
77 | /* FIXME: Lock order reversal */ |
---|
78 | _Thread_Disable_dispatch(); |
---|
79 | _RTEMS_Unlock_allocator(); |
---|
80 | |
---|
81 | executing->Wait.queue = &the_region->Wait_queue; |
---|
82 | executing->Wait.id = id; |
---|
83 | executing->Wait.count = size; |
---|
84 | executing->Wait.return_argument = segment; |
---|
85 | |
---|
86 | _Thread_queue_Enter_critical_section( &the_region->Wait_queue ); |
---|
87 | |
---|
88 | _Thread_queue_Enqueue( |
---|
89 | &the_region->Wait_queue, |
---|
90 | executing, |
---|
91 | timeout |
---|
92 | ); |
---|
93 | |
---|
94 | _Objects_Put( &the_region->Object ); |
---|
95 | |
---|
96 | return (rtems_status_code) executing->Wait.return_code; |
---|
97 | } |
---|
98 | } |
---|
99 | break; |
---|
100 | |
---|
101 | #if defined(RTEMS_MULTIPROCESSING) |
---|
102 | case OBJECTS_REMOTE: /* this error cannot be returned */ |
---|
103 | break; |
---|
104 | #endif |
---|
105 | |
---|
106 | case OBJECTS_ERROR: |
---|
107 | default: |
---|
108 | return_status = RTEMS_INVALID_ID; |
---|
109 | break; |
---|
110 | } |
---|
111 | |
---|
112 | _RTEMS_Unlock_allocator(); |
---|
113 | return return_status; |
---|
114 | } |
---|