[ab02824] | 1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
| 2 | |
---|
[355ee7d] | 3 | /** |
---|
[9278f3d] | 4 | * @file |
---|
[3652ad35] | 5 | * |
---|
[9278f3d] | 6 | * @ingroup RTEMSScoreMessageQueue |
---|
| 7 | * |
---|
| 8 | * @brief This source file contains the implementation of |
---|
| 9 | * _CORE_message_queue_Initialize(). |
---|
[355ee7d] | 10 | */ |
---|
| 11 | |
---|
| 12 | /* |
---|
[939ba81] | 13 | * COPYRIGHT (c) 1989-2009. |
---|
[3652ad35] | 14 | * On-Line Applications Research Corporation (OAR). |
---|
| 15 | * |
---|
[ab02824] | 16 | * Redistribution and use in source and binary forms, with or without |
---|
| 17 | * modification, are permitted provided that the following conditions |
---|
| 18 | * are met: |
---|
| 19 | * 1. Redistributions of source code must retain the above copyright |
---|
| 20 | * notice, this list of conditions and the following disclaimer. |
---|
| 21 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
| 22 | * notice, this list of conditions and the following disclaimer in the |
---|
| 23 | * documentation and/or other materials provided with the distribution. |
---|
| 24 | * |
---|
| 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
| 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
| 29 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
| 35 | * POSSIBILITY OF SUCH DAMAGE. |
---|
[3652ad35] | 36 | */ |
---|
| 37 | |
---|
[80cf60e] | 38 | #ifdef HAVE_CONFIG_H |
---|
[a8eed23] | 39 | #include "config.h" |
---|
| 40 | #endif |
---|
| 41 | |
---|
[b5d514f] | 42 | #include <rtems/score/coremsgimpl.h> |
---|
[fe2cb7c4] | 43 | #include <rtems/score/assert.h> |
---|
[3652ad35] | 44 | |
---|
[fe2cb7c4] | 45 | #define MESSAGE_SIZE_LIMIT \ |
---|
[69b4fe59] | 46 | ( SIZE_MAX - sizeof( uintptr_t ) + 1 - sizeof( CORE_message_queue_Buffer ) ) |
---|
| 47 | |
---|
| 48 | RTEMS_STATIC_ASSERT( |
---|
| 49 | offsetof( CORE_message_queue_Buffer, buffer ) |
---|
| 50 | == sizeof( CORE_message_queue_Buffer ), |
---|
| 51 | CORE_MESSAGE_QUEUE_BUFFER_OFFSET |
---|
| 52 | ); |
---|
[d90fef2] | 53 | |
---|
[5bc7c37] | 54 | Status_Control _CORE_message_queue_Initialize( |
---|
[4a4f41e] | 55 | CORE_message_queue_Control *the_message_queue, |
---|
| 56 | CORE_message_queue_Disciplines discipline, |
---|
| 57 | uint32_t maximum_pending_messages, |
---|
| 58 | size_t maximum_message_size, |
---|
| 59 | CORE_message_queue_Allocate_buffers allocate_buffers, |
---|
| 60 | const void *arg |
---|
[3652ad35] | 61 | ) |
---|
| 62 | { |
---|
[fe2cb7c4] | 63 | size_t buffer_size; |
---|
[3652ad35] | 64 | |
---|
[fe2cb7c4] | 65 | /* Make sure the message size computation does not overflow */ |
---|
| 66 | if ( maximum_message_size > MESSAGE_SIZE_LIMIT ) { |
---|
[5bc7c37] | 67 | return STATUS_MESSAGE_QUEUE_INVALID_SIZE; |
---|
[fe2cb7c4] | 68 | } |
---|
[05279b84] | 69 | |
---|
[fe2cb7c4] | 70 | buffer_size = RTEMS_ALIGN_UP( maximum_message_size, sizeof( uintptr_t ) ); |
---|
| 71 | _Assert( buffer_size >= maximum_message_size ); |
---|
| 72 | |
---|
[69b4fe59] | 73 | buffer_size += sizeof( CORE_message_queue_Buffer ); |
---|
| 74 | _Assert( buffer_size >= sizeof( CORE_message_queue_Buffer ) ); |
---|
[fe2cb7c4] | 75 | |
---|
| 76 | /* Make sure the memory allocation size computation does not overflow */ |
---|
| 77 | if ( maximum_pending_messages > SIZE_MAX / buffer_size ) { |
---|
[5bc7c37] | 78 | return STATUS_MESSAGE_QUEUE_INVALID_NUMBER; |
---|
[fe2cb7c4] | 79 | } |
---|
[05279b84] | 80 | |
---|
[4a4f41e] | 81 | the_message_queue->message_buffers = ( *allocate_buffers )( |
---|
| 82 | the_message_queue, |
---|
| 83 | (size_t) maximum_pending_messages * buffer_size, |
---|
| 84 | arg |
---|
[fe2cb7c4] | 85 | ); |
---|
[28352fae] | 86 | |
---|
[fe2cb7c4] | 87 | if ( the_message_queue->message_buffers == NULL ) { |
---|
[5bc7c37] | 88 | return STATUS_MESSAGE_QUEUE_NO_MEMORY; |
---|
[fe2cb7c4] | 89 | } |
---|
[28352fae] | 90 | |
---|
[34dd90a5] | 91 | the_message_queue->maximum_pending_messages = maximum_pending_messages; |
---|
| 92 | the_message_queue->number_of_pending_messages = 0; |
---|
| 93 | the_message_queue->maximum_message_size = maximum_message_size; |
---|
[28352fae] | 94 | |
---|
[34dd90a5] | 95 | _CORE_message_queue_Set_notify( the_message_queue, NULL ); |
---|
[3652ad35] | 96 | _Chain_Initialize_empty( &the_message_queue->Pending_messages ); |
---|
[e366f77] | 97 | _Thread_queue_Object_initialize( &the_message_queue->Wait_queue ); |
---|
[1e1a91ed] | 98 | |
---|
| 99 | if ( discipline == CORE_MESSAGE_QUEUE_DISCIPLINES_PRIORITY ) { |
---|
| 100 | the_message_queue->operations = &_Thread_queue_Operations_priority; |
---|
| 101 | } else { |
---|
| 102 | the_message_queue->operations = &_Thread_queue_Operations_FIFO; |
---|
| 103 | } |
---|
[3652ad35] | 104 | |
---|
[34dd90a5] | 105 | _Chain_Initialize ( |
---|
| 106 | &the_message_queue->Inactive_messages, |
---|
| 107 | the_message_queue->message_buffers, |
---|
| 108 | (size_t) maximum_pending_messages, |
---|
| 109 | buffer_size |
---|
| 110 | ); |
---|
| 111 | |
---|
[5bc7c37] | 112 | return STATUS_SUCCESSFUL; |
---|
[3652ad35] | 113 | } |
---|