valgrind fixes

This commit is contained in:
Kyle Isom 2023-10-09 13:16:05 -07:00
parent f8041c2bfa
commit 5c06c81029
2 changed files with 26 additions and 6 deletions

View File

@ -5,6 +5,7 @@
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include "Buffer.h" #include "Buffer.h"
#include <iostream>
namespace klib { namespace klib {
@ -18,7 +19,9 @@ nearestPower(size_t x)
{ {
if (x == 0) { if (x == 0) {
return 0; return 0;
} };
std::cout << "x -> ";
x--; x--;
@ -29,6 +32,8 @@ nearestPower(size_t x)
x |= x >> 16; x |= x >> 16;
x |= x >> 32; x |= x >> 32;
std::cout << x + 1 << std::endl;
return x+1; return x+1;
} }
@ -170,6 +175,9 @@ Buffer::Trim()
void void
Buffer::Clear() Buffer::Clear()
{ {
if (this->length == 0) {
return;
}
memset(this->contents, 0, this->length); memset(this->contents, 0, this->length);
this->length = 0; this->length = 0;
} }
@ -179,7 +187,13 @@ Buffer::Reclaim()
{ {
this->Clear(); this->Clear();
if (this->contents == nullptr) {
assert(this->length == 0);
assert(this->capacity == 0);
return;
}
delete this->contents; delete this->contents;
this->contents = nullptr;
this->capacity = 0; this->capacity = 0;
} }
@ -216,11 +230,11 @@ Buffer::shiftRight(size_t offset, size_t delta)
bool bool
Buffer::shiftLeft(size_t offset, size_t delta) Buffer::shiftLeft(size_t offset, size_t delta)
{ {
auto resized = false; // for (size_t i = offset; i < this->length; i++) {
// this->contents[i] = this->contents[i+delta];
// }
for (size_t i = offset; i < this->length; i++) { memmove(this->contents+offset, this->contents+(offset+delta), this->length);
this->contents[i] = this->contents[i+delta];
}
return this->Trim() != 0; return this->Trim() != 0;
} }

View File

@ -24,6 +24,12 @@ main()
buffer.Append("and now for something completely different..."); buffer.Append("and now for something completely different...");
std::cout << buffer.Contents() << std::endl; std::cout << buffer.Contents() << std::endl;
std::cout << "Length: " << buffer.Length() << ", capacity " << buffer.Capacity() << std::endl;
buffer.Resize(128);
std::cout << "Length: " << buffer.Length() << ", capacity " << buffer.Capacity() << std::endl;
buffer.Trim();
std::cout << "Length: " << buffer.Length() << ", capacity " << buffer.Capacity() << std::endl;
return 0;
return 0;
} }