pulse-server: message: store pointer to impl directly

Store a pointer to the owner `impl` object instead of
the (embedded) `stat` object. This way `message_free()`
can be simplified since the owner `impl` does not need
to be passed explicitly.
This commit is contained in:
Barnabás Pőcze 2022-06-16 19:54:13 +02:00 committed by Wim Taymans
parent b843b0ab29
commit 080a97c0bb
5 changed files with 27 additions and 30 deletions

View file

@ -159,10 +159,10 @@ void client_free(struct client *client)
pending_sample_free(p);
if (client->message)
message_free(impl, client->message, false, false);
message_free(client->message, false, false);
spa_list_consume(msg, &client->out_messages, link)
message_free(impl, msg, true, false);
message_free(msg, true, false);
spa_list_consume(o, &client->operations, link)
operation_free(o);
@ -220,14 +220,12 @@ int client_queue_message(struct client *client, struct message *msg)
return 0;
error:
message_free(impl, msg, false, false);
message_free(msg, false, false);
return res;
}
static int client_try_flush_messages(struct client *client)
{
struct impl *impl = client->impl;
pw_log_trace("client %p: flushing", client);
spa_assert(!client->disconnect);
@ -254,7 +252,7 @@ static int client_try_flush_messages(struct client *client)
} else {
if (debug_messages && m->channel == SPA_ID_INVALID)
message_dump(SPA_LOG_LEVEL_INFO, m);
message_free(impl, m, true, false);
message_free(m, true, false);
client->out_index = 0;
continue;
}
@ -307,7 +305,7 @@ static bool drop_from_out_queue(struct client *client, struct message *m)
if (m == first && client->out_index > 0)
return false;
message_free(client->impl, m, true, false);
message_free(m, true, false);
return true;
}

View file

@ -397,12 +397,12 @@ static int ensure_size(struct message *m, uint32_t size)
diff = alloc - m->allocated;
if ((data = realloc(m->data, alloc)) == NULL) {
free(m->data);
m->stat->allocated -= m->allocated;
m->impl->stat.allocated -= m->allocated;
m->allocated = 0;
return -errno;
}
m->stat->allocated += diff;
m->stat->accumulated += diff;
m->impl->stat.allocated += diff;
m->impl->stat.accumulated += diff;
m->data = data;
m->allocated = alloc;
return size;
@ -826,18 +826,20 @@ struct message *message_alloc(struct impl *impl, uint32_t channel, uint32_t size
msg = spa_list_first(&impl->free_messages, struct message, link);
spa_list_remove(&msg->link);
pw_log_trace("using recycled message %p size:%d", msg, size);
spa_assert(msg->impl == impl);
} else {
if ((msg = calloc(1, sizeof(*msg))) == NULL)
return NULL;
pw_log_trace("new message %p size:%d", msg, size);
msg->stat = &impl->stat;
msg->stat->n_allocated++;
msg->stat->n_accumulated++;
msg->impl = impl;
msg->impl->stat.n_allocated++;
msg->impl->stat.n_accumulated++;
}
if (ensure_size(msg, size) < 0) {
message_free(impl, msg, false, true);
message_free(msg, false, true);
return NULL;
}
@ -849,23 +851,23 @@ struct message *message_alloc(struct impl *impl, uint32_t channel, uint32_t size
return msg;
}
void message_free(struct impl *impl, struct message *msg, bool dequeue, bool destroy)
void message_free(struct message *msg, bool dequeue, bool destroy)
{
if (dequeue)
spa_list_remove(&msg->link);
if (msg->stat->allocated > MAX_ALLOCATED || msg->allocated > MAX_SIZE)
if (msg->impl->stat.allocated > MAX_ALLOCATED || msg->allocated > MAX_SIZE)
destroy = true;
if (destroy) {
pw_log_trace("destroy message %p size:%d", msg, msg->allocated);
msg->stat->n_allocated--;
msg->stat->allocated -= msg->allocated;
msg->impl->stat.n_allocated--;
msg->impl->stat.allocated -= msg->allocated;
free(msg->data);
free(msg);
} else {
pw_log_trace("recycle message %p size:%d/%d", msg, msg->length, msg->allocated);
spa_list_append(&impl->free_messages, &msg->link);
spa_list_append(&msg->impl->free_messages, &msg->link);
msg->length = 0;
}
}

View file

@ -32,11 +32,10 @@
#include <spa/support/log.h>
struct impl;
struct stats;
struct message {
struct spa_list link;
struct stats *stat;
struct impl *impl;
uint32_t extra[4];
uint32_t channel;
uint32_t allocated;
@ -68,7 +67,7 @@ enum {
};
struct message *message_alloc(struct impl *impl, uint32_t channel, uint32_t size);
void message_free(struct impl *impl, struct message *msg, bool dequeue, bool destroy);
void message_free(struct message *msg, bool dequeue, bool destroy);
int message_get(struct message *m, ...);
int message_put(struct message *m, ...);
int message_dump(enum spa_log_level level, struct message *m);

View file

@ -4315,7 +4315,7 @@ error_invalid:
goto error;
error:
if (reply)
message_free(impl, reply, false, false);
message_free(reply, false, false);
return res;
}
@ -4391,7 +4391,7 @@ static int do_get_sample_info(struct client *client, uint32_t command, uint32_t
error:
if (reply)
message_free(impl, reply, false, false);
message_free(reply, false, false);
return res;
}
@ -5355,7 +5355,7 @@ static void impl_clear(struct impl *impl)
client_free(c);
spa_list_consume(msg, &impl->free_messages, link)
message_free(impl, msg, true, true);
message_free(msg, true, true);
pw_map_for_each(&impl->samples, impl_free_sample, impl);
pw_map_clear(&impl->samples);

View file

@ -66,7 +66,6 @@
static int handle_packet(struct client *client, struct message *msg)
{
struct impl * const impl = client->impl;
uint32_t command, tag;
int res = 0;
@ -110,7 +109,7 @@ static int handle_packet(struct client *client, struct message *msg)
res = cmd->run(client, command, tag, msg);
finish:
message_free(impl, msg, false, false);
message_free(msg, false, false);
if (res < 0)
reply_error(client, command, tag, res);
@ -119,7 +118,6 @@ finish:
static int handle_memblock(struct client *client, struct message *msg)
{
struct impl * const impl = client->impl;
struct stream *stream;
uint32_t channel, flags, index;
int64_t offset, diff;
@ -190,7 +188,7 @@ static int handle_memblock(struct client *client, struct message *msg)
stream_send_request(stream);
finish:
message_free(impl, msg, false, false);
message_free(msg, false, false);
return res;
}
@ -264,7 +262,7 @@ static int do_read(struct client *client)
}
if (client->message)
message_free(impl, client->message, false, false);
message_free(client->message, false, false);
client->message = message_alloc(impl, channel, length);
} else if (client->message &&