From 014db29b31127490295314fee7637fbb39cc82f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Tue, 21 Apr 2020 11:30:53 -0300 Subject: Revert page split and change element count --- page.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/page.c b/page.c index 09bf860..65d344f 100644 --- a/page.c +++ b/page.c @@ -58,23 +58,25 @@ struct page *new_page() { return result; } -void split_page(struct page *front) { - struct page *back = new_page(); +void split_page(struct page *back) { + struct page *front = new_page(); - memcpy(back->buffer, front->buffer, PAGE_SIZE / 2); + memcpy(front->buffer, back->buffer + PAGE_SIZE / 2, PAGE_SIZE / 2); - front->gap_start = 0; - front->gap_end = PAGE_SIZE / 2; + front->gap_start = PAGE_SIZE / 2; + front->gap_end = PAGE_SIZE; + front->element_count = PAGE_SIZE / 2; back->gap_start = PAGE_SIZE / 2; back->gap_end = PAGE_SIZE; + back->element_count = PAGE_SIZE / 2; - if (front->prev) { - front->prev->next = back; + if (back->next) { + back->next->prev = front; } - back->prev = front->prev; - back->next = front; + front->next = back->next; front->prev = back; + back->next = front; } void free_page(struct page *page) { @@ -112,9 +114,10 @@ void insert_at_point(struct point *point, uint8_t c) { struct page *page = point->current_page; if (page->gap_start == page->gap_end) { split_page(page); - if (point->index < PAGE_SIZE / 2) { - page = page->prev; + if (point->index >= PAGE_SIZE / 2) { + page = page->next; point->current_page = page; + point->index -= PAGE_SIZE / 2; } } move_gap(page, point->index); -- cgit v1.2.3