mq.c defines the functions which are used to initialize the message entry structure
and to put and get messages to and from the message queue.
The message queue is used hold messages sent to inet from other processes.
mq_init is defined in inet/mq.c.
The message entry structure and the message queue are defined in inet/mq.h.


typedef struct mq
{
message mq_mess;
struct mq *mq_next;
int mq_allocated;
} mq_t;


mq_t is the message entry structure.

1  /*
     2  inet/mq.c
       
     3  Created:        Jan 3, 1992 by Philip Homburg
       
     4  Copyright 1995 Philip Homburg
     5  */
       
     6  #include "inet.h"
     7  #include "mq.h"
     8  #include "generic/assert.h"
       
     9  THIS_FILE
       
    10  #define MQ_SIZE         128
       

mq_list is the message queue.
The message queue is both an array and a linked list when it is first initialized.

    11  PRIVATE mq_t mq_list[MQ_SIZE];

mq_freelist points to the head of the list of free (unused) message entry elements in the mq_list array.

    12  PRIVATE mq_t *mq_freelist;
       

After mq_init() is called the mq_next field of every member of the mq_list array points to the preceding
member in the array. Also mq_init() makes mq_freelist point to the end of the array. mq_freelist points
to the head of the list of free (unused) elements in the mq_list array.

    13  void mq_init()
    14  {
    15          int i;
       
    16          mq_freelist= NULL;
    17          for (i= 0; i<MQ_SIZE; i++)
    18          {
    19                  mq_list[i].mq_next= mq_freelist;
    20                  mq_freelist= &mq_list[i];
    21                  mq_list[i].mq_allocated= 0;
    22          }
    23  }
       

mq_get returns a message entry from the mq_list array and in the mq_freelist linked list. It's marked as
allocated by setting mq_allocated = 1 and removing it from the mq_freelist linked list.

    24  mq_t *mq_get()
    25  {
    26          mq_t *mq;
       
    27          mq= mq_freelist;
    28          assert(mq != NULL);
       
    29          mq_freelist= mq->mq_next;
    30          mq->mq_next= NULL;
    31          assert(mq->mq_allocated == 0);
    32          mq->mq_allocated= 1;
    33          return mq;
    34  }
       

A message entry is freed by calling mq_free(mq) where mq is the message item. The message entry is
marked as freed by setting mq_allocated = 0 and adding it back to the mq_freelist linked list.

    35  void mq_free(mq)
    36  mq_t *mq;
    37  {
    38          mq->mq_next= mq_freelist;
    39          mq_freelist= mq;
    40          assert(mq->mq_allocated == 1);
    41          mq->mq_allocated= 0;
    42  }
       
    43  /*
    44   * $PchId: mq.c,v 1.6 1996/05/07 21:10:16 philip Exp $
    45   */