source: rtems/cpukit/rtems/src/regionreturnsegment.c @ e7541849

4.104.114.84.95
Last change on this file since e7541849 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.5 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#ifdef RTEMS_REGION_SHRED_ON_FREE
20#include <string.h>
21
22#ifndef RTEMS_REGION_FREE_SHRED_PATTERN
23#define RTEMS_REGION_FREE_SHRED_PATTERN 0x00
24#endif
25#endif
26
27#include <rtems/system.h>
28#include <rtems/rtems/status.h>
29#include <rtems/rtems/support.h>
30#include <rtems/score/object.h>
31#include <rtems/rtems/options.h>
32#include <rtems/rtems/region.h>
33#include <rtems/score/states.h>
34#include <rtems/score/thread.h>
35#include <rtems/score/apimutex.h>
36
37/*PAGE
38 *
39 *  rtems_region_return_segment
40 *
41 *  This directive will return a segment to its region.
42 *
43 *  Input parameters:
44 *    id      - region id
45 *    segment - pointer to segment address
46 *
47 *  Output parameters:
48 *    RTEMS_SUCCESSFUL - if successful
49 *    error code       - if unsuccessful
50 */
51
52rtems_status_code rtems_region_return_segment(
53  Objects_Id  id,
54  void       *segment
55)
56{
57  register Region_Control *the_region;
58  Thread_Control          *the_thread;
59  Objects_Locations        location;
60  void                   **the_segment;
61#ifdef RTEMS_REGION_FREE_SHRED_PATTERN
62  uint32_t                 size;
63#endif
64  int                      status;
65
66  _RTEMS_Lock_allocator();
67  the_region = _Region_Get( id, &location );
68  switch ( location ) {
69
70    case OBJECTS_REMOTE:        /* this error cannot be returned */
71      _RTEMS_Unlock_allocator();
72      return RTEMS_INTERNAL_ERROR;
73
74    case OBJECTS_ERROR:
75      _RTEMS_Unlock_allocator();
76      return RTEMS_INVALID_ID;
77
78    case OBJECTS_LOCAL:
79
80      _Region_Debug_Walk( the_region, 3 );
81
82#ifdef RTEMS_REGION_FREE_SHRED_PATTERN
83      if ( _Heap_Size_of_user_area( &the_region->Memory, segment, &size ) ) {
84        memset(segment, (RTEMS_REGION_FREE_SHRED_PATTERN & 0xFF), size);
85      } else {
86        _RTEMS_Unlock_allocator();
87        return RTEMS_INVALID_ADDRESS;
88      }
89#endif
90
91      status = _Region_Free_segment( the_region, segment );
92
93      _Region_Debug_Walk( the_region, 4 );
94
95      if ( !status ) {
96        _RTEMS_Unlock_allocator();
97        return RTEMS_INVALID_ADDRESS;
98      }
99
100      the_region->number_of_used_blocks -= 1;
101
102      /*
103       *  Switch from using the memory allocation mutex to using a
104       *  dispatching disabled critical section.  We have to do this
105       *  because this thread may unblock one or more threads that were
106       *  waiting on memory.
107       *
108       *  NOTE: The following loop is O(n) where n is the number of
109       *        threads whose memory request is satisfied.
110       */
111      _RTEMS_Unlock_allocator();
112      _Thread_Disable_dispatch();
113
114      for ( ; ; ) {
115        the_thread = _Thread_queue_First( &the_region->Wait_queue );
116
117        if ( the_thread == NULL )
118           break;
119
120        the_segment = (void **) _Region_Allocate_segment(
121           the_region,
122           the_thread->Wait.count
123        );
124
125        if ( the_segment == NULL )
126           break;
127
128        *(void **)the_thread->Wait.return_argument = the_segment;
129        the_region->number_of_used_blocks += 1;
130        _Thread_queue_Extract( &the_region->Wait_queue, the_thread );
131        the_thread->Wait.return_code = RTEMS_SUCCESSFUL;
132      }
133      _Thread_Enable_dispatch();
134
135      return RTEMS_SUCCESSFUL;
136  }
137
138  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
139}
Note: See TracBrowser for help on using the repository browser.