-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
266 lines (238 loc) · 7.82 KB
/
example.c
File metadata and controls
266 lines (238 loc) · 7.82 KB
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
256
257
258
259
260
261
262
263
264
265
266
#include <stdio.h>
#include <stdlib.h>
#include "easyqueue.h"
#define NUM_PUSH_ITEMS (EZQ_FIXED_BUFFER_CAPACITY * 2)
#define NUM_POP_ITEMS (NUM_PUSH_ITEMS / 2)
#define PRINT_FAILURE(func_name, status) \
do { \
printf("[!] " #func_name "() failed with status==%u\n", status); \
} while (0)
/*!
* @brief Simple cleanup routine intended to be passed to \c ezq_destroy()
* if dynamically items are still in the queue at time of destruction.
*
* @param[in,out] p_item Dynamically allocated address to free.
* @param[out] p_count Address of an integer to increment, to count the
* number of times this function is called.
*/
static void
my_cleanup_fn(void * const p_item, void * const p_count);
/*!
* @brief Demonstrates a simple usage of an \e ezq_queue (and it's
* associated API) with stack-allocated items.
*
* @return \c EZQ_STATUS_SUCCESS if all EZQ API calls are successful,
* otherwise an error-specific \c ezq_status value.
*/
static ezq_status
stack_items_example(void);
/*!
* @brief Demonstrates a simple usage of an \e ezq_queue (and it's
* associated API) with heap-allocated items.
*
* @return \c EZQ_STATUS_SUCCESS if all EZQ API calls are successful,
* otherwise an error-specific \c ezq_status value.
*/
static ezq_status
heap_items_example(void);
int
main(int argc, char **argv)
{
ezq_status estat = EZQ_STATUS_UNKNOWN;
printf(
"[+] Demonstrating using a queue with stack-allocated items\n"
);
estat = stack_items_example();
if (EZQ_STATUS_SUCCESS != estat)
{
goto done;
}
printf(
"\n[+] Demonstrating using a queue with heap-allocated items\n"
);
estat = heap_items_example();
if (EZQ_STATUS_SUCCESS != estat)
{
goto done;
}
done:
(void)argc;
(void)argv;
return estat == EZQ_STATUS_SUCCESS ? EXIT_SUCCESS : (int)estat;
} /* main */
static void
my_cleanup_fn(void * const p_item, void * const p_count)
{
/* Items should have been allocated memory via malloc(), so all that's
* needed is to call free() on them.
*/
free(p_item);
/* Arbitrary data can also be passed if more is needed during the
* cleanup routine. This is normally for supporting cleanup of more
* complex structures, but we can use this functionality for other
* things (like, in this case, counting the number of times this
* function is invoked).
*/
++(*(unsigned int *)p_count);
} /* my_cleanup_fn */
static ezq_status
stack_items_example(void)
{
ezq_status estat = EZQ_STATUS_UNKNOWN;
ezq_queue queue = { 0 };
unsigned int items[NUM_PUSH_ITEMS] = { 0 };
unsigned int *popped = NULL;
unsigned int i = 0;
/* Initialize the queue. */
estat = ezq_init(&queue, 0, malloc, free);
if (EZQ_STATUS_SUCCESS != estat)
{
PRINT_FAILURE(ezq_init, estat);
goto done;
}
printf("[+] Pushing onto fixed-size portion of queue...\n");
for (i = 0; i < NUM_PUSH_ITEMS; ++i)
{
/* Push an item onto the queue. A copy of the address provided to
* ezq_push() is what is actually stored.
* */
items[i] = i + 1;
estat = ezq_push(&queue, &items[i]);
if (EZQ_STATUS_SUCCESS != estat)
{
PRINT_FAILURE(ezq_push, estat);
goto done;
}
printf("[*] Pushed value %u\n", items[i]);
/* When the fixed-size portion of the queue is filled, subsequent
* pushes will begin storing items in an underlying linked list.
* */
if (EZQ_FIXED_BUFFER_CAPACITY == i + 1 && i + 1 < NUM_PUSH_ITEMS)
{
printf(
"[+] Now pushing onto dynamic portion of queue...\n"
);
}
}
printf("[+] Pushed %u/%u items\n", i, NUM_PUSH_ITEMS);
/* Pop items from the queue. */
printf(
"[+] Popping items from queue; items will be moved from the "
"dynamic portion into the fixed-size portion as space is available\n"
);
for (i = 0; i < NUM_POP_ITEMS; ++i)
{
estat = ezq_pop(&queue, (void **)&popped);
if (EZQ_STATUS_SUCCESS != estat)
{
PRINT_FAILURE(ezq_pop, estat);
goto done;
}
printf("[*] Popped value: %d\n", *popped);
}
printf("[+] Popped %u/%u items\n", i, NUM_POP_ITEMS);
/* Tear down the queue. Any remaining items will be implicitly popped.
* Since the items in the queue do not require any kind of cleanup
* routine, no function is passed to ezq_destroy().
*/
printf("[+] Tearing down queue\n");
estat = ezq_destroy(&queue, NULL, NULL);
if (EZQ_STATUS_SUCCESS != estat)
{
PRINT_FAILURE(ezq_destroy, estat);
}
done:
return estat;
} /* stack_items_example */
static ezq_status
heap_items_example(void)
{
ezq_status estat = EZQ_STATUS_UNKNOWN;
ezq_queue queue = { 0 };
unsigned int *p_item = NULL;
unsigned int *popped = NULL;
unsigned int i = 0;
/* Initialize the queue. We have to provide dynamic allocation (and
* free) functions in case the underlying fixed-suze buffer is filled.
* */
estat = ezq_init(&queue, 0, malloc, free);
if (EZQ_STATUS_SUCCESS != estat)
{
PRINT_FAILURE(ezq_init, estat);
goto done;
}
printf("[+] Pushing onto fixed-size portion of queue...\n");
for (i = 0; i < NUM_PUSH_ITEMS; ++i)
{
/* Here, we will dynamically allocate memory for every single item
* that is pushed onto the queue. These items will need to be freed
* after being popped from the queue.
*/
p_item = malloc(sizeof(int));
if (NULL == p_item)
{
printf("[!] malloc() failure\n");
goto done;
}
*p_item = i + 1;
/* Push items onto the queue. A copy of the address provided to
* ezq_push() is what is actually stored.
* */
estat = ezq_push(&queue, p_item);
if (EZQ_STATUS_SUCCESS != estat)
{
PRINT_FAILURE(ezq_push, estat);
goto done;
}
printf("[*] Pushed value %u\n", *p_item);
/* IMPORTANT: It is up to the caller to properly enforce memory
* ownership after an item is pushed onto the queue.
* */
p_item = NULL;
/* When the fixed-size portion of the queue is filled, subsequent
* pushes will begin storing items in an underlying linked list.
* */
if (EZQ_FIXED_BUFFER_CAPACITY == i + 1 && i + 1 < NUM_PUSH_ITEMS)
{
printf(
"[+] Now pushing onto dynamic portion of queue...\n"
);
}
}
printf("[+] Pushed %u/%u items\n", i, NUM_PUSH_ITEMS);
/* Pop items from the queue. */
printf(
"[+] Popping items from queue; items will be moved from the "
"dynamic portion into the fixed-size portion as space is available\n"
);
for (i = 0; i < NUM_POP_ITEMS; ++i)
{
estat = ezq_pop(&queue, (void **)&popped);
if (EZQ_STATUS_SUCCESS != estat)
{
PRINT_FAILURE(ezq_pop, estat);
goto done;
}
printf("[*] Popped value: %d\n", *popped);
/* Since the pushed items were dynamically allocated, they should be
* appropriately freed.
*/
free(popped);
popped = NULL;
}
printf("[+] Popped %u/%u items\n", i, NUM_POP_ITEMS);
/* Tear down the queue. Any remaining items will be implicitly popped.
* Note how this time we pass a cleanup function to ezq_destroy().
*/
printf("[+] Tearing down queue\n");
i = 0;
estat = ezq_destroy(&queue, my_cleanup_fn, &i);
if (EZQ_STATUS_SUCCESS != estat)
{
PRINT_FAILURE(ezq_destroy, estat);
goto done;
}
printf("[+] my_cleanup_fn() was called %u times\n", i);
done:
return estat;
} /* heap_items_example */