From ae003d1854021bdbc3128b2752198761d06772fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Wed, 22 Apr 2020 20:16:30 -0300 Subject: Refactor page structure Changed the page's memory alignment to hopefully use less space. Also renamed buffer to elements. --- page.c | 14 +++++++------- point.c | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/page.c b/page.c index b945ef8..9a712ed 100644 --- a/page.c +++ b/page.c @@ -5,17 +5,17 @@ #define PAGE_SIZE 4096 struct page { - uint8_t *buffer; + uint8_t *elements; + struct page *next; + struct page *prev; uint16_t gap_start; uint16_t gap_end; uint16_t element_count; - struct page *next; - struct page *prev; }; struct page *new_page() { struct page *result = malloc(sizeof(struct page)); - result->buffer = malloc(PAGE_SIZE); + result->elements = malloc(PAGE_SIZE); result->gap_start = 0; result->gap_end = PAGE_SIZE; result->next = 0; @@ -26,7 +26,7 @@ struct page *new_page() { void split_page(struct page *back) { struct page *front = new_page(); - memcpy(front->buffer, back->buffer + PAGE_SIZE / 2, PAGE_SIZE / 2); + memcpy(front->elements, back->elements + PAGE_SIZE / 2, PAGE_SIZE / 2); front->gap_start = PAGE_SIZE / 2; front->gap_end = PAGE_SIZE; @@ -55,7 +55,7 @@ void free_page(struct page *page) { } void move_gap_forward(struct page *page) { - page->buffer[page->gap_start] = page->buffer[page->gap_end]; + page->elements[page->gap_start] = page->elements[page->gap_end]; page->gap_start++; page->gap_end++; } @@ -63,5 +63,5 @@ void move_gap_forward(struct page *page) { void move_gap_backward(struct page *page) { page->gap_end--; page->gap_start--; - page->buffer[page->gap_end] = page->buffer[page->gap_start]; + page->elements[page->gap_end] = page->elements[page->gap_start]; } diff --git a/point.c b/point.c index e9d6472..8413ad8 100644 --- a/point.c +++ b/point.c @@ -15,7 +15,7 @@ uint16_t index_to_offset(struct point *point) { } uint8_t element(struct point *point) { - return point->page->buffer[index_to_offset(point)]; + return point->page->elements[index_to_offset(point)]; } bool at_eof(struct point *point) { @@ -60,7 +60,7 @@ void insert_at_point(struct point *point, uint8_t c) { } } align_gap(point); - point->page->buffer[point->page->gap_start] = c; + point->page->elements[point->page->gap_start] = c; point->page->gap_start++; point->page->element_count++; move_point_forward(point); -- cgit v1.2.3