1 | /* |
---|
2 | * Region Manager |
---|
3 | * |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-1998. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * Copyright assigned to U.S. Government, 1994. |
---|
8 | * |
---|
9 | * The license and distribution terms for this file may be |
---|
10 | * found in the file LICENSE in this distribution or at |
---|
11 | * http://www.OARcorp.com/rtems/license.html. |
---|
12 | * |
---|
13 | * $Id$ |
---|
14 | */ |
---|
15 | |
---|
16 | #include <rtems/system.h> |
---|
17 | #include <rtems/rtems/status.h> |
---|
18 | #include <rtems/rtems/support.h> |
---|
19 | #include <rtems/score/object.h> |
---|
20 | #include <rtems/rtems/options.h> |
---|
21 | #include <rtems/rtems/region.h> |
---|
22 | #include <rtems/score/states.h> |
---|
23 | #include <rtems/score/thread.h> |
---|
24 | |
---|
25 | /*PAGE |
---|
26 | * |
---|
27 | * rtems_region_get_segment |
---|
28 | * |
---|
29 | * This directive will obtain a segment from the given region. |
---|
30 | * |
---|
31 | * Input parameters: |
---|
32 | * id - region id |
---|
33 | * size - segment size in bytes |
---|
34 | * option_set - wait option |
---|
35 | * timeout - number of ticks to wait (0 means wait forever) |
---|
36 | * segment - pointer to segment address |
---|
37 | * |
---|
38 | * Output parameters: |
---|
39 | * segment - pointer to segment address filled in |
---|
40 | * RTEMS_SUCCESSFUL - if successful |
---|
41 | * error code - if unsuccessful |
---|
42 | */ |
---|
43 | |
---|
44 | rtems_status_code rtems_region_get_segment( |
---|
45 | Objects_Id id, |
---|
46 | unsigned32 size, |
---|
47 | rtems_option option_set, |
---|
48 | rtems_interval timeout, |
---|
49 | void **segment |
---|
50 | ) |
---|
51 | { |
---|
52 | register Region_Control *the_region; |
---|
53 | Objects_Locations location; |
---|
54 | Thread_Control *executing; |
---|
55 | void *the_segment; |
---|
56 | |
---|
57 | *segment = NULL; |
---|
58 | |
---|
59 | if ( size == 0 ) |
---|
60 | return RTEMS_INVALID_SIZE; |
---|
61 | |
---|
62 | executing = _Thread_Executing; |
---|
63 | the_region = _Region_Get( id, &location ); |
---|
64 | switch ( location ) { |
---|
65 | case OBJECTS_REMOTE: /* this error cannot be returned */ |
---|
66 | return RTEMS_INTERNAL_ERROR; |
---|
67 | |
---|
68 | case OBJECTS_ERROR: |
---|
69 | return RTEMS_INVALID_ID; |
---|
70 | |
---|
71 | case OBJECTS_LOCAL: |
---|
72 | if ( size > the_region->maximum_segment_size ) { |
---|
73 | _Thread_Enable_dispatch(); |
---|
74 | return RTEMS_INVALID_SIZE; |
---|
75 | } |
---|
76 | |
---|
77 | _Region_Debug_Walk( the_region, 1 ); |
---|
78 | |
---|
79 | the_segment = _Region_Allocate_segment( the_region, size ); |
---|
80 | |
---|
81 | _Region_Debug_Walk( the_region, 2 ); |
---|
82 | |
---|
83 | if ( the_segment ) { |
---|
84 | the_region->number_of_used_blocks += 1; |
---|
85 | _Thread_Enable_dispatch(); |
---|
86 | *segment = the_segment; |
---|
87 | return RTEMS_SUCCESSFUL; |
---|
88 | } |
---|
89 | |
---|
90 | if ( _Options_Is_no_wait( option_set ) ) { |
---|
91 | _Thread_Enable_dispatch(); |
---|
92 | return RTEMS_UNSATISFIED; |
---|
93 | } |
---|
94 | |
---|
95 | executing->Wait.queue = &the_region->Wait_queue; |
---|
96 | executing->Wait.id = id; |
---|
97 | executing->Wait.count = size; |
---|
98 | executing->Wait.return_argument = (unsigned32 *) segment; |
---|
99 | |
---|
100 | _Thread_queue_Enter_critical_section( &the_region->Wait_queue ); |
---|
101 | |
---|
102 | _Thread_queue_Enqueue( &the_region->Wait_queue, timeout ); |
---|
103 | |
---|
104 | _Thread_Enable_dispatch(); |
---|
105 | return (rtems_status_code) executing->Wait.return_code; |
---|
106 | } |
---|
107 | |
---|
108 | return RTEMS_INTERNAL_ERROR; /* unreached - only to remove warnings */ |
---|
109 | } |
---|