From 54b3fb7adb40e4e536e3a172f503be213d091607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Tue, 14 Jul 2020 22:15:15 -0300 Subject: Fix pages not linked to previous ones --- buffer.cpp | 24 ++++++++++++++++++------ test | 6 ++++++ 2 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 test diff --git a/buffer.cpp b/buffer.cpp index ba3f87c..7b4c92d 100644 --- a/buffer.cpp +++ b/buffer.cpp @@ -4,18 +4,30 @@ struct Buffer { Buffer(const char *name) : name(name), storage(new Page()) {} + void free_storage() { + while (storage) { + Page *iter = storage; + storage = storage->next; + delete iter; + } + } + void read_file(const char *pathname) { + free_storage(); int file = open(pathname, O_RDONLY); + storage = new Page(); + int bytes_read = read(file, storage->elements, PAGE_SIZE); Page *iter = storage; - int bytes_read = read(file, iter->elements, PAGE_SIZE); - iter->gap_start = bytes_read; - iter->element_count = bytes_read; - while (bytes_read == PAGE_SIZE) { + while (bytes_read > 0) { + iter->gap_start = bytes_read; + iter->element_count = bytes_read; iter->next = new Page(); + iter->next->prev = iter; iter = iter->next; bytes_read = read(file, iter->elements, PAGE_SIZE); - iter->gap_start = bytes_read; - iter->element_count = bytes_read; + } + if (iter->element_count == 0) { + delete iter; } close(file); } diff --git a/test b/test new file mode 100644 index 0000000..aabbb99 --- /dev/null +++ b/test @@ -0,0 +1,6 @@ +asdfasd +asdfasd +asdfasd +asdfasd +asdfasd +asdfasd -- cgit v1.2.3