RTEMS
coremsg.c
Go to the documentation of this file.
1 
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 
22 #include <rtems/score/assert.h>
23 
24 #define MESSAGE_SIZE_LIMIT \
25  ( SIZE_MAX - sizeof( uintptr_t ) + 1 - sizeof( CORE_message_queue_Buffer ) )
26 
28  offsetof( CORE_message_queue_Buffer, buffer )
29  == sizeof( CORE_message_queue_Buffer ),
30  CORE_MESSAGE_QUEUE_BUFFER_OFFSET
31 );
32 
34  CORE_message_queue_Control *the_message_queue,
36  uint32_t maximum_pending_messages,
37  size_t maximum_message_size,
38  CORE_message_queue_Allocate_buffers allocate_buffers,
39  const void *arg
40 )
41 {
42  size_t buffer_size;
43 
44  /* Make sure the message size computation does not overflow */
45  if ( maximum_message_size > MESSAGE_SIZE_LIMIT ) {
46  return STATUS_MESSAGE_QUEUE_INVALID_SIZE;
47  }
48 
49  buffer_size = RTEMS_ALIGN_UP( maximum_message_size, sizeof( uintptr_t ) );
50  _Assert( buffer_size >= maximum_message_size );
51 
52  buffer_size += sizeof( CORE_message_queue_Buffer );
53  _Assert( buffer_size >= sizeof( CORE_message_queue_Buffer ) );
54 
55  /* Make sure the memory allocation size computation does not overflow */
56  if ( maximum_pending_messages > SIZE_MAX / buffer_size ) {
57  return STATUS_MESSAGE_QUEUE_INVALID_NUMBER;
58  }
59 
60  the_message_queue->message_buffers = ( *allocate_buffers )(
61  the_message_queue,
62  (size_t) maximum_pending_messages * buffer_size,
63  arg
64  );
65 
66  if ( the_message_queue->message_buffers == NULL ) {
67  return STATUS_MESSAGE_QUEUE_NO_MEMORY;
68  }
69 
70  the_message_queue->maximum_pending_messages = maximum_pending_messages;
71  the_message_queue->number_of_pending_messages = 0;
72  the_message_queue->maximum_message_size = maximum_message_size;
73 
74  _CORE_message_queue_Set_notify( the_message_queue, NULL );
75  _Chain_Initialize_empty( &the_message_queue->Pending_messages );
76  _Thread_queue_Object_initialize( &the_message_queue->Wait_queue );
77 
78  if ( discipline == CORE_MESSAGE_QUEUE_DISCIPLINES_PRIORITY ) {
79  the_message_queue->operations = &_Thread_queue_Operations_priority;
80  } else {
81  the_message_queue->operations = &_Thread_queue_Operations_FIFO;
82  }
83 
85  &the_message_queue->Inactive_messages,
86  the_message_queue->message_buffers,
87  (size_t) maximum_pending_messages,
88  buffer_size
89  );
90 
91  return STATUS_SUCCESSFUL;
92 }
Inlined Routines in the Core Message Handler.
void *(* CORE_message_queue_Allocate_buffers)(CORE_message_queue_Control *the_message_queue, size_t size, const void *arg)
This handler shall allocate the message buffer storage area for a message queue.
Definition: coremsgimpl.h:89
#define _CORE_message_queue_Set_notify(the_message_queue, the_handler)
Initializes notification information.
Definition: coremsgimpl.h:593
void _Chain_Initialize(Chain_Control *the_chain, void *starting_address, size_t number_nodes, size_t node_size)
Initializes a chain header.
Definition: chain.c:26
void _Thread_queue_Object_initialize(Thread_queue_Control *the_thread_queue)
Initializes a thread queue embedded in an object with identifier.
Definition: threadq.c:148
Information for the Assert Handler.
#define RTEMS_STATIC_ASSERT(_cond, _msg)
Asserts at compile time that the specified condition is satisfied.
Definition: basedefs.h:838
The structure is used to organize message buffers of a message queue.
Definition: coremsgbuffer.h:63
#define RTEMS_ALIGN_UP(_value, _alignment)
Returns the specified value aligned up to the specified alignment.
Definition: basedefs.h:375
CORE_message_queue_Disciplines
The possible blocking disciplines for a message queue.
Definition: coremsg.h:69
Status_Control _CORE_message_queue_Initialize(CORE_message_queue_Control *the_message_queue, CORE_message_queue_Disciplines discipline, uint32_t maximum_pending_messages, size_t maximum_message_size, CORE_message_queue_Allocate_buffers allocate_buffers, const void *arg)
Initializes a message queue.
Definition: coremsg.c:33
static __inline__ void _Chain_Initialize_empty(Chain_Control *the_chain)
Initializes this chain as empty.
Definition: chainimpl.h:505
Control block used to manage each message queue.
Definition: coremsg.h:96
#define _Assert(_e)
Assertion similar to assert() controlled via RTEMS_DEBUG instead of NDEBUG.
Definition: assert.h:100
CORE_message_queue_Buffer * message_buffers
Definition: coremsg.h:126