Commit 952ae5c5 authored by Kenton Varda's avatar Kenton Varda

Remove byte/word default constructors and rely on calloc for zero-initialization.

parent edad34c4
......@@ -25,6 +25,7 @@
#include <vector>
#include <string.h>
#include <iostream>
#include <stdlib.h>
namespace capnproto {
......@@ -45,12 +46,16 @@ public:
private:
WordCount preferredSegmentSize;
std::vector<std::unique_ptr<SegmentBuilder>> segments;
std::vector<std::unique_ptr<word[]>> memory;
std::vector<word*> memory;
};
MallocMessage::MallocMessage(WordCount preferredSegmentSize)
: preferredSegmentSize(preferredSegmentSize) {}
MallocMessage::~MallocMessage() {}
MallocMessage::~MallocMessage() {
for (word* ptr: memory) {
free(ptr);
}
}
SegmentReader* MallocMessage::tryGetSegment(SegmentId id) {
if (id.value >= segments.size()) {
......@@ -77,10 +82,9 @@ SegmentBuilder* MallocMessage::getSegment(SegmentId id) {
SegmentBuilder* MallocMessage::getSegmentWithAvailable(WordCount minimumAvailable) {
if (segments.empty() || segments.back()->available() < minimumAvailable) {
WordCount newSize = std::max(minimumAvailable, preferredSegmentSize);
memory.push_back(std::unique_ptr<word[]>(new word[newSize / WORDS]));
memset(memory.back().get(), 0, newSize / WORDS * sizeof(word));
memory.push_back(reinterpret_cast<word*>(calloc(newSize / WORDS, sizeof(word))));
segments.push_back(std::unique_ptr<SegmentBuilder>(new SegmentBuilder(
this, SegmentId(segments.size()), memory.back().get(), newSize)));
this, SegmentId(segments.size()), memory.back(), newSize)));
}
return segments.back().get();
}
......
......@@ -317,8 +317,8 @@ inline constexpr auto operator*(UnitRatio<Number1, Unit2, Unit> ratio,
// =======================================================================================
// Raw memory types and measures
class byte { uint8_t content; CAPNPROTO_DISALLOW_COPY(byte); public: byte() = default; };
class word { uint64_t content; CAPNPROTO_DISALLOW_COPY(word); public: word() = default; };
class byte { uint8_t content; CAPNPROTO_DISALLOW_COPY(byte); };
class word { uint64_t content; CAPNPROTO_DISALLOW_COPY(word); };
// byte and word are opaque types with sizes of 8 and 64 bits, respectively. These types are useful
// only to make pointer arithmetic clearer. Since the contents are private, the only way to access
// them is to first reinterpret_cast to some other pointer type.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment