Skip to content

Commit

Permalink
messagebus: Release memory on cleanup
Browse files Browse the repository at this point in the history
There's no cleanup of its internal allocations right now, fix that.
While at it, convert the silly linked list to a proper one.

Signed-off-by: Alexander Shishkin <[email protected]>
  • Loading branch information
virtuoso committed Oct 5, 2024
1 parent 0fe3a43 commit 978b38d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
2 changes: 2 additions & 0 deletions core/clap.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ struct clap_context *clap_init(struct clap_config *cfg, int argc, char **argv, c
ctx->argv = argv;
ctx->envp = envp;

messagebus_init();
log_init(log_flags);
(void)librarian_init(ctx->cfg.base_url);
if (ctx->cfg.font)
Expand All @@ -156,5 +157,6 @@ void clap_done(struct clap_context *ctx, int status)
phys_done();
if (ctx->cfg.graphics)
gl_done();
messagebus_done();
exit_cleanup_run(status);
}
30 changes: 22 additions & 8 deletions core/messagebus.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "common.h"
#include "messagebus.h"

static struct subscriber *subscriber[MT_MAX];
static struct list subscriber[MT_MAX];

int subscribe(enum message_type type, subscriber_fn fn, void *data)
{
Expand All @@ -16,12 +16,7 @@ int subscribe(enum message_type type, subscriber_fn fn, void *data)

s->handle = fn;
s->data = data;
s->next = NULL;

for (lastp = &subscriber[type]; *lastp; lastp = &((*lastp)->next))
;

*lastp = s;
list_append(&subscriber[type], &s->entry);

return 0;
}
Expand All @@ -31,7 +26,7 @@ int message_send(struct message *m)
struct subscriber *s;
int ret = 0, res;

for (s = subscriber[m->type]; s; s = s->next) {
list_for_each_entry(s, &subscriber[m->type], entry) {
res = s->handle(m, s->data);
if (res == MSG_STOP)
break;
Expand All @@ -43,5 +38,24 @@ int message_send(struct message *m)

int messagebus_init(void)
{
int i;

for (i = 0; i < MT_MAX; i++)
list_init(&subscriber[i]);

return 0;
}

void messagebus_done(void)
{
int i;

for (i = 0; i < MT_MAX; i++) {
struct subscriber *s, *iter;

list_for_each_entry_iter(s, iter, &subscriber[i], entry) {
list_del(&s->entry);
free(s);
}
}
}
3 changes: 2 additions & 1 deletion core/messagebus.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,12 @@ typedef int (*subscriber_fn)(struct message *m, void *data);
struct subscriber {
subscriber_fn handle;
void *data;
struct subscriber *next;
struct list entry;
};

int subscribe(enum message_type type, subscriber_fn fn, void *data);
int message_send(struct message *m);
int messagebus_init(void);
void messagebus_done(void);

#endif /* __CLAP_MESSAGEBUS_H__ */
4 changes: 4 additions & 0 deletions core/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdbool.h>
#include "object.h"
#include "common.h"
#include "messagebus.h"
#include "util.h"

#define TEST_MAGIC0 0xdeadbeef
Expand Down Expand Up @@ -384,6 +385,8 @@ int main()
{
int ret, i;

messagebus_init();

for (i = 0; i < array_size(tests); i++) {
failcount = 0;
ret = tests[i].test();
Expand All @@ -392,6 +395,7 @@ int main()
break;
}

messagebus_done();
#ifdef CONFIG_BROWSER
exit_cleanup_run(ret);
#endif
Expand Down

0 comments on commit 978b38d

Please sign in to comment.