broadcast: invert ownership of messages.

Make the update/announce messages own the element in the broadcast map
not the other way around.

Then we keep a pointer to the message, and when we free it
(eg. channel closed, update replaces it), it gets freed from the
broadcast map automatically.

The result is much nicer!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2018-05-10 21:52:37 +09:30
committed by Christian Decker
parent 8940528bdb
commit c71e16f784
8 changed files with 60 additions and 129 deletions

View File

@@ -26,35 +26,21 @@ static void destroy_queued_message(struct queued_message *msg,
static struct queued_message *new_queued_message(const tal_t *ctx,
struct broadcast_state *bstate,
const u8 *payload TAKES,
const u8 *payload,
u64 index)
{
struct queued_message *msg = tal(ctx, struct queued_message);
msg->payload = tal_dup_arr(msg, u8, payload, tal_len(payload), 0);
msg->payload = payload;
msg->index = index;
uintmap_add(&bstate->broadcasts, index, msg);
tal_add_destructor2(msg, destroy_queued_message, bstate);
return msg;
}
bool replace_broadcast(const tal_t *ctx,
struct broadcast_state *bstate,
u64 *index,
const u8 *payload TAKES)
void insert_broadcast(struct broadcast_state *bstate, const u8 *payload)
{
struct queued_message *msg;
bool evicted = false;
msg = uintmap_get(&bstate->broadcasts, *index);
if (msg) {
tal_free(msg);
evicted = true;
}
/* Now add the message to the queue */
msg = new_queued_message(ctx, bstate, payload, bstate->next_index++);
*index = msg->index;
return evicted;
/* Free payload, free index. */
new_queued_message(payload, bstate, payload, bstate->next_index++);
}
const u8 *next_broadcast(struct broadcast_state *bstate, u64 *last_index)
@@ -66,13 +52,3 @@ const u8 *next_broadcast(struct broadcast_state *bstate, u64 *last_index)
return m->payload;
return NULL;
}
const u8 *get_broadcast(struct broadcast_state *bstate, u64 msgidx)
{
struct queued_message *m;
m = uintmap_get(&bstate->broadcasts, msgidx);
if (m)
return m->payload;
return NULL;
}