1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Initialize a Message Queue |
---|
5 | * @ingroup RTEMSScoreMessageQueue |
---|
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 | #ifdef HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/score/coremsgimpl.h> |
---|
22 | #include <rtems/score/assert.h> |
---|
23 | #include <rtems/score/wkspace.h> |
---|
24 | |
---|
25 | #define MESSAGE_SIZE_LIMIT \ |
---|
26 | ( SIZE_MAX - sizeof( uintptr_t ) + 1 - sizeof( CORE_message_queue_Buffer ) ) |
---|
27 | |
---|
28 | RTEMS_STATIC_ASSERT( |
---|
29 | offsetof( CORE_message_queue_Buffer, buffer ) |
---|
30 | == sizeof( CORE_message_queue_Buffer ), |
---|
31 | CORE_MESSAGE_QUEUE_BUFFER_OFFSET |
---|
32 | ); |
---|
33 | |
---|
34 | Status_Control _CORE_message_queue_Initialize( |
---|
35 | CORE_message_queue_Control *the_message_queue, |
---|
36 | CORE_message_queue_Disciplines discipline, |
---|
37 | uint32_t maximum_pending_messages, |
---|
38 | size_t maximum_message_size |
---|
39 | ) |
---|
40 | { |
---|
41 | size_t buffer_size; |
---|
42 | |
---|
43 | /* Make sure the message size computation does not overflow */ |
---|
44 | if ( maximum_message_size > MESSAGE_SIZE_LIMIT ) { |
---|
45 | return STATUS_MESSAGE_QUEUE_INVALID_SIZE; |
---|
46 | } |
---|
47 | |
---|
48 | buffer_size = RTEMS_ALIGN_UP( maximum_message_size, sizeof( uintptr_t ) ); |
---|
49 | _Assert( buffer_size >= maximum_message_size ); |
---|
50 | |
---|
51 | buffer_size += sizeof( CORE_message_queue_Buffer ); |
---|
52 | _Assert( buffer_size >= sizeof( CORE_message_queue_Buffer ) ); |
---|
53 | |
---|
54 | /* Make sure the memory allocation size computation does not overflow */ |
---|
55 | if ( maximum_pending_messages > SIZE_MAX / buffer_size ) { |
---|
56 | return STATUS_MESSAGE_QUEUE_INVALID_NUMBER; |
---|
57 | } |
---|
58 | |
---|
59 | the_message_queue->message_buffers = _Workspace_Allocate( |
---|
60 | (size_t) maximum_pending_messages * buffer_size |
---|
61 | ); |
---|
62 | |
---|
63 | if ( the_message_queue->message_buffers == NULL ) { |
---|
64 | return STATUS_MESSAGE_QUEUE_NO_MEMORY; |
---|
65 | } |
---|
66 | |
---|
67 | the_message_queue->maximum_pending_messages = maximum_pending_messages; |
---|
68 | the_message_queue->number_of_pending_messages = 0; |
---|
69 | the_message_queue->maximum_message_size = maximum_message_size; |
---|
70 | |
---|
71 | _CORE_message_queue_Set_notify( the_message_queue, NULL ); |
---|
72 | _Chain_Initialize_empty( &the_message_queue->Pending_messages ); |
---|
73 | _Thread_queue_Object_initialize( &the_message_queue->Wait_queue ); |
---|
74 | |
---|
75 | if ( discipline == CORE_MESSAGE_QUEUE_DISCIPLINES_PRIORITY ) { |
---|
76 | the_message_queue->operations = &_Thread_queue_Operations_priority; |
---|
77 | } else { |
---|
78 | the_message_queue->operations = &_Thread_queue_Operations_FIFO; |
---|
79 | } |
---|
80 | |
---|
81 | _Chain_Initialize ( |
---|
82 | &the_message_queue->Inactive_messages, |
---|
83 | the_message_queue->message_buffers, |
---|
84 | (size_t) maximum_pending_messages, |
---|
85 | buffer_size |
---|
86 | ); |
---|
87 | |
---|
88 | return STATUS_SUCCESSFUL; |
---|
89 | } |
---|