source: rtems/c/src/exec/rtems/src/region.c @ 9863dbf

4.104.114.84.95
Last change on this file since 9863dbf was 9863dbf, checked in by Joel Sherrill <joel.sherrill@…>, on 08/18/95 at 21:42:58

+ Added object type field to object id.

+ Added name pointer to Object_Control.

+ Modified Object Open and Close to address name field.

+ Removed name as separate element from Thread and Proxy Control.

+ Added parameter "object class" to calls to Initialize Information

  • Property mode set to 100644
File size: 11.7 KB
Line 
1/*
2 *  Region Manager
3 *
4 *
5 *  COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994.
6 *  On-Line Applications Research Corporation (OAR).
7 *  All rights assigned to U.S. Government, 1994.
8 *
9 *  This material may be reproduced by or for the U.S. Government pursuant
10 *  to the copyright license under the clause at DFARS 252.227-7013.  This
11 *  notice must appear in all copies of this file and its derivatives.
12 *
13 *  $Id$
14 */
15
16#include <rtems/system.h>
17#include <rtems/config.h>
18#include <rtems/object.h>
19#include <rtems/options.h>
20#include <rtems/region.h>
21#include <rtems/states.h>
22#include <rtems/thread.h>
23
24/*PAGE
25 *
26 *  _Region_Manager_initialization
27 *
28 *  This routine initializes all region manager related data structures.
29 *
30 *  Input parameters:
31 *    maximum_regions - number of regions to initialize
32 *
33 *  Output parameters:  NONE
34 */
35
36void _Region_Manager_initialization(
37  unsigned32 maximum_regions
38)
39{
40  _Objects_Initialize_information(
41     &_Region_Information,
42     OBJECTS_RTEMS_REGIONS,
43     FALSE,
44     maximum_regions,
45     sizeof( Region_Control )
46   );
47}
48
49/*PAGE
50 *
51 *  rtems_region_create
52 *
53 *  This directive creates a region of physical contiguous memory area
54 *  from which variable sized segments can be allocated.
55 *
56 *  Input parameters:
57 *    name             - user defined region name
58 *    starting_address - physical start address of region
59 *    length           - physical length in bytes
60 *    page_size        - page size in bytes
61 *    attribute_set    - region attributes
62 *    id               - address of region id to set
63 *
64 *  Output parameters:
65 *    id       - region id
66 *    RTEMS_SUCCESSFUL - if successful
67 *    error code - if unsuccessful
68 */
69
70rtems_status_code rtems_region_create(
71  Objects_Name        name,
72  void               *starting_address,
73  unsigned32          length,
74  unsigned32          page_size,
75  rtems_attribute  attribute_set,
76  Objects_Id         *id
77)
78{
79  Region_Control *the_region;
80
81  if ( !_Objects_Is_name_valid( name ) )
82    return ( RTEMS_INVALID_NAME );
83
84  if ( !_Addresses_Is_aligned( starting_address ) )
85    return( RTEMS_INVALID_ADDRESS );
86
87  _Thread_Disable_dispatch();             /* to prevent deletion */
88
89  the_region = _Region_Allocate();
90
91  if ( !the_region ) {
92    _Thread_Enable_dispatch();
93    return( RTEMS_TOO_MANY );
94  }
95
96  the_region->maximum_segment_size =
97    _Heap_Initialize(&the_region->Memory, starting_address, length, page_size);
98
99  if ( !the_region->maximum_segment_size ) {
100    _Region_Free( the_region );
101    _Thread_Enable_dispatch();
102    return( RTEMS_INVALID_SIZE );
103  }
104
105  the_region->starting_address      = starting_address;
106  the_region->length                = length;
107  the_region->page_size             = page_size;
108  the_region->attribute_set         = attribute_set;
109  the_region->number_of_used_blocks = 0;
110
111  _Thread_queue_Initialize(
112     &the_region->Wait_queue, attribute_set, STATES_WAITING_FOR_SEGMENT );
113
114  _Objects_Open( &_Region_Information, &the_region->Object, name );
115
116  *id = the_region->Object.id;
117  _Thread_Enable_dispatch();
118  return( RTEMS_SUCCESSFUL );
119}
120
121/*PAGE
122 *
123 *  rtems_region_ident
124 *
125 *  This directive returns the system ID associated with
126 *  the region name.
127 *
128 *  Input parameters:
129 *    name - user defined region name
130 *    id   - pointer to region id
131 *
132 *  Output parameters:
133 *    *id      - region id
134 *    RTEMS_SUCCESSFUL - if successful
135 *    error code - if unsuccessful
136 */
137
138rtems_status_code rtems_region_ident(
139  Objects_Name  name,
140  Objects_Id   *id
141)
142{
143  return _Objects_Name_to_id(
144      &_Region_Information,
145      name,
146      RTEMS_SEARCH_LOCAL_NODE,
147      id
148    );
149}
150
151/*PAGE
152 *
153 *  rtems_region_delete
154 *
155 *  This directive allows a thread to delete a region specified by
156 *  the region identifier, provided that none of its segments are
157 *  still allocated.
158 *
159 *  Input parameters:
160 *    id - region id
161 *
162 *  Output parameters:
163 *    RTEMS_SUCCESSFUL - if successful
164 *    error code - if unsuccessful
165 */
166
167rtems_status_code rtems_region_delete(
168  Objects_Id id
169)
170{
171  register Region_Control *the_region;
172  Objects_Locations               location;
173
174  the_region = _Region_Get( id, &location );
175  switch ( location ) {
176    case OBJECTS_ERROR:
177      return( RTEMS_INVALID_ID );
178    case OBJECTS_REMOTE:        /* this error cannot be returned */
179      return( RTEMS_INTERNAL_ERROR );
180    case OBJECTS_LOCAL:
181      _Region_Debug_Walk( the_region, 5 );
182      if ( the_region->number_of_used_blocks == 0 ) {
183        _Objects_Close( &_Region_Information, &the_region->Object );
184        _Region_Free( the_region );
185        _Thread_Enable_dispatch();
186        return( RTEMS_SUCCESSFUL );
187      }
188      _Thread_Enable_dispatch();
189      return( RTEMS_RESOURCE_IN_USE );
190  }
191
192  return( RTEMS_INTERNAL_ERROR );   /* unreached - only to remove warnings */
193}
194
195/*PAGE
196 *
197 *  rtems_region_extend
198 *
199 *  This directive attempts to grow a region of physical contiguous memory area
200 *  from which variable sized segments can be allocated.
201 *
202 *  Input parameters:
203 *    id         - id of region to grow
204 *    start      - starting address of memory area for extension
205 *    length     - physical length in bytes to grow the region
206 *
207 *  Output parameters:
208 *    RTEMS_SUCCESSFUL - if successful
209 *    error code       - if unsuccessful
210 */
211
212rtems_status_code rtems_region_extend(
213  Objects_Id          id,
214  void               *starting_address,
215  unsigned32          length
216)
217{
218  Region_Control     *the_region;
219  Objects_Locations   location;
220  unsigned32          amount_extended;
221  Heap_Extend_status  heap_status;
222  rtems_status_code   status;
223
224  status = RTEMS_SUCCESSFUL;
225
226  the_region = _Region_Get( id, &location );
227  switch ( location ) {
228    case OBJECTS_ERROR:
229      return( RTEMS_INVALID_ID );
230    case OBJECTS_REMOTE:        /* this error cannot be returned */
231      return( RTEMS_INTERNAL_ERROR );
232    case OBJECTS_LOCAL:
233
234      heap_status = _Heap_Extend(
235        &the_region->Memory,
236        starting_address,
237        length,
238        &amount_extended
239      );
240
241      switch ( heap_status ) {
242        case HEAP_EXTEND_SUCCESSFUL:
243          the_region->length                += amount_extended;
244          the_region->maximum_segment_size  += amount_extended;
245          break;
246        case HEAP_EXTEND_ERROR:
247          status = RTEMS_INVALID_ADDRESS;
248          break;
249        case HEAP_EXTEND_NOT_IMPLEMENTED:
250          status = RTEMS_NOT_IMPLEMENTED;
251          break;
252      }
253      _Thread_Enable_dispatch();
254      return( status );
255  }
256
257  return( RTEMS_INTERNAL_ERROR );
258}
259
260/*PAGE
261 *
262 *  rtems_region_get_segment
263 *
264 *  This directive will obtain a segment from the given region.
265 *
266 *  Input parameters:
267 *    id         - region id
268 *    size       - segment size in bytes
269 *    option_set - wait option
270 *    timeout    - number of ticks to wait (0 means wait forever)
271 *    segment    - pointer to segment address
272 *
273 *  Output parameters:
274 *    segment    - pointer to segment address filled in
275 *    RTEMS_SUCCESSFUL - if successful
276 *    error code - if unsuccessful
277 */
278
279rtems_status_code rtems_region_get_segment(
280  Objects_Id         id,
281  unsigned32         size,
282  rtems_option    option_set,
283  rtems_interval  timeout,
284  void              **segment
285)
286{
287  register Region_Control *the_region;
288  Objects_Locations        location;
289  Thread_Control          *executing;
290  void                    *the_segment;
291
292  if ( size == 0 )
293    return( RTEMS_INVALID_SIZE );
294
295  executing  = _Thread_Executing;
296  the_region = _Region_Get( id, &location );
297  switch ( location ) {
298    case OBJECTS_ERROR:
299      return( RTEMS_INVALID_ID );
300    case OBJECTS_REMOTE:        /* this error cannot be returned */
301      return( RTEMS_INTERNAL_ERROR );
302    case OBJECTS_LOCAL:
303      if ( size > the_region->maximum_segment_size ) {
304        _Thread_Enable_dispatch();
305        return( RTEMS_INVALID_SIZE );
306      }
307
308      _Region_Debug_Walk( the_region, 1 );
309
310      the_segment = _Region_Allocate_segment( the_region, size );
311
312      _Region_Debug_Walk( the_region, 2 );
313
314      if ( the_segment ) {
315        the_region->number_of_used_blocks += 1;
316        _Thread_Enable_dispatch();
317        *segment = the_segment;
318        return( RTEMS_SUCCESSFUL );
319      }
320
321      if ( _Options_Is_no_wait( option_set ) ) {
322        _Thread_Enable_dispatch();
323        return( RTEMS_UNSATISFIED );
324      }
325
326      executing->Wait.queue              = &the_region->Wait_queue;
327      executing->Wait.id                 = id;
328      executing->Wait.Extra.segment_size = size;
329      executing->Wait.return_argument    = (unsigned32 *) segment;
330
331      the_region->Wait_queue.sync = TRUE;
332
333      _Thread_queue_Enqueue( &the_region->Wait_queue, timeout );
334
335      _Thread_Enable_dispatch();
336      return( executing->Wait.return_code );
337  }
338
339  return( RTEMS_INTERNAL_ERROR );   /* unreached - only to remove warnings */
340}
341/*PAGE
342 *
343 *  rtems_region_get_segment_size
344 *
345 *  This directive will return the size of the segment indicated
346 *
347 *  Input parameters:
348 *    id         - region id
349 *    segment    - segment address
350 *    size       - pointer to segment size in bytes
351 *
352 *  Output parameters:
353 *    size       - segment size in bytes filled in
354 *    RTEMS_SUCCESSFUL - if successful
355 *    error code - if unsuccessful
356 */
357
358rtems_status_code rtems_region_get_segment_size(
359  Objects_Id         id,
360  void              *segment,
361  unsigned32        *size
362)
363{
364  register Region_Control *the_region;
365  Objects_Locations        location;
366  Thread_Control          *executing;
367
368  executing  = _Thread_Executing;
369  the_region = _Region_Get( id, &location );
370  switch ( location ) {
371    case OBJECTS_ERROR:
372      return( RTEMS_INVALID_ID );
373    case OBJECTS_REMOTE:        /* this error cannot be returned */
374      return( RTEMS_INTERNAL_ERROR );
375    case OBJECTS_LOCAL:
376
377      if ( _Heap_Size_of_user_area( &the_region->Memory, segment, size ) ) {
378        _Thread_Enable_dispatch();
379        return( RTEMS_SUCCESSFUL );
380      }
381      _Thread_Enable_dispatch();
382      return( RTEMS_INVALID_ADDRESS );
383  }
384
385  return( RTEMS_INTERNAL_ERROR );   /* unreached - only to remove warnings */
386}
387
388/*PAGE
389 *
390 *  rtems_region_return_segment
391 *
392 *  This directive will return a segment to its region.
393 *
394 *  Input parameters:
395 *    id      - region id
396 *    segment - pointer to segment address
397 *
398 *  Output parameters:
399 *    RTEMS_SUCCESSFUL - if successful
400 *    error code        - if unsuccessful
401 */
402
403rtems_status_code rtems_region_return_segment(
404  Objects_Id  id,
405  void       *segment
406)
407{
408  register Region_Control *the_region;
409  Thread_Control          *the_thread;
410  Objects_Locations        location;
411  void                   **the_segment;
412  int                      status;
413
414  the_region = _Region_Get( id, &location );
415  switch ( location ) {
416    case OBJECTS_ERROR:
417      return( RTEMS_INVALID_ID );
418    case OBJECTS_REMOTE:        /* this error cannot be returned */
419      return( RTEMS_INTERNAL_ERROR );
420    case OBJECTS_LOCAL:
421
422      _Region_Debug_Walk( the_region, 3 );
423
424      status = _Region_Free_segment( the_region, segment );
425
426      _Region_Debug_Walk( the_region, 4 );
427
428      if ( !status ) {
429        _Thread_Enable_dispatch();
430        return( RTEMS_INVALID_ADDRESS );
431      }
432
433      the_region->number_of_used_blocks -= 1;
434      for ( ; ; ) {
435        the_thread = _Thread_queue_First( &the_region->Wait_queue );
436
437        if ( the_thread == NULL )
438           break;
439
440        the_segment = _Region_Allocate_segment(
441                        the_region, the_thread->Wait.Extra.segment_size );
442
443        if ( the_segment == NULL )
444           break;
445
446        *(void **)the_thread->Wait.return_argument = the_segment;
447        the_region->number_of_used_blocks += 1;
448        _Thread_queue_Extract( &the_region->Wait_queue, the_thread );
449        the_thread->Wait.return_code = RTEMS_SUCCESSFUL;
450      }
451
452      _Thread_Enable_dispatch();
453      return( RTEMS_SUCCESSFUL );
454  }
455
456  return( RTEMS_INTERNAL_ERROR );   /* unreached - only to remove warnings */
457}
Note: See TracBrowser for help on using the repository browser.