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.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/score/coremsgimpl.h> |
---|
22 | #include <rtems/score/wkspace.h> |
---|
23 | |
---|
24 | /* |
---|
25 | * size_t_mult32_with_overflow |
---|
26 | * |
---|
27 | * This method multiplies two size_t 32-bit numbers and checks |
---|
28 | * for overflow. It returns false if an overflow occurred and |
---|
29 | * the result is bad. |
---|
30 | */ |
---|
31 | static inline bool size_t_mult32_with_overflow( |
---|
32 | size_t a, |
---|
33 | size_t b, |
---|
34 | size_t *c |
---|
35 | ) |
---|
36 | { |
---|
37 | long long x = (long long)a*b; |
---|
38 | |
---|
39 | if ( x > SIZE_MAX ) |
---|
40 | return false; |
---|
41 | *c = (size_t) x; |
---|
42 | return true; |
---|
43 | } |
---|
44 | |
---|
45 | bool _CORE_message_queue_Initialize( |
---|
46 | CORE_message_queue_Control *the_message_queue, |
---|
47 | CORE_message_queue_Disciplines discipline, |
---|
48 | uint32_t maximum_pending_messages, |
---|
49 | size_t maximum_message_size |
---|
50 | ) |
---|
51 | { |
---|
52 | size_t message_buffering_required = 0; |
---|
53 | size_t aligned_message_size; |
---|
54 | size_t align_mask; |
---|
55 | |
---|
56 | the_message_queue->maximum_pending_messages = maximum_pending_messages; |
---|
57 | the_message_queue->number_of_pending_messages = 0; |
---|
58 | the_message_queue->maximum_message_size = maximum_message_size; |
---|
59 | _CORE_message_queue_Set_notify( the_message_queue, NULL ); |
---|
60 | |
---|
61 | /* |
---|
62 | * Align up the maximum message size to be an integral multiple of the |
---|
63 | * pointer size. |
---|
64 | */ |
---|
65 | align_mask = sizeof(uintptr_t) - 1; |
---|
66 | aligned_message_size = ( maximum_message_size + align_mask ) & ~align_mask; |
---|
67 | |
---|
68 | /* |
---|
69 | * Check for an integer overflow. It can occur while aligning up the maximum |
---|
70 | * message size. |
---|
71 | */ |
---|
72 | if (aligned_message_size < maximum_message_size) |
---|
73 | return false; |
---|
74 | |
---|
75 | /* |
---|
76 | * Calculate how much total memory is required for message buffering and |
---|
77 | * check for overflow on the multiplication. |
---|
78 | */ |
---|
79 | if ( !size_t_mult32_with_overflow( |
---|
80 | (size_t) maximum_pending_messages, |
---|
81 | aligned_message_size + sizeof(CORE_message_queue_Buffer_control), |
---|
82 | &message_buffering_required ) ) |
---|
83 | return false; |
---|
84 | |
---|
85 | /* |
---|
86 | * Attempt to allocate the message memory |
---|
87 | */ |
---|
88 | the_message_queue->message_buffers = (CORE_message_queue_Buffer *) |
---|
89 | _Workspace_Allocate( message_buffering_required ); |
---|
90 | |
---|
91 | if (the_message_queue->message_buffers == 0) |
---|
92 | return false; |
---|
93 | |
---|
94 | /* |
---|
95 | * Initialize the pool of inactive messages, pending messages, |
---|
96 | * and set of waiting threads. |
---|
97 | */ |
---|
98 | _Chain_Initialize ( |
---|
99 | &the_message_queue->Inactive_messages, |
---|
100 | the_message_queue->message_buffers, |
---|
101 | (size_t) maximum_pending_messages, |
---|
102 | aligned_message_size + sizeof( CORE_message_queue_Buffer_control ) |
---|
103 | ); |
---|
104 | |
---|
105 | _Chain_Initialize_empty( &the_message_queue->Pending_messages ); |
---|
106 | |
---|
107 | _Thread_queue_Initialize( &the_message_queue->Wait_queue ); |
---|
108 | |
---|
109 | if ( discipline == CORE_MESSAGE_QUEUE_DISCIPLINES_PRIORITY ) { |
---|
110 | the_message_queue->operations = &_Thread_queue_Operations_priority; |
---|
111 | } else { |
---|
112 | the_message_queue->operations = &_Thread_queue_Operations_FIFO; |
---|
113 | } |
---|
114 | |
---|
115 | return true; |
---|
116 | } |
---|