source: rtems/cpukit/rtems/src/regiongetsegment.c @ a29d2e7

4.104.114.84.95
Last change on this file since a29d2e7 was 1095ec1, checked in by Ralf Corsepius <ralf.corsepius@…>, on 01/18/05 at 09:03:45

Include config.h.

  • Property mode set to 100644
File size: 3.3 KB
Line 
1/*
2 *  Region Manager
3 *
4 *
5 *  COPYRIGHT (c) 1989-1999.
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
48rtems_status_code rtems_region_get_segment(
49  Objects_Id         id,
50  uint32_t           size,
51  rtems_option       option_set,
52  rtems_interval     timeout,
53  void              **segment
54)
55{
56  register Region_Control *the_region;
57  Objects_Locations        location;
58  Thread_Control          *executing;
59  void                    *the_segment;
60
61  if ( !segment )
62    return RTEMS_INVALID_ADDRESS;
63
64  *segment = NULL;
65
66  if ( size == 0 )
67    return RTEMS_INVALID_SIZE;
68
69  _RTEMS_Lock_allocator();
70  executing  = _Thread_Executing;
71  the_region = _Region_Get( id, &location );
72  switch ( location ) {
73    case OBJECTS_REMOTE:        /* this error cannot be returned */
74      _RTEMS_Unlock_allocator();
75      return RTEMS_INTERNAL_ERROR;
76
77    case OBJECTS_ERROR:
78      _RTEMS_Unlock_allocator();
79      return RTEMS_INVALID_ID;
80
81    case OBJECTS_LOCAL:
82      if ( size > the_region->maximum_segment_size ) {
83        _RTEMS_Unlock_allocator();
84        return RTEMS_INVALID_SIZE;
85      }
86
87      _Region_Debug_Walk( the_region, 1 );
88
89      the_segment = _Region_Allocate_segment( the_region, size );
90
91      _Region_Debug_Walk( the_region, 2 );
92
93      if ( the_segment ) {
94        the_region->number_of_used_blocks += 1;
95        _RTEMS_Unlock_allocator();
96        *segment = the_segment;
97        return RTEMS_SUCCESSFUL;
98      }
99
100      if ( _Options_Is_no_wait( option_set ) ) {
101        _RTEMS_Unlock_allocator();
102        return RTEMS_UNSATISFIED;
103      }
104
105      /*
106       *  Switch from using the memory allocation mutex to using a
107       *  dispatching disabled critical section.  We have to do this
108       *  because this thread is going to block.
109       */
110      _Thread_Disable_dispatch();
111      _RTEMS_Unlock_allocator();
112
113      executing->Wait.queue           = &the_region->Wait_queue;
114      executing->Wait.id              = id;
115      executing->Wait.count           = size;
116      executing->Wait.return_argument = segment;
117
118      _Thread_queue_Enter_critical_section( &the_region->Wait_queue );
119
120      _Thread_queue_Enqueue( &the_region->Wait_queue, timeout );
121
122      _Thread_Enable_dispatch();
123
124      return (rtems_status_code) executing->Wait.return_code;
125  }
126
127  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
128}
Note: See TracBrowser for help on using the repository browser.