1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Allocate Object |
---|
5 | * @ingroup ScoreObject |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-1999. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/score/objectimpl.h> |
---|
22 | #include <rtems/score/assert.h> |
---|
23 | #include <rtems/score/chainimpl.h> |
---|
24 | #include <rtems/score/sysstate.h> |
---|
25 | |
---|
26 | /* #define RTEMS_DEBUG_OBJECT_ALLOCATION */ |
---|
27 | |
---|
28 | #if defined(RTEMS_DEBUG_OBJECT_ALLOCATION) |
---|
29 | #include <rtems/bspIo.h> |
---|
30 | #endif |
---|
31 | |
---|
32 | static Objects_Control *_Objects_Get_inactive( |
---|
33 | Objects_Information *information |
---|
34 | ) |
---|
35 | { |
---|
36 | return (Objects_Control *) _Chain_Get_unprotected( &information->Inactive ); |
---|
37 | } |
---|
38 | |
---|
39 | Objects_Control *_Objects_Allocate_unprotected( |
---|
40 | Objects_Information *information |
---|
41 | ) |
---|
42 | { |
---|
43 | Objects_Control *the_object; |
---|
44 | |
---|
45 | _Assert( |
---|
46 | _Debug_Is_owner_of_allocator() |
---|
47 | || !_System_state_Is_up( _System_state_Get() ) |
---|
48 | ); |
---|
49 | |
---|
50 | /* |
---|
51 | * If the application is using the optional manager stubs and |
---|
52 | * still attempts to create the object, the information block |
---|
53 | * should be all zeroed out because it is in the BSS. So let's |
---|
54 | * check that code for this manager is even present. |
---|
55 | */ |
---|
56 | if ( information->size == 0 ) |
---|
57 | return NULL; |
---|
58 | |
---|
59 | /* |
---|
60 | * OK. The manager should be initialized and configured to have objects. |
---|
61 | * With any luck, it is safe to attempt to allocate an object. |
---|
62 | */ |
---|
63 | the_object = _Objects_Get_inactive( information ); |
---|
64 | |
---|
65 | if ( information->auto_extend ) { |
---|
66 | /* |
---|
67 | * If the list is empty then we are out of objects and need to |
---|
68 | * extend information base. |
---|
69 | */ |
---|
70 | |
---|
71 | if ( !the_object ) { |
---|
72 | _Objects_Extend_information( information ); |
---|
73 | the_object = _Objects_Get_inactive( information ); |
---|
74 | } |
---|
75 | |
---|
76 | if ( the_object ) { |
---|
77 | uint32_t block; |
---|
78 | |
---|
79 | block = (uint32_t) _Objects_Get_index( the_object->id ) - |
---|
80 | _Objects_Get_index( information->minimum_id ); |
---|
81 | block /= information->allocation_size; |
---|
82 | |
---|
83 | information->inactive_per_block[ block ]--; |
---|
84 | information->inactive--; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | #if defined(RTEMS_DEBUG_OBJECT_ALLOCATION) |
---|
89 | if ( !the_object ) { |
---|
90 | printk( |
---|
91 | "OBJECT ALLOCATION FAILURE! API/Class %d/%d\n", |
---|
92 | information->the_api, |
---|
93 | information->the_class |
---|
94 | ); |
---|
95 | } |
---|
96 | #endif |
---|
97 | |
---|
98 | return the_object; |
---|
99 | } |
---|
100 | |
---|
101 | Objects_Control *_Objects_Allocate( Objects_Information *information ) |
---|
102 | { |
---|
103 | _RTEMS_Lock_allocator(); |
---|
104 | |
---|
105 | return _Objects_Allocate_unprotected( information ); |
---|
106 | } |
---|