Skip to content

Commit f3f5574

Browse files
author
Alex Young
committed
Testing, examples, interface changes
- Added unit testing with check - Added improved makefiles - queue_handle_t -> queue_handle to be a little more POSIX compliant (names ending in _t are reserved) - even though queue_t still ends in _t (i aim to change when finding a suitable name for it) - Readme updates - Project restructure
1 parent 57e0baf commit f3f5574

File tree

9 files changed

+380
-19
lines changed

9 files changed

+380
-19
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
main
1+
.DS_Store
2+
.out

Makefile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
main: main.c queue.h
2-
gcc -std=c99 main.c -o main
1+
default: check examples
32

4-
clean:
5-
rm main
3+
check:
4+
$(MAKE) -C tests $@
5+
6+
examples:
7+
$(MAKE) -C examples
8+
9+
.PHONY: default check examples

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,34 @@ A type-agnostic header-only macro-based struct queue implementation in C.
44

55
## Usage
66

7-
To create a queueable struct, include the `queue_handle_t qh` member in your struct.
7+
To create a queueable struct, include the `queue_handle_t qh` member in your
8+
struct.
89

9-
Before using QUEUE_PUSH, ensure that the queue struct is set to NULL, this is the only initialisation that needs to be done.
10+
Before using QUEUE_PUSH, ensure that the queue struct is set to NULL, this is
11+
the only initialisation that needs to be done.
1012

1113
This queue implementation is not thread safe.
1214

13-
Freeing the queue does not free every element in the queue if they have been dynamically allocated. This has to be done by popping all the elements in the queue and freeing them manually.
15+
Freeing the queue does not free every element in the queue if they have been
16+
dynamically allocated. This has to be done by popping all the elements in the
17+
queue and freeing them manually.
18+
19+
Pushing the same element (at the same address) into the queue is not supported.
20+
This is because the 'next' member of the queue's queue_handle will be
21+
overwitten. This will case undefined behaviour when pushing or popping to or
22+
from the queue (see commented out test case).
1423

1524
```c
1625
#include <stdio.h>
1726
#include "queue.h"
1827

1928
struct msg {
2029
char *content;
21-
queue_handle_t qh;
30+
queue_handle qh;
2231
}
2332

2433
int main(void) {
25-
struct msgs *queue;
34+
struct msg *queue;
2635
struct msg m1, m2;
2736

2837
queue = NULL;

examples/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
default:
2+
3+
.PHONY: default

main.c renamed to examples/main.c

File renamed without changes.

examples/readme_example.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <stdio.h>
2+
#include "queue.h"
3+
4+
struct msg {
5+
char *content;
6+
queue_handle qh;
7+
}
8+
9+
int main(void) {
10+
struct msg *queue;
11+
struct msg m1, m2;
12+
13+
queue = NULL;
14+
15+
m1.content = "abc";
16+
QUEUE_PUSH(queue, &m1);
17+
18+
printf("queue size: %d\n", QUEUE_SIZE(queue)); // queue size: 1
19+
20+
QUEUE_POP(queue, &m2);
21+
printf("m2 content: %s\n", m2.content); // m2 content: abc
22+
23+
QUEUE_FREE(queue);
24+
25+
return 0;
26+
}

queue.h renamed to src/queue.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66
typedef struct {
77
void *front;
88
void *back;
9-
10-
void *frontqh;
9+
void *frontqh; // TODO: remove?
1110
void *backqh;
12-
1311
unsigned int size;
1412
} queue_t;
1513

1614
typedef struct queue_handle {
1715
queue_t *q;
1816
void *next;
19-
} queue_handle_t;
17+
} queue_handle;
2018

2119
#define QUEUE_PUSH(queue, element) \
2220
do { \
@@ -31,8 +29,7 @@ typedef struct queue_handle {
3129
(queue) = (element); \
3230
} else { \
3331
queue_t *q = (queue)->qh.q; \
34-
queue_handle_t *backqh = q->backqh; \
35-
queue_handle_t *frontqh = q->frontqh; \
32+
queue_handle *backqh = q->backqh; \
3633
\
3734
(element)->qh.q = q; \
3835
(element)->qh.next = NULL; \
@@ -43,8 +40,6 @@ typedef struct queue_handle {
4340
backqh = &((element)->qh); \
4441
\
4542
q->backqh = backqh; \
46-
q->frontqh = frontqh; \
47-
\
4843
} \
4944
(queue)->qh.q->size++; \
5045
} while (0)
@@ -66,7 +61,9 @@ typedef struct queue_handle {
6661

6762
#define QUEUE_FREE(queue) \
6863
do { \
69-
free(queue->qh.q); \
64+
if (queue && queue->qh.q) { \
65+
free(queue->qh.q); \
66+
} \
7067
} while (0)
7168

7269
#endif /* QUEUE_H_ */

tests/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CC = gcc
2+
STD = -std=c99
3+
CFLAGS = $(STD) -I ../src
4+
LIBS = -lcheck
5+
6+
check: check_queue.c ../src/queue.h
7+
$(CC) $(CFLAGS) check_queue.c -o check_queue $(LIBS)
8+
./check_queue
9+
rm check_queue
10+
11+
.PHONY: check

0 commit comments

Comments
 (0)