1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup RTEMSScoreMessageQueue |
---|
5 | * |
---|
6 | * @brief This source file contains the implementation of |
---|
7 | * _CORE_message_queue_Initialize(). |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * COPYRIGHT (c) 1989-2009. |
---|
12 | * On-Line Applications Research Corporation (OAR). |
---|
13 | * |
---|
14 | * The license and distribution terms for this file may be |
---|
15 | * found in the file LICENSE in this distribution or at |
---|
16 | * http://www.rtems.org/license/LICENSE. |
---|
17 | */ |
---|
18 | |
---|
19 | #ifdef HAVE_CONFIG_H |
---|
20 | #include "config.h" |
---|
21 | #endif |
---|
22 | |
---|
23 | #include <rtems/score/coremsgimpl.h> |
---|
24 | #include <rtems/score/assert.h> |
---|
25 | |
---|
26 | #define MESSAGE_SIZE_LIMIT \ |
---|
27 | ( SIZE_MAX - sizeof( uintptr_t ) + 1 - sizeof( CORE_message_queue_Buffer ) ) |
---|
28 | |
---|
29 | RTEMS_STATIC_ASSERT( |
---|
30 | offsetof( CORE_message_queue_Buffer, buffer ) |
---|
31 | == sizeof( CORE_message_queue_Buffer ), |
---|
32 | CORE_MESSAGE_QUEUE_BUFFER_OFFSET |
---|
33 | ); |
---|
34 | |
---|
35 | Status_Control _CORE_message_queue_Initialize( |
---|
36 | CORE_message_queue_Control *the_message_queue, |
---|
37 | CORE_message_queue_Disciplines discipline, |
---|
38 | uint32_t maximum_pending_messages, |
---|
39 | size_t maximum_message_size, |
---|
40 | CORE_message_queue_Allocate_buffers allocate_buffers, |
---|
41 | const void *arg |
---|
42 | ) |
---|
43 | { |
---|
44 | size_t buffer_size; |
---|
45 | |
---|
46 | /* Make sure the message size computation does not overflow */ |
---|
47 | if ( maximum_message_size > MESSAGE_SIZE_LIMIT ) { |
---|
48 | return STATUS_MESSAGE_QUEUE_INVALID_SIZE; |
---|
49 | } |
---|
50 | |
---|
51 | buffer_size = RTEMS_ALIGN_UP( maximum_message_size, sizeof( uintptr_t ) ); |
---|
52 | _Assert( buffer_size >= maximum_message_size ); |
---|
53 | |
---|
54 | buffer_size += sizeof( CORE_message_queue_Buffer ); |
---|
55 | _Assert( buffer_size >= sizeof( CORE_message_queue_Buffer ) ); |
---|
56 | |
---|
57 | /* Make sure the memory allocation size computation does not overflow */ |
---|
58 | if ( maximum_pending_messages > SIZE_MAX / buffer_size ) { |
---|
59 | return STATUS_MESSAGE_QUEUE_INVALID_NUMBER; |
---|
60 | } |
---|
61 | |
---|
62 | the_message_queue->message_buffers = ( *allocate_buffers )( |
---|
63 | the_message_queue, |
---|
64 | (size_t) maximum_pending_messages * buffer_size, |
---|
65 | arg |
---|
66 | ); |
---|
67 | |
---|
68 | if ( the_message_queue->message_buffers == NULL ) { |
---|
69 | return STATUS_MESSAGE_QUEUE_NO_MEMORY; |
---|
70 | } |
---|
71 | |
---|
72 | the_message_queue->maximum_pending_messages = maximum_pending_messages; |
---|
73 | the_message_queue->number_of_pending_messages = 0; |
---|
74 | the_message_queue->maximum_message_size = maximum_message_size; |
---|
75 | |
---|
76 | _CORE_message_queue_Set_notify( the_message_queue, NULL ); |
---|
77 | _Chain_Initialize_empty( &the_message_queue->Pending_messages ); |
---|
78 | _Thread_queue_Object_initialize( &the_message_queue->Wait_queue ); |
---|
79 | |
---|
80 | if ( discipline == CORE_MESSAGE_QUEUE_DISCIPLINES_PRIORITY ) { |
---|
81 | the_message_queue->operations = &_Thread_queue_Operations_priority; |
---|
82 | } else { |
---|
83 | the_message_queue->operations = &_Thread_queue_Operations_FIFO; |
---|
84 | } |
---|
85 | |
---|
86 | _Chain_Initialize ( |
---|
87 | &the_message_queue->Inactive_messages, |
---|
88 | the_message_queue->message_buffers, |
---|
89 | (size_t) maximum_pending_messages, |
---|
90 | buffer_size |
---|
91 | ); |
---|
92 | |
---|
93 | return STATUS_SUCCESSFUL; |
---|
94 | } |
---|