source: rtems/cpukit/score/src/coremsg.c @ 2369b10

4.115
Last change on this file since 2369b10 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 3.3 KB
Line 
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/statesimpl.h>
23#include <rtems/score/wkspace.h>
24
25/*
26 *  size_t_mult32_with_overflow
27 *
28 *  This method multiplies two size_t 32-bit numbers and checks
29 *  for overflow.  It returns false if an overflow occurred and
30 *  the result is bad.
31 */
32static inline bool size_t_mult32_with_overflow(
33  size_t  a,
34  size_t  b,
35  size_t *c
36)
37{
38  long long x = (long long)a*b;
39
40  if ( x > SIZE_MAX )
41    return false;
42  *c = (size_t) x;
43  return true;
44}
45
46bool _CORE_message_queue_Initialize(
47  CORE_message_queue_Control    *the_message_queue,
48  CORE_message_queue_Attributes *the_message_queue_attributes,
49  uint32_t                       maximum_pending_messages,
50  size_t                         maximum_message_size
51)
52{
53  size_t message_buffering_required = 0;
54  size_t allocated_message_size;
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, NULL );
60
61  allocated_message_size = maximum_message_size;
62
63  /*
64   * Check if allocated_message_size is aligned to uintptr-size boundary.
65   * If not, it will increase allocated_message_size to multiplicity of pointer
66   * size.
67   */
68  if (allocated_message_size & (sizeof(uintptr_t) - 1)) {
69    allocated_message_size += sizeof(uintptr_t);
70    allocated_message_size &= ~(sizeof(uintptr_t) - 1);
71  }
72
73  /*
74   * Check for an overflow. It can occur while increasing allocated_message_size
75   * to multiplicity of uintptr_t above.
76   */
77  if (allocated_message_size < maximum_message_size)
78    return false;
79
80  /*
81   *  Calculate how much total memory is required for message buffering and
82   *  check for overflow on the multiplication.
83   */
84  if ( !size_t_mult32_with_overflow(
85        (size_t) maximum_pending_messages,
86        allocated_message_size + sizeof(CORE_message_queue_Buffer_control),
87        &message_buffering_required ) )
88    return false;
89
90  /*
91   *  Attempt to allocate the message memory
92   */
93  the_message_queue->message_buffers = (CORE_message_queue_Buffer *)
94     _Workspace_Allocate( message_buffering_required );
95
96  if (the_message_queue->message_buffers == 0)
97    return false;
98
99  /*
100   *  Initialize the pool of inactive messages, pending messages,
101   *  and set of waiting threads.
102   */
103  _Chain_Initialize (
104    &the_message_queue->Inactive_messages,
105    the_message_queue->message_buffers,
106    (size_t) maximum_pending_messages,
107    allocated_message_size + sizeof( CORE_message_queue_Buffer_control )
108  );
109
110  _Chain_Initialize_empty( &the_message_queue->Pending_messages );
111
112  _Thread_queue_Initialize(
113    &the_message_queue->Wait_queue,
114    _CORE_message_queue_Is_priority( the_message_queue_attributes ) ?
115       THREAD_QUEUE_DISCIPLINE_PRIORITY : THREAD_QUEUE_DISCIPLINE_FIFO,
116    STATES_WAITING_FOR_MESSAGE,
117    CORE_MESSAGE_QUEUE_STATUS_TIMEOUT
118  );
119
120  return true;
121}
Note: See TracBrowser for help on using the repository browser.