403253d96db4c1d87e9b51850cd9f679fd175e7e
[lcr.git] / message.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Route                                                          **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** message handling                                                          **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13
14 MESSAGES
15
16 struct lcr_msg *message_first = NULL;
17 struct lcr_msg **messagepointer_end = &message_first;
18
19 /* creates a new message with the given attributes. the message must be filled then. after filling, the message_put must be called */
20 struct lcr_msg *message_create(int id_from, int id_to, int flow, int type)
21 {
22         struct lcr_msg *message;
23
24         message = (struct lcr_msg *)MALLOC(sizeof(struct lcr_msg));
25         if (!message)
26                 FATAL("No memory for message.\n");
27         mmemuse++;
28
29         message->id_from = id_from;
30         message->id_to = id_to;
31         message->flow = flow;
32         message->type = type;
33
34         return(message);
35 }
36
37 /* attaches a message to the end of the message chain */
38 void message_put(struct lcr_msg *message)
39 {
40         if (message->id_to == 0) {
41                 PDEBUG(DEBUG_MSG, "message %s not written, because destination is 0.\n", messages_txt[message->type]);
42                 message_free(message);
43                 return;
44         }
45         
46         if ((options.deb&DEBUG_MSG) && message->type != MESSAGE_DATA)
47                 PDEBUG(DEBUG_MSG, "message %s written from %ld to %ld (memory %x)\n", messages_txt[message->type], message->id_from, message->id_to, message);
48
49         *messagepointer_end = message;
50         messagepointer_end = &(message->next);
51         /* Nullify next pointer if recycled messages */
52         *messagepointer_end=NULL;
53 }
54
55 struct lcr_msg *message_forward(int id_from, int id_to, int flow, union parameter *param)
56 {
57         struct lcr_msg *message;
58
59         /* get point to message */
60         message = (struct lcr_msg *)((unsigned long)param - ((unsigned long)(&message->param) - (unsigned long)message));
61
62         /* protect, so forwarded messages are not freed after handling */
63         message->keep = 1;
64
65         message->id_from = id_from;
66         message->id_to = id_to;
67         message->flow = flow;
68         message_put(message);
69
70         return(message);
71 }
72
73 /* detaches the first messages from the message chain */
74 struct lcr_msg *message_get(void)
75 {
76         struct lcr_msg *message;
77
78         if (!message_first)
79                 return(0);
80
81         message = message_first;
82         message_first = message->next;
83         if (!message_first)
84                 messagepointer_end = &message_first;
85
86         message->keep = 0;
87
88         if ((options.deb&DEBUG_MSG) && message->type != MESSAGE_DATA)
89
90                 PDEBUG(DEBUG_MSG, "message %s reading from %ld to %ld (memory %x)\n", messages_txt[message->type], message->id_from, message->id_to, message);
91
92         return(message);
93 }
94
95 /* free a message */
96 void message_free(struct lcr_msg *message)
97 {
98         if (message->keep)
99                 return;
100         FREE(message, sizeof(struct lcr_msg));
101         mmemuse--;
102 }
103
104