From b05cf56b1a51b0c178c606ca41f6fb0c0208da27 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Mon, 9 Oct 2023 05:00:38 -0700 Subject: [PATCH] start buffer test --- Buffer.cc | 58 ++++++++++++++++++++++++++------------------------- Buffer.h | 4 ++-- BufferTest.cc | 6 +++++- 3 files changed, 37 insertions(+), 31 deletions(-) diff --git a/Buffer.cc b/Buffer.cc index b799c0f..8ed0632 100644 --- a/Buffer.cc +++ b/Buffer.cc @@ -2,6 +2,7 @@ // Created by kyle on 2023-10-09. // +#include #include #include "Buffer.h" @@ -12,6 +13,26 @@ constexpr size_t defaultCapacity = 32; constexpr size_t maxReasonableLine = 8192; +static size_t +nearestPower(size_t x) +{ + if (x == 0) { + return 0; + } + + x--; + + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + x |= x >> 32; + + return x; +} + + Buffer::Buffer() : contents(nullptr), length(0), capacity(0) { @@ -31,16 +52,10 @@ Buffer::Buffer(const char *data) { size_t datalen = strnlen(data, maxReasonableLine); - this->Append(data, datalen); + this->Append((uint8_t *)data, datalen); } -uint8_t * -Buffer::Contents() -{ - return this->contents; -} - bool Buffer::Append(uint8_t *data, size_t datalen) { @@ -68,11 +83,6 @@ Buffer::Insert(size_t index, uint8_t *data, size_t datalen) { auto resized = this->shift(index, datalen); - if (newCap > 0) { - this->Resize(newCap); - resized = true; - } - memcpy(this->contents + index, data, datalen); return resized; } @@ -83,11 +93,6 @@ Buffer::Insert(size_t index, uint8_t c) return this->Insert(index, &c, 1); } -size_t -Buffer::Size() -{ - return this->length; -} void Buffer::Resize(size_t newCapacity) @@ -113,11 +118,16 @@ Buffer::Resize(size_t newCapacity) size_t Buffer::Trim() { - size_t projectedCapacity = this->capacity * 2; + size_t projectedCapacity = nearestPower(this->length); + + assert(projectedCapacity >= length); if (projectedCapacity < this->capacity) { this->Resize(projectedCapacity); + return this->Capacity(); } + + return 0; } void @@ -143,20 +153,12 @@ Buffer::mustGrow(size_t delta) } auto newCapacity = delta + this->length; - newCapacity--; - - newCapacity |= newCapacity >> 1; - newCapacity |= newCapacity >> 2; - newCapacity |= newCapacity >> 4; - newCapacity |= newCapacity >> 8; - newCapacity |= newCapacity >> 16; - newCapacity |= newCapacity >> 32; - return newCapacity; + return nearestPower(newCapacity); } bool -Buffer:shift(size_t offset, size_t delta) +Buffer::shift(size_t offset, size_t delta) { auto resized = false; auto newCap = this->mustGrow(delta); diff --git a/Buffer.h b/Buffer.h index 8e6045c..f7fd291 100644 --- a/Buffer.h +++ b/Buffer.h @@ -18,7 +18,7 @@ public: Buffer(const char *); uint8_t *Contents() { return this->contents; } - size_t Size() { return this->size; }; + size_t Size() { return this->length; }; size_t Capacity() { return this->capacity; } bool Append(uint8_t *data, size_t datalen); @@ -35,7 +35,7 @@ public: void Reclaim(); private: - bool mustGrow(size_t delta); + size_t mustGrow(size_t delta); bool shift(size_t offset, size_t delta); uint8_t *contents; diff --git a/BufferTest.cc b/BufferTest.cc index 37359cf..858322b 100644 --- a/BufferTest.cc +++ b/BufferTest.cc @@ -6,7 +6,11 @@ int main() { - Buffer buffer("hlo, world"); + kge::Buffer buffer("hlo, world"); + + std::cout << buffer.Contents() << std::endl; + + buffer.Insert(1, (uint8_t *)"el", 2); std::cout << buffer.Contents() << std::endl; }