From 475b7c531bac977f41cc95dc8b78b548312282a7 Mon Sep 17 00:00:00 2001 From: Tijmen van Nesselrooij Date: Sat, 18 May 2019 13:04:39 +0200 Subject: [PATCH] Make vector resize / reserve simple --- src/sequential/vector.hpp | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/sequential/vector.hpp b/src/sequential/vector.hpp index 1ea3618..3a78446 100644 --- a/src/sequential/vector.hpp +++ b/src/sequential/vector.hpp @@ -7,7 +7,6 @@ class Vector { private: T * data; - std::size_t byteSize; std::size_t reserveSize; std::size_t size; @@ -17,27 +16,21 @@ private: { std::free(data); data = nullptr; - byteSize = 0ul; reserveSize = 0ul; return; } - std::size_t newByteSize = allocationSize * sizeof(T); - newByteSize += newByteSize % 32ul; // Allocate in blocks of 32 bytes - - if(newByteSize <= byteSize) + if(allocationSize == size) { - reserveSize = allocationSize; return; } - void * result = std::realloc(data, newByteSize); + void * result = std::realloc(data, sizeof(T) * allocationSize); if(result == nullptr) { throw std::runtime_error("Cannot allocate the requested size of memory."); } - byteSize = newByteSize; reserveSize = allocationSize; data = reinterpret_cast(result); } @@ -82,7 +75,6 @@ public: Vector() : data(nullptr), - byteSize(0ul), reserveSize(0ul), size(0ul) {}