Skip to content

Commit 1a50bac

Browse files
committed
Rework am_event_free() to take pointer to event - not double pointer
1 parent 05fd3d7 commit 1a50bac

File tree

6 files changed

+17
-26
lines changed

6 files changed

+17
-26
lines changed

libs/ao/ao.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ bool am_ao_publish_exclude_x(
117117
* the function. Also takes care of the case when no active objects
118118
* subscribed to this event.
119119
*/
120-
am_event_free(&event);
120+
am_event_free(event);
121121

122122
return all_published;
123123
}

libs/ao/cooperative/port.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,9 @@ void am_ao_stop(struct am_ao *ao) {
159159

160160
struct am_event **e = NULL;
161161
while ((e = am_queue_pop_front(&ao->event_queue)) != NULL) {
162-
const struct am_event *event = *e;
163162
me->crit_exit();
164-
AM_ASSERT(event);
165-
am_event_free(&event);
163+
AM_ASSERT(*e);
164+
am_event_free(*e);
166165
me->crit_enter();
167166
}
168167
am_queue_dtor(&ao->event_queue);

libs/ao/preemptive/port.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,9 @@ void am_ao_stop(struct am_ao *ao) {
153153

154154
struct am_event **e = NULL;
155155
while ((e = am_queue_pop_front(&ao->event_queue)) != NULL) {
156-
const struct am_event *event = *e;
157156
me->crit_exit();
158-
AM_ASSERT(event);
159-
am_event_free(&event);
157+
AM_ASSERT(*e);
158+
am_event_free(*e);
160159
me->crit_enter();
161160
}
162161
am_queue_dtor(&ao->event_queue);

libs/event/event.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ struct am_event *am_event_allocate(int id, int size) {
145145
return event;
146146
}
147147

148-
static void am_event_free_unsafe(const struct am_event **event) {
149-
if (am_event_is_static(*event)) {
148+
static void am_event_free_unsafe(const struct am_event *event) {
149+
if (am_event_is_static(event)) {
150150
return; /* the event is statically allocated */
151151
}
152152

153-
struct am_event *e = AM_CAST(struct am_event *, *event);
153+
struct am_event *e = AM_CAST(struct am_event *, event);
154154
AM_ASSERT(e->pool_index_plus_one <= AM_EVENT_POOLS_NUM_MAX);
155155
/*
156156
* Check if event is valid.
@@ -165,13 +165,10 @@ static void am_event_free_unsafe(const struct am_event **event) {
165165
}
166166

167167
am_onesize_free(&am_event_state_.pools[e->pool_index_plus_one - 1], e);
168-
169-
*event = NULL;
170168
}
171169

172-
void am_event_free(const struct am_event **event) {
170+
void am_event_free(const struct am_event *event) {
173171
AM_ASSERT(event);
174-
AM_ASSERT(*event); /* double free? */
175172

176173
struct am_event_state *me = &am_event_state_;
177174
me->crit_enter();
@@ -325,7 +322,7 @@ void am_event_inc_ref_cnt(const struct am_event *event) {
325322

326323
void am_event_dec_ref_cnt(const struct am_event *event) {
327324
AM_ASSERT(event);
328-
am_event_free(&event);
325+
am_event_free(event);
329326
}
330327

331328
int am_event_get_ref_cnt(const struct am_event *event) {
@@ -385,9 +382,9 @@ static enum am_event_rc am_event_push_x(
385382
if (nfree <= margin) {
386383
if (safe) {
387384
me->crit_exit();
388-
am_event_free(&event);
385+
am_event_free(event);
389386
} else {
390-
am_event_free_unsafe(&event);
387+
am_event_free_unsafe(event);
391388
}
392389
return AM_EVENT_RC_ERR;
393390
}
@@ -503,7 +500,7 @@ bool am_event_pop_front(
503500
return true;
504501
}
505502

506-
am_event_free(&event);
503+
am_event_free(event);
507504

508505
return true;
509506
}
@@ -516,11 +513,10 @@ int am_event_flush_queue(struct am_queue *queue) {
516513

517514
struct am_event **e = NULL;
518515
while ((e = am_queue_pop_front(queue)) != NULL) {
519-
const struct am_event *event = *e;
520516
me->crit_exit();
521517
++cnt;
522-
AM_ASSERT(event);
523-
am_event_free(&event);
518+
AM_ASSERT(*e);
519+
am_event_free(*e);
524520
me->crit_enter();
525521
}
526522

libs/event/event.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,17 +278,14 @@ struct am_event *am_event_allocate(int id, int size);
278278
* Decrements event reference counter by one and frees the event,
279279
* if the reference counter drops to zero.
280280
*
281-
* If the event is freed, then the pointer to the event is set to NULL
282-
* to help catching double free cases.
283-
*
284281
* The function does nothing for statically allocated events -
285282
* the events for which am_event_is_static() returns true.
286283
*
287284
* Thread safe.
288285
*
289286
* @param event the event to free
290287
*/
291-
void am_event_free(const struct am_event **event);
288+
void am_event_free(const struct am_event *event);
292289

293290
/**
294291
* Duplicate an event (eXtended version).

libs/hsm/tests/defer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ static void test_defer(void) {
215215
am_event_allocate(in[i].event, (int)sizeof(struct am_event));
216216
am_event_inc_ref_cnt(e);
217217
am_hsm_dispatch(&me->hsm, e);
218-
am_event_free(&e);
218+
am_event_free(e);
219219
defer_commit();
220220
AM_ASSERT(0 == strncmp(me->log_buf, in[i].out, strlen(in[i].out)));
221221
me->log_buf[0] = '\0';

0 commit comments

Comments
 (0)