1 /*
2 	Relay -- a tool to record and play Quake2 demos
3 	Copyright (C) 2000 Conor Davis
4 
5 	This program is free software; you can redistribute it and/or
6 	modify it under the terms of the GNU General Public License
7 	as published by the Free Software Foundation; either version 2
8 	of the License, or (at your option) any later version.
9 
10 	This program is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with this program; if not, write to the Free Software
17 	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 	Conor Davis
20 	cedavis@planetquake.com
21 */
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "shared.h"
27 #include "q2defines.h"
28 #include "msg.h"
29 
30 static msg_t *msg_head = NULL;
31 
CreateMessage(int id,void * data,size_t data_size,qboolean reliable)32 msg_t *CreateMessage(int id, void *data, size_t data_size, qboolean reliable)
33 {
34 	msg_t	*msg;
35 
36 	msg = Z_Malloc(sizeof(msg_t));
37 	msg->id = id;
38 	msg->reliable = reliable;
39 	msg->reference_count = 0;
40 	msg->prev = NULL;
41 	msg->next = msg_head;
42 
43 	msg_head = msg;
44 	if (msg->next)
45 		msg->next->prev = msg;
46 
47 	if (data_size)
48 	{
49 		msg->data = Z_Malloc(data_size);
50 		memcpy(msg->data, data, data_size);
51 	}
52 	else
53 		msg->data = NULL;
54 
55 	return msg;
56 }
57 
FreeMessage(msg_t * msg)58 void FreeMessage(msg_t *msg)
59 {
60 	if (msg->prev)
61 		msg->prev->next = msg->next;
62 	else
63 		msg_head = msg->next;
64 
65 	if (msg->next)
66 		msg->next->prev = msg->prev;
67 
68 	if (msg->data)
69 		Z_Free(msg->data);
70 
71 	Z_Free(msg);
72 }
73 
AddMessage(msg_t ** array,msg_t * msg)74 void AddMessage(msg_t **array, msg_t *msg)
75 {
76 	while (array[0])
77 		array++;
78 
79 	array[0] = msg;
80 	array[1] = NULL;
81 
82 	msg->reference_count++;
83 }
84 
RemoveMessage(msg_t ** array,msg_t * msg)85 void RemoveMessage(msg_t **array, msg_t *msg)
86 {
87 	msg->reference_count--;
88 	if (msg->reference_count <= 0)
89 		FreeMessage(msg);
90 
91 	while (array[0])
92 	{
93 		if (array[0] == msg)
94 		{
95 			while (array[0])
96 			{
97 				array[0] = array[1];
98 				array++;
99 			}
100 			return;
101 		}
102 		array++;
103 	}
104 }
105 
RemoveAllMessages(msg_t ** array)106 void RemoveAllMessages(msg_t **array)
107 {
108 	while (array[0])
109 	{
110 		array[0]->reference_count--;
111 		if (array[0]->reference_count <= 0)
112 			FreeMessage(array[0]);
113 		array[0] = NULL;
114 		array++;
115 	}
116 }
117 
FreeAllMessages()118 void FreeAllMessages()
119 {
120 	while (msg_head)
121 		FreeMessage(msg_head);
122 }