1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Initialize a Message Queue |
---|
5 | * @ingroup ScoreMessageQueue |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2009. |
---|
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.com/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/system.h> |
---|
22 | #include <rtems/score/chain.h> |
---|
23 | #include <rtems/score/isr.h> |
---|
24 | #include <rtems/score/object.h> |
---|
25 | #include <rtems/score/coremsg.h> |
---|
26 | #include <rtems/score/states.h> |
---|
27 | #include <rtems/score/thread.h> |
---|
28 | #include <rtems/score/wkspace.h> |
---|
29 | |
---|
30 | /* |
---|
31 | * size_t_mult32_with_overflow |
---|
32 | * |
---|
33 | * This method multiplies two size_t 32-bit numbers and checks |
---|
34 | * for overflow. It returns false if an overflow occurred and |
---|
35 | * the result is bad. |
---|
36 | */ |
---|
37 | static inline bool size_t_mult32_with_overflow( |
---|
38 | size_t a, |
---|
39 | size_t b, |
---|
40 | size_t *c |
---|
41 | ) |
---|
42 | { |
---|
43 | long long x = (long long)a*b; |
---|
44 | |
---|
45 | if ( x > SIZE_MAX ) |
---|
46 | return false; |
---|
47 | *c = (size_t) x; |
---|
48 | return true; |
---|
49 | } |
---|
50 | |
---|
51 | bool _CORE_message_queue_Initialize( |
---|
52 | CORE_message_queue_Control *the_message_queue, |
---|
53 | CORE_message_queue_Attributes *the_message_queue_attributes, |
---|
54 | uint32_t maximum_pending_messages, |
---|
55 | size_t maximum_message_size |
---|
56 | ) |
---|
57 | { |
---|
58 | size_t message_buffering_required = 0; |
---|
59 | size_t allocated_message_size; |
---|
60 | |
---|
61 | the_message_queue->maximum_pending_messages = maximum_pending_messages; |
---|
62 | the_message_queue->number_of_pending_messages = 0; |
---|
63 | the_message_queue->maximum_message_size = maximum_message_size; |
---|
64 | _CORE_message_queue_Set_notify( the_message_queue, NULL, NULL ); |
---|
65 | |
---|
66 | allocated_message_size = maximum_message_size; |
---|
67 | |
---|
68 | /* |
---|
69 | * Check if allocated_message_size is aligned to uintptr-size boundary. |
---|
70 | * If not, it will increase allocated_message_size to multiplicity of pointer |
---|
71 | * size. |
---|
72 | */ |
---|
73 | if (allocated_message_size & (sizeof(uintptr_t) - 1)) { |
---|
74 | allocated_message_size += sizeof(uintptr_t); |
---|
75 | allocated_message_size &= ~(sizeof(uintptr_t) - 1); |
---|
76 | } |
---|
77 | |
---|
78 | /* |
---|
79 | * Check for an overflow. It can occur while increasing allocated_message_size |
---|
80 | * to multiplicity of uintptr_t above. |
---|
81 | */ |
---|
82 | if (allocated_message_size < maximum_message_size) |
---|
83 | return false; |
---|
84 | |
---|
85 | /* |
---|
86 | * Calculate how much total memory is required for message buffering and |
---|
87 | * check for overflow on the multiplication. |
---|
88 | */ |
---|
89 | if ( !size_t_mult32_with_overflow( |
---|
90 | (size_t) maximum_pending_messages, |
---|
91 | allocated_message_size + sizeof(CORE_message_queue_Buffer_control), |
---|
92 | &message_buffering_required ) ) |
---|
93 | return false; |
---|
94 | |
---|
95 | /* |
---|
96 | * Attempt to allocate the message memory |
---|
97 | */ |
---|
98 | the_message_queue->message_buffers = (CORE_message_queue_Buffer *) |
---|
99 | _Workspace_Allocate( message_buffering_required ); |
---|
100 | |
---|
101 | if (the_message_queue->message_buffers == 0) |
---|
102 | return false; |
---|
103 | |
---|
104 | /* |
---|
105 | * Initialize the pool of inactive messages, pending messages, |
---|
106 | * and set of waiting threads. |
---|
107 | */ |
---|
108 | _Chain_Initialize ( |
---|
109 | &the_message_queue->Inactive_messages, |
---|
110 | the_message_queue->message_buffers, |
---|
111 | (size_t) maximum_pending_messages, |
---|
112 | allocated_message_size + sizeof( CORE_message_queue_Buffer_control ) |
---|
113 | ); |
---|
114 | |
---|
115 | _Chain_Initialize_empty( &the_message_queue->Pending_messages ); |
---|
116 | |
---|
117 | _Thread_queue_Initialize( |
---|
118 | &the_message_queue->Wait_queue, |
---|
119 | _CORE_message_queue_Is_priority( the_message_queue_attributes ) ? |
---|
120 | THREAD_QUEUE_DISCIPLINE_PRIORITY : THREAD_QUEUE_DISCIPLINE_FIFO, |
---|
121 | STATES_WAITING_FOR_MESSAGE, |
---|
122 | CORE_MESSAGE_QUEUE_STATUS_TIMEOUT |
---|
123 | ); |
---|
124 | |
---|
125 | return true; |
---|
126 | } |
---|