source: rtems/cpukit/score/src/coremsg.c @ e18303d9

Last change on this file since e18303d9 was e18303d9, checked in by Joel Sherrill <joel.sherrill@…>, on Jul 14, 2004 at 9:05:38 PM

2004-07-14 Joel Sherrill <joel@…>

PR 650/rtems

  • src/coremsg.c: Check for mathemathical overflow when calculating amount of memory to allocate for message buffers.
  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*
2 *  CORE Message Queue Handler
3 *
4 *  DESCRIPTION:
5 *
6 *  This package is the implementation of the CORE Message Queue Handler.
7 *  This core object provides task synchronization and communication functions
8 *  via messages passed to queue objects.
9 *
10 *  COPYRIGHT (c) 1989-1999.
11 *  On-Line Applications Research Corporation (OAR).
12 *
13 *  The license and distribution terms for this file may be
14 *  found in the file LICENSE in this distribution or at
15 *  http://www.rtems.com/license/LICENSE.
16 *
17 *  $Id$
18 */
19
20#include <rtems/system.h>
21#include <rtems/score/chain.h>
22#include <rtems/score/isr.h>
23#include <rtems/score/object.h>
24#include <rtems/score/coremsg.h>
25#include <rtems/score/states.h>
26#include <rtems/score/thread.h>
27#include <rtems/score/wkspace.h>
28#if defined(RTEMS_MULTIPROCESSING)
29#include <rtems/score/mpci.h>
30#endif
31
32/*PAGE
33 *
34 *  _CORE_message_queue_Initialize
35 *
36 *  This routine initializes a newly created message queue based on the
37 *  specified data.
38 *
39 *  Input parameters:
40 *    the_message_queue            - the message queue to initialize
41 *    the_class                    - the API specific object class
42 *    the_message_queue_attributes - the message queue's attributes
43 *    maximum_pending_messages     - maximum message and reserved buffer count
44 *    maximum_message_size         - maximum size of each message
45 *
46 *  Output parameters:
47 *    TRUE   - if the message queue is initialized
48 *    FALSE  - if the message queue is NOT initialized
49 */
50
51boolean _CORE_message_queue_Initialize(
52  CORE_message_queue_Control    *the_message_queue,
53  CORE_message_queue_Attributes *the_message_queue_attributes,
54  unsigned32                     maximum_pending_messages,
55  unsigned32                     maximum_message_size
56)
57{
58  unsigned32 message_buffering_required;
59  unsigned32 allocated_message_size;
60
61  the_message_queue->maximum_pending_messages   = maximum_pending_messages;
62  the_message_queue->number_of_pending_messages = 0;
63  the_message_queue->maximum_message_size       = maximum_message_size;
64  _CORE_message_queue_Set_notify( the_message_queue, NULL, NULL );
65 
66  /*
67   *  Round size up to multiple of a pointer for chain init and
68   *  check for overflow on adding overhead to each message.
69   */
70 
71  allocated_message_size = maximum_message_size;
72  if (allocated_message_size & (sizeof(unsigned32) - 1)) {
73      allocated_message_size += sizeof(unsigned32);
74      allocated_message_size &= ~(sizeof(unsigned32) - 1);
75  }
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  message_buffering_required = maximum_pending_messages *
85       (allocated_message_size + sizeof(CORE_message_queue_Buffer_control));
86 
87  if (message_buffering_required < allocated_message_size)
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    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.