From 4ab1df810bfe1236a44f78e24dd55ea00efba69c Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Tue, 5 Apr 2022 19:45:22 +0100 Subject: [PATCH] fix mismatch of new[] and delete in Buffer --- include/fxos/util/Buffer.h | 2 +- include/fxos/vspace.h | 2 +- lib/util/Buffer.cpp | 12 ++++-------- lib/vspace.cpp | 7 ++----- 4 files changed, 8 insertions(+), 15 deletions(-) diff --git a/include/fxos/util/Buffer.h b/include/fxos/util/Buffer.h index d8c9915..214ee99 100644 --- a/include/fxos/util/Buffer.h +++ b/include/fxos/util/Buffer.h @@ -46,7 +46,7 @@ struct Buffer /* Buffer size */ size_t size; /* Data */ - std::shared_ptr data; + std::unique_ptr data; /* File path, when constructed from file */ std::string path; diff --git a/include/fxos/vspace.h b/include/fxos/vspace.h index a2c14a6..40f7208 100644 --- a/include/fxos/vspace.h +++ b/include/fxos/vspace.h @@ -40,7 +40,7 @@ struct Binding: public AbstractMemory /* Constructor from data buffer. An error is raised if the buffer is not at least of the size of the region. In this case, a new buffer can be constructed with the required size. */ - Binding(MemoryRegion region, Buffer buffer); + Binding(MemoryRegion region, Buffer const &buffer); /* Targeted region, might overlap with other bindings */ MemoryRegion region; diff --git a/lib/util/Buffer.cpp b/lib/util/Buffer.cpp index d92e61b..b8a09fc 100644 --- a/lib/util/Buffer.cpp +++ b/lib/util/Buffer.cpp @@ -26,11 +26,8 @@ Buffer::Buffer(): Buffer::Buffer(size_t bufsize, int fill) { this->size = bufsize; - // TODO: Buffer(size, fill) -> use std::make_unique<> - char *buffer = new char[size]; - memset(buffer, fill, size); - - this->data = std::shared_ptr(buffer); + this->data = std::make_unique(bufsize); + memset(this->data.get(), fill, bufsize); this->path = "(anonymous)"; } @@ -53,9 +50,8 @@ void Buffer::loadFromFile(std::string const &filepath, ssize_t bufsize, size_t size_to_read = std::min(size, (size_t)statbuf.st_size); /* Read buffer and fill whatever is left */ - // TODO: use make_unique<> - this->data = std::shared_ptr(new char[size]); - memset(this->data.get(), fill, size); + this->data = std::make_unique(this->size); + memset(this->data.get(), fill, this->size); ssize_t x = read(fd, this->data.get(), size_to_read); close(fd); diff --git a/lib/vspace.cpp b/lib/vspace.cpp index e2e2946..29315e1 100644 --- a/lib/vspace.cpp +++ b/lib/vspace.cpp @@ -11,12 +11,9 @@ namespace FxOS { -Binding::Binding(MemoryRegion source_region, Buffer source_buffer): - region {source_region}, buffer {source_buffer} +Binding::Binding(MemoryRegion source_region, Buffer const &source_buffer): + region {source_region}, buffer {source_buffer, region.size()} { - /* Extend the buffer if it's not at least as large as the region */ - if(buffer.size < region.size()) - buffer = Buffer(buffer, region.size()); } char const *Binding::translate_dynamic(uint32_t addr, int *size)