1 | /* |
---|
2 | * Region Manager |
---|
3 | * |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-2007. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.com/license/LICENSE. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #if HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <rtems/system.h> |
---|
20 | #include <rtems/rtems/status.h> |
---|
21 | #include <rtems/rtems/support.h> |
---|
22 | #include <rtems/score/object.h> |
---|
23 | #include <rtems/rtems/options.h> |
---|
24 | #include <rtems/rtems/region.h> |
---|
25 | #include <rtems/score/states.h> |
---|
26 | #include <rtems/score/thread.h> |
---|
27 | #include <rtems/score/apimutex.h> |
---|
28 | |
---|
29 | /*PAGE |
---|
30 | * |
---|
31 | * rtems_region_get_segment |
---|
32 | * |
---|
33 | * This directive will obtain a segment from the given region. |
---|
34 | * |
---|
35 | * Input parameters: |
---|
36 | * id - region id |
---|
37 | * size - segment size in bytes |
---|
38 | * option_set - wait option |
---|
39 | * timeout - number of ticks to wait (0 means wait forever) |
---|
40 | * segment - pointer to segment address |
---|
41 | * |
---|
42 | * Output parameters: |
---|
43 | * segment - pointer to segment address filled in |
---|
44 | * RTEMS_SUCCESSFUL - if successful |
---|
45 | * error code - if unsuccessful |
---|
46 | */ |
---|
47 | |
---|
48 | rtems_status_code rtems_region_get_segment( |
---|
49 | Objects_Id id, |
---|
50 | intptr_t size, |
---|
51 | rtems_option option_set, |
---|
52 | rtems_interval timeout, |
---|
53 | void **segment |
---|
54 | ) |
---|
55 | { |
---|
56 | Thread_Control *executing; |
---|
57 | Objects_Locations location; |
---|
58 | rtems_status_code return_status = RTEMS_INTERNAL_ERROR; |
---|
59 | register Region_Control *the_region; |
---|
60 | void *the_segment; |
---|
61 | |
---|
62 | if ( !segment ) |
---|
63 | return RTEMS_INVALID_ADDRESS; |
---|
64 | |
---|
65 | *segment = NULL; |
---|
66 | |
---|
67 | if ( size == 0 ) |
---|
68 | return RTEMS_INVALID_SIZE; |
---|
69 | |
---|
70 | _RTEMS_Lock_allocator(); |
---|
71 | |
---|
72 | executing = _Thread_Executing; |
---|
73 | the_region = _Region_Get( id, &location ); |
---|
74 | switch ( location ) { |
---|
75 | |
---|
76 | case OBJECTS_LOCAL: |
---|
77 | if ( size > the_region->maximum_segment_size ) |
---|
78 | return_status = RTEMS_INVALID_SIZE; |
---|
79 | |
---|
80 | else { |
---|
81 | _Region_Debug_Walk( the_region, 1 ); |
---|
82 | |
---|
83 | the_segment = _Region_Allocate_segment( the_region, size ); |
---|
84 | |
---|
85 | _Region_Debug_Walk( the_region, 2 ); |
---|
86 | |
---|
87 | if ( the_segment ) { |
---|
88 | the_region->number_of_used_blocks += 1; |
---|
89 | *segment = the_segment; |
---|
90 | return_status = RTEMS_SUCCESSFUL; |
---|
91 | } |
---|
92 | |
---|
93 | else if ( _Options_Is_no_wait( option_set ) ) { |
---|
94 | return_status = RTEMS_UNSATISFIED; |
---|
95 | } |
---|
96 | |
---|
97 | else { |
---|
98 | /* |
---|
99 | * Switch from using the memory allocation mutex to using a |
---|
100 | * dispatching disabled critical section. We have to do this |
---|
101 | * because this thread is going to block. |
---|
102 | */ |
---|
103 | _Thread_Disable_dispatch(); |
---|
104 | _RTEMS_Unlock_allocator(); |
---|
105 | |
---|
106 | executing->Wait.queue = &the_region->Wait_queue; |
---|
107 | executing->Wait.id = id; |
---|
108 | executing->Wait.count = size; |
---|
109 | executing->Wait.return_argument = segment; |
---|
110 | |
---|
111 | _Thread_queue_Enter_critical_section( &the_region->Wait_queue ); |
---|
112 | |
---|
113 | _Thread_queue_Enqueue( &the_region->Wait_queue, timeout ); |
---|
114 | |
---|
115 | _Thread_Enable_dispatch(); |
---|
116 | |
---|
117 | return (rtems_status_code) executing->Wait.return_code; |
---|
118 | } |
---|
119 | } |
---|
120 | break; |
---|
121 | |
---|
122 | #if defined(RTEMS_MULTIPROCESSING) |
---|
123 | case OBJECTS_REMOTE: /* this error cannot be returned */ |
---|
124 | break; |
---|
125 | #endif |
---|
126 | |
---|
127 | case OBJECTS_ERROR: |
---|
128 | return_status = RTEMS_INVALID_ID; |
---|
129 | break; |
---|
130 | } |
---|
131 | |
---|
132 | _RTEMS_Unlock_allocator(); |
---|
133 | return return_status; |
---|
134 | } |
---|