From: Liam R. Howlett Date: Thu, 21 Aug 2025 20:30:24 +0000 (-0400) Subject: abstract split dest and src X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=a612a709fc1faca22883234e992c64d078f7f604;p=users%2Fjedix%2Flinux-maple.git abstract split dest and src Signed-off-by: Liam R. Howlett --- diff --git a/lib/maple_tree.c b/lib/maple_tree.c index a83d5a5b01d9..7ef6c481702b 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -2999,6 +2999,92 @@ static inline void spanning_data_calc(struct maple_copy *cp, } +static inline +void spanning_split_dest_setup(struct maple_copy *cp, struct ma_state *mas, + enum maple_type mt) +{ + cp->d_count = 0; + /* Calc split here */ + if (cp->data < mt_slots[mt]) { + cp->split = cp->data; + cp->dst[cp->d_count].mt = mt; + cp->dst[cp->d_count++].node = ma_mnode_ptr(mas_pop_node(mas)); + /* New root */ + } else if (cp->data >= mt_slots[mt] * 2 - 1) { + cp->split = cp->data / 3; + cp->dst[cp->d_count].mt = mt; + cp->dst[cp->d_count++].node = ma_mnode_ptr(mas_pop_node(mas)); + cp->dst[cp->d_count].mt = mt; + cp->dst[cp->d_count++].node = ma_mnode_ptr(mas_pop_node(mas)); + cp->dst[cp->d_count].mt = mt; + cp->dst[cp->d_count++].node = ma_mnode_ptr(mas_pop_node(mas)); + /* New root */ + } else { + cp->split = (cp->data + 1) / 2; + cp->dst[cp->d_count].mt = mt; + cp->dst[cp->d_count++].node = ma_mnode_ptr(mas_pop_node(mas)); + cp->dst[cp->d_count].mt = mt; + cp->dst[cp->d_count++].node = ma_mnode_ptr(mas_pop_node(mas)); + } + printk("split = %u data %u d_count %u\n", cp->split, cp->data, cp->d_count); +} + +static inline +void spanning_split_src_setup(struct maple_copy *cp, struct ma_state *mas, + struct ma_wr_state *l_wr_mas, struct ma_wr_state *r_wr_mas, + struct ma_state *sib) +{ + cp->s_count = 0; + if (sib->end && sib->max < l_wr_mas->mas->min) + append_node_cp(cp, sib, 0, sib->end); + + /* Copy left 0 - offset */ + if (l_wr_mas->mas->offset || l_wr_mas->r_min < mas->index) { + unsigned long l_end = l_wr_mas->mas->offset; + + if (l_wr_mas->r_min == mas->index) + l_end--; + append_node_cp(cp, l_wr_mas->mas, 0, l_end); + } + + if (cp->s_count) + cp->src[cp->s_count - 1].max = mas->index - 1; + /* Insert, overwrite prev pivot */ + cp->src[cp->s_count].node = ma_mnode_ptr(cp); + cp->src[cp->s_count].max = mas->last; + cp->src[cp->s_count].mt = maple_copy; + cp->src[cp->s_count].end = 0; + cp->src[cp->s_count].start = 0; + cp->slot[0] = l_wr_mas->entry; + cp->max = mas->index; + if (l_wr_mas->entry) + cp->gap[0] = 0; + else + cp->gap[0] = mas->last - mas->index; + cp->s_count++; + + /* Copy right either from offset or offset + 1 pending on r_max */ + if (r_wr_mas->mas->end != r_wr_mas->mas->offset || r_wr_mas->r_max > mas->last) { + unsigned long r_start = r_wr_mas->mas->offset; + + if (r_wr_mas->r_max == mas->last) + r_start++; + + append_node_cp(cp, r_wr_mas->mas, r_wr_mas->mas->offset, r_start); + } + + if (sib->end) { + if (sib->min > r_wr_mas->mas->max) { + append_node_cp(cp, sib, 0, sib->end); + /* Copy sibling, if necessary */ + /* set sib to right */ + //r_wr_mas->mas = sib; + } else { + /* set sib to left*/ + //l_wr_mas->mas = sib; + } + } +} static void spanning_data_write(struct maple_copy *cp, struct ma_state *mas) { struct maple_node *dst, *src; @@ -3296,7 +3382,6 @@ static noinline void mas_wr_spanning_rebalance(struct ma_state *mas, struct maple_big_node b_node; struct ma_state sib; struct maple_copy cp; - enum maple_type mt; MA_STATE(l_mas, mas->tree, mas->index, mas->index); MA_STATE(r_mas, mas->tree, mas->index, mas->last); MA_STATE(m_mas, mas->tree, mas->index, mas->index); @@ -3311,82 +3396,9 @@ static noinline void mas_wr_spanning_rebalance(struct ma_state *mas, mt_dump(mas->tree, mt_dump_hex); spanning_data_calc(&cp, mas, mast, l_wr_mas, r_wr_mas, &sib); - mt = l_wr_mas->type; - cp.d_count = 0; - /* Calc split here */ - if (cp.data < mt_slots[mt]) { - cp.split = cp.data; - cp.dst[cp.d_count].mt = mt; - cp.dst[cp.d_count++].node = ma_mnode_ptr(mas_pop_node(mas)); - /* New root */ - } else if (cp.data >= mt_slots[mt] * 2 - 1) { - cp.split = cp.data / 3; - cp.dst[cp.d_count].mt = mt; - cp.dst[cp.d_count++].node = ma_mnode_ptr(mas_pop_node(mas)); - cp.dst[cp.d_count].mt = mt; - cp.dst[cp.d_count++].node = ma_mnode_ptr(mas_pop_node(mas)); - cp.dst[cp.d_count].mt = mt; - cp.dst[cp.d_count++].node = ma_mnode_ptr(mas_pop_node(mas)); - /* New root */ - } else { - cp.split = (cp.data + 1) / 2; - cp.dst[cp.d_count].mt = mt; - cp.dst[cp.d_count++].node = ma_mnode_ptr(mas_pop_node(mas)); - cp.dst[cp.d_count].mt = mt; - cp.dst[cp.d_count++].node = ma_mnode_ptr(mas_pop_node(mas)); - } - printk("split = %u data %u d_count %u\n", cp.split, cp.data, cp.d_count); - - cp.s_count = 0; - if (sib.end && sib.max < l_wr_mas->mas->min) - append_node_cp(&cp, &sib, 0, sib.end); - - /* Copy left 0 - offset */ - if (l_wr_mas->mas->offset || l_wr_mas->r_min < mas->index) { - unsigned long l_end = l_wr_mas->mas->offset; - - if (l_wr_mas->r_min == mas->index) - l_end--; - append_node_cp(&cp, l_wr_mas->mas, 0, l_end); - } - - if (cp.s_count) - cp.src[cp.s_count - 1].max = mas->index - 1; - /* Insert, overwrite prev pivot */ - cp.src[cp.s_count].node = ma_mnode_ptr(&cp); - cp.src[cp.s_count].max = mas->last; - cp.src[cp.s_count].mt = maple_copy; - cp.src[cp.s_count].end = 0; - cp.src[cp.s_count].start = 0; - cp.slot[0] = l_wr_mas->entry; - cp.max = mas->index; - if (l_wr_mas->entry) - cp.gap[0] = 0; - else - cp.gap[0] = mas->last - mas->index; - cp.s_count++; - - /* Copy right either from offset or offset + 1 pending on r_max */ - if (r_wr_mas->mas->end != r_wr_mas->mas->offset || r_wr_mas->r_max > mas->last) { - unsigned long r_start = r_wr_mas->mas->offset; - - if (r_wr_mas->r_max == mas->last) - r_start++; + spanning_split_dest_setup(&cp, mas, l_wr_mas->type); + spanning_split_src_setup(&cp, mas, l_wr_mas, r_wr_mas, &sib); - append_node_cp(&cp, r_wr_mas->mas, r_wr_mas->mas->offset, r_start); - } - - if (sib.end) { - if (sib.min > r_wr_mas->mas->max) { - append_node_cp(&cp, &sib, 0, sib.end); - /* Copy sibling, if necessary */ - /* set sib to right */ - //r_wr_mas->mas = sib; - } else { - /* set sib to left*/ - //l_wr_mas->mas = sib; - } - } spanning_data_write(&cp, mas);