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 | |
---|
52 | rtems_status_code rtems_region_return_segment( |
---|
53 | Objects_Id id, |
---|
54 | void *segment |
---|
55 | ) |
---|
56 | { |
---|
57 | register Region_Control *the_region; |
---|
58 | Objects_Locations location; |
---|
59 | #ifdef RTEMS_REGION_FREE_SHRED_PATTERN |
---|
60 | uint32_t size; |
---|
61 | #endif |
---|
62 | int status; |
---|
63 | |
---|
64 | _RTEMS_Lock_allocator(); |
---|
65 | the_region = _Region_Get( id, &location ); |
---|
66 | switch ( location ) { |
---|
67 | |
---|
68 | case OBJECTS_REMOTE: /* this error cannot be returned */ |
---|
69 | _RTEMS_Unlock_allocator(); |
---|
70 | return RTEMS_INTERNAL_ERROR; |
---|
71 | |
---|
72 | case OBJECTS_ERROR: |
---|
73 | _RTEMS_Unlock_allocator(); |
---|
74 | return RTEMS_INVALID_ID; |
---|
75 | |
---|
76 | case OBJECTS_LOCAL: |
---|
77 | |
---|
78 | _Region_Debug_Walk( the_region, 3 ); |
---|
79 | |
---|
80 | #ifdef RTEMS_REGION_FREE_SHRED_PATTERN |
---|
81 | if ( _Heap_Size_of_user_area( &the_region->Memory, segment, &size ) ) { |
---|
82 | memset( segment, (RTEMS_REGION_FREE_SHRED_PATTERN & 0xFF), size ); |
---|
83 | } else { |
---|
84 | _RTEMS_Unlock_allocator(); |
---|
85 | return RTEMS_INVALID_ADDRESS; |
---|
86 | } |
---|
87 | #endif |
---|
88 | |
---|
89 | status = _Region_Free_segment( the_region, segment ); |
---|
90 | |
---|
91 | _Region_Debug_Walk( the_region, 4 ); |
---|
92 | |
---|
93 | if ( !status ) { |
---|
94 | _RTEMS_Unlock_allocator(); |
---|
95 | return RTEMS_INVALID_ADDRESS; |
---|
96 | } |
---|
97 | |
---|
98 | the_region->number_of_used_blocks -= 1; |
---|
99 | |
---|
100 | _Region_Process_queue(the_region); /* unlocks allocator internally */ |
---|
101 | |
---|
102 | return RTEMS_SUCCESSFUL; |
---|
103 | } |
---|
104 | |
---|
105 | return RTEMS_INTERNAL_ERROR; /* unreached - only to remove warnings */ |
---|
106 | } |
---|