1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/**
* xrdp: A Remote Desktop Protocol server.
*
* Copyright (C) Laxmikant Rashinkar 2013 LK.Rashinkar@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* FIFO implementation to store a pointer to a user struct */
/* module based logging */
#define MODULE_NAME "FIFO "
#define LOCAL_DEBUG
#include "fifo.h"
#include "mlog.h"
#include "os_calls.h"
/**
* Initialize a FIFO that grows as required
*
* @param fp pointer to a FIFO
* @param num_entries initial size
*
* @return 0 on success, -1 on failure
*****************************************************************************/
int
fifo_init(FIFO* fp, int num_entries)
{
log_debug_high("entered");
/* validate params */
if (!fp)
{
log_debug_high("invalid parameters");
return -1;
}
if (num_entries < 1)
num_entries = 10;
fp->rd_ptr = 0;
fp->wr_ptr = 0;
fp->user_data = (long *) g_malloc(sizeof(long) * num_entries, 1);
if (fp->user_data)
{
fp->entries = num_entries;
log_debug_low("FIFO created; rd_ptr=%d wr_ptr=%d entries=%d",
fp->rd_ptr, fp->wr_ptr, fp->entries);
return 0;
}
else
{
log_error("FIFO create error; system out of memory");
fp->entries = 0;
return -1;
}
}
/**
* Deinit FIFO and release resources
*
* @param fp FIFO to deinit
*
* @return 0 on success, -1 on error
*****************************************************************************/
int
fifo_deinit(FIFO* fp)
{
log_debug_high("entered");
if (!fp)
{
log_debug_high("FIFO is null");
return -1;
}
if (fp->user_data)
{
g_free(fp->user_data);
fp->user_data = 0;
}
fp->rd_ptr = 0;
fp->wr_ptr = 0;
fp->entries = 0;
return 0;
}
/**
* Check if FIFO is empty
*
* @param fp FIFO
*
* @return 1 if FIFO is empty, 0 otherwise
*****************************************************************************/
int
fifo_is_empty(FIFO* fp)
{
log_debug_high("entered");
if (!fp)
{
log_debug_high("FIFO is null");
return 0;
}
return (fp->rd_ptr == fp->wr_ptr) ? 1 : 0;
}
/**
* Insert an item at the end
*
* @param fp FIFO
* @param data data to insert into FIFO
*
* @param 0 on success, -1 on error
*****************************************************************************/
int
fifo_insert(FIFO* fp, void* data)
{
long* lp;
int next_val; /* next value for wr_ptr */
int i;
log_debug_high("entered");
if (!fp)
{
log_debug_high("FIFO is null");
return -1;
}
next_val = fp->wr_ptr + 1;
if (next_val >= fp->entries)
next_val = 0;
if (next_val == fp->rd_ptr)
{
/* FIFO is full, expand it by 10 entries */
lp = (long *) g_malloc(sizeof(long) * (fp->entries + 10), 1);
if (!lp)
{
log_debug_low("FIFO full; cannot expand, no memory");
return -1;
}
log_debug_low("FIFO full, expanding by 10 entries");
/* copy old data new location */
for (i = 0; i < (fp->entries - 1); i++)
{
lp[i] = fp->user_data[fp->rd_ptr++];
if (fp->rd_ptr >= fp->entries)
fp->rd_ptr = 0;
}
/* update pointers */
fp->rd_ptr = 0;
fp->wr_ptr = fp->entries - 1;
next_val = fp->entries;
fp->entries += 10;
/* free old data */
g_free(fp->user_data);
fp->user_data = lp;
}
log_debug_low("inserting data at index %d", fp->wr_ptr);
fp->user_data[fp->wr_ptr] = (long) data;
fp->wr_ptr = next_val;
return 0;
}
/**
* Remove an item from the head
*
* @param fp FIFO
*
* @param data on success, NULL on error
*****************************************************************************/
void*
fifo_remove(FIFO* fp)
{
long data;
log_debug_high("entered");
if (!fp)
{
log_debug_high("FIFO is null");
return 0;
}
if (fp->rd_ptr == fp->wr_ptr)
{
log_debug_high("FIFO is empty");
return 0;
}
log_debug_low("removing data at index %d", fp->rd_ptr);
data = fp->user_data[fp->rd_ptr++];
if (fp->rd_ptr >= fp->entries)
{
log_debug_high("FIFO rd_ptr wrapped around");
fp->rd_ptr = 0;
}
return (void *) data;
}
/**
* Return item from head, but do not remove it
*
* @param fp FIFO
*
* @param data on success, NULL on error
*****************************************************************************/
void*
fifo_peek(FIFO* fp)
{
log_debug_high("entered\n");
if (!fp)
{
log_debug_high("FIFO is null\n");
return 0;
}
if (fp->rd_ptr == fp->wr_ptr)
{
log_debug_high("FIFO is empty\n");
return 0;
}
log_debug_low("peeking data at index %d\n", fp->rd_ptr);
return (void *) fp->user_data[fp->rd_ptr];
}
|