Skip to content
This repository was archived by the owner on Aug 19, 2021. It is now read-only.

Commit be5ffc1

Browse files
committed
Fixed uncalled destructors in equeue_destroy
Before, only the heads of slots were correctly destructed. Fixed and added test case.
1 parent eb6eee1 commit be5ffc1

File tree

2 files changed

+50
-23
lines changed

2 files changed

+50
-23
lines changed

equeue.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ int equeue_create_inplace(equeue_t *q, size_t size, void *buffer) {
5757
}
5858

5959
void equeue_destroy(equeue_t *q) {
60-
while (q->queue) {
61-
struct equeue_event *e = q->queue;
62-
q->queue = e->next;
63-
equeue_dealloc(q, e+1);
60+
// call destructors on pending events
61+
for (struct equeue_event *es = q->queue; es; es = es->next) {
62+
for (struct equeue_event *e = q->queue; e; e = e->sibling) {
63+
if (e->dtor) {
64+
e->dtor(e + 1);
65+
}
66+
}
6467
}
6568

6669
equeue_mutex_destroy(&q->memlock);

tests/tests.c

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ void simple_func(void *p) {
4747
}
4848

4949
struct indirect {
50-
bool *touched;
50+
int *touched;
5151
uint8_t buffer[7];
5252
};
5353

5454
void indirect_func(void *p) {
5555
struct indirect *i = (struct indirect*)p;
56-
*i->touched = true;
56+
(*i->touched)++;
5757
}
5858

5959
struct timing {
@@ -142,7 +142,7 @@ void simple_post_test(void) {
142142
int err = equeue_create(&q, 2048);
143143
test_assert(!err);
144144

145-
bool touched = false;
145+
int touched = false;
146146
struct indirect *i = equeue_alloc(&q, sizeof(struct indirect));
147147
test_assert(i);
148148

@@ -162,29 +162,53 @@ void destructor_test(void) {
162162
int err = equeue_create(&q, 2048);
163163
test_assert(!err);
164164

165-
bool touched = false;
166-
struct indirect *i = equeue_alloc(&q, sizeof(struct indirect));
167-
test_assert(i);
165+
int touched;
166+
struct indirect *e;
167+
int ids[3];
168168

169-
i->touched = &touched;
170-
equeue_event_dtor(i, indirect_func);
171-
int id = equeue_post(&q, pass_func, i);
172-
test_assert(id);
169+
touched = 0;
170+
for (int i = 0; i < 3; i++) {
171+
e = equeue_alloc(&q, sizeof(struct indirect));
172+
test_assert(e);
173+
174+
e->touched = &touched;
175+
equeue_event_dtor(e, indirect_func);
176+
int id = equeue_post(&q, pass_func, e);
177+
test_assert(id);
178+
}
173179

174180
equeue_dispatch(&q, 0);
175-
test_assert(touched);
181+
test_assert(touched == 3);
176182

177-
touched = false;
178-
i = equeue_alloc(&q, sizeof(struct indirect));
179-
test_assert(i);
183+
touched = 0;
184+
for (int i = 0; i < 3; i++) {
185+
e = equeue_alloc(&q, sizeof(struct indirect));
186+
test_assert(e);
180187

181-
i->touched = &touched;
182-
equeue_event_dtor(i, indirect_func);
183-
id = equeue_post(&q, pass_func, i);
184-
test_assert(id);
188+
e->touched = &touched;
189+
equeue_event_dtor(e, indirect_func);
190+
ids[i] = equeue_post(&q, pass_func, e);
191+
test_assert(ids[i]);
192+
}
193+
194+
for (int i = 0; i < 3; i++) {
195+
equeue_cancel(&q, ids[i]);
196+
}
197+
test_assert(touched == 3);
198+
199+
touched = 0;
200+
for (int i = 0; i < 3; i++) {
201+
e = equeue_alloc(&q, sizeof(struct indirect));
202+
test_assert(e);
203+
204+
e->touched = &touched;
205+
equeue_event_dtor(e, indirect_func);
206+
int id = equeue_post(&q, pass_func, e);
207+
test_assert(id);
208+
}
185209

186210
equeue_destroy(&q);
187-
test_assert(touched);
211+
test_assert(touched == 3);
188212
}
189213

190214
void allocation_failure_test(void) {

0 commit comments

Comments
 (0)