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.com/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/system.h> |
---|
22 | #include <rtems/rtems/status.h> |
---|
23 | #include <rtems/rtems/support.h> |
---|
24 | #include <rtems/score/object.h> |
---|
25 | #include <rtems/rtems/optionsimpl.h> |
---|
26 | #include <rtems/rtems/regionimpl.h> |
---|
27 | #include <rtems/score/thread.h> |
---|
28 | #include <rtems/score/apimutex.h> |
---|
29 | |
---|
30 | rtems_status_code rtems_region_get_segment( |
---|
31 | rtems_id id, |
---|
32 | uintptr_t size, |
---|
33 | rtems_option option_set, |
---|
34 | rtems_interval timeout, |
---|
35 | void **segment |
---|
36 | ) |
---|
37 | { |
---|
38 | Thread_Control *executing; |
---|
39 | Objects_Locations location; |
---|
40 | rtems_status_code return_status; |
---|
41 | Region_Control *the_region; |
---|
42 | void *the_segment; |
---|
43 | |
---|
44 | if ( !segment ) |
---|
45 | return RTEMS_INVALID_ADDRESS; |
---|
46 | |
---|
47 | *segment = NULL; |
---|
48 | |
---|
49 | if ( size == 0 ) |
---|
50 | return RTEMS_INVALID_SIZE; |
---|
51 | |
---|
52 | _RTEMS_Lock_allocator(); |
---|
53 | |
---|
54 | executing = _Thread_Executing; |
---|
55 | the_region = _Region_Get( id, &location ); |
---|
56 | switch ( location ) { |
---|
57 | |
---|
58 | case OBJECTS_LOCAL: |
---|
59 | if ( size > the_region->maximum_segment_size ) |
---|
60 | return_status = RTEMS_INVALID_SIZE; |
---|
61 | |
---|
62 | else { |
---|
63 | _Region_Debug_Walk( the_region, 1 ); |
---|
64 | |
---|
65 | the_segment = _Region_Allocate_segment( the_region, size ); |
---|
66 | |
---|
67 | _Region_Debug_Walk( the_region, 2 ); |
---|
68 | |
---|
69 | if ( the_segment ) { |
---|
70 | the_region->number_of_used_blocks += 1; |
---|
71 | *segment = the_segment; |
---|
72 | return_status = RTEMS_SUCCESSFUL; |
---|
73 | } else if ( _Options_Is_no_wait( option_set ) ) { |
---|
74 | return_status = RTEMS_UNSATISFIED; |
---|
75 | } else { |
---|
76 | /* |
---|
77 | * Switch from using the memory allocation mutex to using a |
---|
78 | * dispatching disabled critical section. We have to do this |
---|
79 | * because this thread is going to block. |
---|
80 | */ |
---|
81 | /* FIXME: Lock order reversal */ |
---|
82 | _Thread_Disable_dispatch(); |
---|
83 | _RTEMS_Unlock_allocator(); |
---|
84 | |
---|
85 | executing->Wait.queue = &the_region->Wait_queue; |
---|
86 | executing->Wait.id = id; |
---|
87 | executing->Wait.count = size; |
---|
88 | executing->Wait.return_argument = segment; |
---|
89 | |
---|
90 | _Thread_queue_Enter_critical_section( &the_region->Wait_queue ); |
---|
91 | |
---|
92 | _Thread_queue_Enqueue( &the_region->Wait_queue, timeout ); |
---|
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 | } |
---|