Make memory allocation safer by avoiding passing raw pointers around

This commit is contained in:
Konstantin Nazarov 2024-11-29 23:04:04 +00:00
parent 74094491f6
commit 4d406a11f7
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
5 changed files with 108 additions and 90 deletions

View file

@ -37,7 +37,7 @@ Result<void> Arena::gc_roots() {
Result<GcRootList*> Arena::gc_root(GcRootList* node) {
auto pod = TRY(gc_pod(node->_root->_ptr));
auto lst = TRY(alloc<GcRootList>());
auto lst = TRY(alloc_raw<GcRootList>());
node->_root->_ptr = pod;
node->_root->_node = lst;
@ -111,34 +111,34 @@ Result<PodObject*> Arena::gc_pod(PodObject* obj) {
}
Result<PodObject*> Arena::gc_nil(PodNil* obj) {
auto nobj = TRY(alloc<PodNil>());
auto nobj = TRY(alloc_raw<PodNil>());
nobj->header.tag = Tag::Nil;
return nobj;
}
Result<PodObject*> Arena::gc_bool(PodBool* obj) {
auto nobj = TRY(alloc<PodBool>());
auto nobj = TRY(alloc_raw<PodBool>());
nobj->header.tag = Tag::Bool;
nobj->value = obj->value;
return nobj;
}
Result<PodObject*> Arena::gc_int64(PodInt64* obj) {
auto nobj = TRY(alloc<PodInt64>());
auto nobj = TRY(alloc_raw<PodInt64>());
nobj->header.tag = Tag::Int64;
nobj->value = obj->value;
return nobj;
}
Result<PodObject*> Arena::gc_float(PodFloat* obj) {
auto nobj = TRY(alloc<PodFloat>());
auto nobj = TRY(alloc_raw<PodFloat>());
nobj->header.tag = Tag::Float;
nobj->value = obj->value;
return nobj;
}
Result<PodObject*> Arena::gc_string(PodString* obj) {
auto nobj = TRY(alloc<PodString>(obj->size * sizeof(char32_t)));
auto nobj = TRY(alloc_raw<PodString>(obj->size * sizeof(char32_t)));
nobj->header.tag = Tag::String;
nobj->size = obj->size;
memcpy(nobj->data, obj->data, obj->size * sizeof(char32_t));
@ -146,7 +146,7 @@ Result<PodObject*> Arena::gc_string(PodString* obj) {
}
Result<PodObject*> Arena::gc_symbol(PodSymbol* obj) {
auto nobj = TRY(alloc<PodSymbol>(obj->size * sizeof(char32_t)));
auto nobj = TRY(alloc_raw<PodSymbol>(obj->size * sizeof(char32_t)));
nobj->header.tag = Tag::Symbol;
nobj->size = obj->size;
memcpy(nobj->data, obj->data, obj->size * sizeof(char32_t));
@ -154,7 +154,7 @@ Result<PodObject*> Arena::gc_symbol(PodSymbol* obj) {
}
Result<PodObject*> Arena::gc_srcloc(PodSrcLoc* obj) {
auto nobj = TRY(alloc<PodSrcLoc>());
auto nobj = TRY(alloc_raw<PodSrcLoc>());
nobj->header.tag = Tag::SrcLoc;
@ -164,7 +164,7 @@ Result<PodObject*> Arena::gc_srcloc(PodSrcLoc* obj) {
}
Result<PodObject*> Arena::gc_syntax(PodSyntax* obj) {
auto nobj = TRY(alloc<PodSyntax>());
auto nobj = TRY(alloc_raw<PodSyntax>());
nobj->header.tag = Tag::Syntax;
@ -176,7 +176,7 @@ Result<PodObject*> Arena::gc_syntax(PodSyntax* obj) {
}
Result<PodObject*> Arena::gc_pair(PodPair* obj) {
auto nobj = TRY(alloc<PodPair>());
auto nobj = TRY(alloc_raw<PodPair>());
nobj->header.tag = Tag::Pair;
@ -193,7 +193,7 @@ Result<PodObject*> Arena::gc_pair(PodPair* obj) {
}
Result<PodObject*> Arena::gc_array(PodArray* obj) {
auto nobj = TRY(alloc<PodArray>(sizeof(OffPtr<PodObject>) * obj->size));
auto nobj = TRY(alloc_raw<PodArray>(sizeof(OffPtr<PodObject>) * obj->size));
nobj->header.tag = Tag::Array;
nobj->size = obj->size;
for (uint64_t i = 0; i < obj->size; i++) {
@ -204,7 +204,7 @@ Result<PodObject*> Arena::gc_array(PodArray* obj) {
}
Result<PodObject*> Arena::gc_bytearray(PodByteArray* obj) {
auto nobj = TRY(alloc<PodByteArray>(obj->size));
auto nobj = TRY(alloc_raw<PodByteArray>(obj->size));
nobj->header.tag = Tag::ByteArray;
nobj->size = obj->size;
memcpy(nobj->data, obj->data, obj->size);
@ -212,7 +212,8 @@ Result<PodObject*> Arena::gc_bytearray(PodByteArray* obj) {
}
Result<PodObject*> Arena::gc_dict(PodDict* obj) {
auto nobj = TRY(alloc<PodDict>(obj->size * 2 * sizeof(OffPtr<PodObject>)));
auto nobj =
TRY(alloc_raw<PodDict>(obj->size * 2 * sizeof(OffPtr<PodObject>)));
nobj->header.tag = Tag::Dict;
nobj->size = obj->size;
for (uint64_t i = 0; i < obj->size * 2; i++) {
@ -223,7 +224,7 @@ Result<PodObject*> Arena::gc_dict(PodDict* obj) {
}
Result<PodObject*> Arena::gc_opcode(PodOpcode* obj) {
auto nobj = TRY(alloc<PodOpcode>());
auto nobj = TRY(alloc_raw<PodOpcode>());
nobj->header.tag = Tag::Opcode;
nobj->opcode = obj->opcode;
nobj->arg1 = obj->arg1;
@ -235,7 +236,7 @@ Result<PodObject*> Arena::gc_opcode(PodOpcode* obj) {
}
Result<PodObject*> Arena::gc_function(PodFunction* obj) {
auto nobj = TRY(alloc<PodFunction>());
auto nobj = TRY(alloc_raw<PodFunction>());
nobj->header.tag = Tag::Function;
nobj->arity = obj->arity;
nobj->name = TRY(gc_pod(obj->name.get()));
@ -247,7 +248,7 @@ Result<PodObject*> Arena::gc_function(PodFunction* obj) {
}
Result<PodObject*> Arena::gc_stdlib_function(PodStdlibFunction* obj) {
auto nobj = TRY(alloc<PodStdlibFunction>());
auto nobj = TRY(alloc_raw<PodStdlibFunction>());
nobj->header.tag = Tag::StdlibFunction;
nobj->fun_id = obj->fun_id;
@ -255,7 +256,7 @@ Result<PodObject*> Arena::gc_stdlib_function(PodStdlibFunction* obj) {
}
Result<PodObject*> Arena::gc_module(PodModule* obj) {
auto nobj = TRY(alloc<PodModule>());
auto nobj = TRY(alloc_raw<PodModule>());
nobj->header.tag = Tag::Module;
nobj->name = TRY(gc_pod(obj->name.get()));
nobj->fun = TRY(gc_pod(obj->fun.get()));
@ -264,7 +265,8 @@ Result<PodObject*> Arena::gc_module(PodModule* obj) {
}
Result<PodObject*> Arena::gc_stack(PodStackFrame* obj) {
auto nobj = TRY(alloc<PodStackFrame>(sizeof(OffPtr<PodObject>) * obj->size));
auto nobj =
TRY(alloc_raw<PodStackFrame>(sizeof(OffPtr<PodObject>) * obj->size));
nobj->header.tag = Tag::StackFrame;
nobj->parent = TRY(gc_pod(obj->parent.get()));
nobj->fun = TRY(gc_pod(obj->fun.get()));
@ -279,7 +281,7 @@ Result<PodObject*> Arena::gc_stack(PodStackFrame* obj) {
}
Result<PodObject*> Arena::gc_error(PodError* obj) {
auto nobj = TRY(alloc<PodError>());
auto nobj = TRY(alloc_raw<PodError>());
nobj->header.tag = Tag::Error;
nobj->name = TRY(gc_pod(obj->name.get()));
@ -289,7 +291,7 @@ Result<PodObject*> Arena::gc_error(PodError* obj) {
}
Result<PodObject*> Arena::gc_continuation(PodContinuation* obj) {
auto nobj = TRY(alloc<PodContinuation>());
auto nobj = TRY(alloc_raw<PodContinuation>());
nobj->header.tag = Tag::Continuation;
nobj->value = TRY(gc_pod(obj->value.get()));
@ -299,7 +301,7 @@ Result<PodObject*> Arena::gc_continuation(PodContinuation* obj) {
}
Result<PodObject*> Arena::gc_task(PodTask* obj) {
auto nobj = TRY(alloc<PodTask>());
auto nobj = TRY(alloc_raw<PodTask>());
nobj->header.tag = Tag::Task;
nobj->task_id = obj->task_id;
@ -309,7 +311,7 @@ Result<PodObject*> Arena::gc_task(PodTask* obj) {
}
Result<PodObject*> Arena::gc_task_result(PodTaskResult* obj) {
auto nobj = TRY(alloc<PodTaskResult>());
auto nobj = TRY(alloc_raw<PodTaskResult>());
nobj->header.tag = Tag::TaskResult;
nobj->result = TRY(gc_pod(obj->result.get()));

View file

@ -131,7 +131,7 @@ class Arena {
: _first(first), _second(second), _gcroot(), _gc_hold(0) {}
template <class T>
Result<T*> alloc(uint64_t extra = 0) {
Result<T*> alloc_raw(uint64_t extra = 0) {
uint64_t objsize = sizeof(T) + extra;
auto ptr = _first->alloc(objsize);
@ -148,16 +148,27 @@ class Arena {
}
template <class T>
Result<T*> alloc_bytes(uint64_t size = 0) {
Result<GcRoot<T>> alloc(uint64_t extra = 0) {
T* res = TRY(alloc_raw<T>(extra));
return MkGcRoot(res);
}
// TODO: allocating an uninitialized chunk of memory probably
// clashes with garbage collection and may lead to surprising
// results.
template <class T>
Result<GcRoot<T>> alloc_bytes(uint64_t size = 0) {
auto ptr = _first->alloc(size);
if (ptr.has_value()) {
return (T*)ptr.value();
auto res = (T*)ptr.value();
return MkGcRoot(res);
}
TRY(gc());
return (T*)TRY(_first->alloc(size));
auto res = (T*)TRY(_first->alloc(size));
return MkGcRoot(res);
}
Result<void> gc();
@ -228,12 +239,17 @@ class StaticArena : public Arena {
};
template <class T>
Result<T*> arena_alloc(uint64_t extra = 0) {
Result<T*> arena_alloc_raw(uint64_t extra = 0) {
return get_arena().alloc_raw<T>(extra);
}
template <class T>
Result<GcRoot<T>> arena_alloc(uint64_t extra = 0) {
return get_arena().alloc<T>(extra);
}
template <class T>
Result<T*> arena_alloc_bytes(uint64_t size = 0) {
Result<GcRoot<T>> arena_alloc_bytes(uint64_t size = 0) {
return get_arena().alloc_bytes<T>(size);
}
@ -243,7 +259,7 @@ template <class T>
requires std::derived_from<T, PodObject>
Result<GcRoot<T>> GcRoot<T>::create(T* ptr) {
get_arena().sethold(ptr);
auto lst = TRY(arena_alloc<GcRootList>());
auto lst = TRY(arena_alloc_raw<GcRootList>());
get_arena().add_root(lst);
return std::move(GcRoot<T>((T*)get_arena().gethold(), lst));
}

View file

@ -64,7 +64,7 @@ Result<Symbol> Symbol::create(String& rhs) {
pod->size = res_size;
memcpy(pod->data, rhs._value->data, sizeof(char32_t) * rhs_size);
return Symbol(TRY(MkGcRoot(pod)));
return Symbol(std::move(pod));
}
Result<String> String::create(const Symbol& rhs) {
@ -75,7 +75,7 @@ Result<String> String::create(const Symbol& rhs) {
pod->size = res_size;
memcpy(pod->data, rhs._value->data, sizeof(char32_t) * rhs_size);
return String(TRY(MkGcRoot(pod)));
return String(std::move(pod));
}
Result<ByteArray> ByteArray::create(const String& str) {
@ -94,7 +94,7 @@ Result<ByteArray> ByteArray::create(const String& str) {
res = utf8_write_codepoint(res, codepoint);
}
return ByteArray(TRY(MkGcRoot(pod)));
return ByteArray(std::move(pod));
}
Result<String> ByteArray::hex() const {
@ -109,7 +109,7 @@ Result<String> ByteArray::hex() const {
pod->data[i * 2 + 1] = hexa[b & 0xF];
}
return String(TRY(MkGcRoot(pod)));
return String(std::move(pod));
}
Result<ByteArray> ByteArray::to_rle() const {
auto size = _value->size;
@ -152,7 +152,7 @@ Result<ByteArray> ByteArray::to_rle() const {
}
}
return ByteArray(TRY(MkGcRoot(pod)));
return ByteArray(std::move(pod));
}
Result<ByteArray> ByteArray::from_rle() const {
@ -192,14 +192,14 @@ Result<ByteArray> ByteArray::from_rle() const {
}
}
return ByteArray(TRY(MkGcRoot(pod)));
return ByteArray(std::move(pod));
}
Result<SrcLoc> SrcLoc::create(const SourceRange& sourcerange) {
auto pod = TRY(arena_alloc<PodSrcLoc>());
pod->sourcerange = sourcerange;
return SrcLoc(TRY(MkGcRoot(pod)));
return SrcLoc(std::move(pod));
}
short sourceposition_cmp(const SourcePosition& lhs, const SourcePosition& rhs) {
@ -232,7 +232,7 @@ Result<Syntax> Syntax::create(const String& filename, const SrcLoc& srcloc,
pod->srcloc = srcloc.pod();
pod->expression = expression.pod();
return Syntax(TRY(MkGcRoot(pod)));
return Syntax(std::move(pod));
}
Result<Syntax> Syntax::create(const String& filename,
@ -495,7 +495,7 @@ Result<StackFrame> StackFrame::create(const Value& parent, const Value& fun,
pod->pc = 0;
pod->size = 0;
return StackFrame(TRY(MkGcRoot(pod)));
return StackFrame(std::move(pod));
}
Result<Value> StackFrame::copy_value() const {
@ -533,7 +533,7 @@ Result<StackFrame> StackFrame::set(uint64_t idx, const Value& val) const {
}
pod->data[idx] = val.pod();
return StackFrame(TRY(MkGcRoot(pod)));
return StackFrame(std::move(pod));
}
Result<StackFrame> StackFrame::settop(uint64_t idx) const {
@ -557,7 +557,7 @@ Result<StackFrame> StackFrame::settop(uint64_t idx) const {
}
}
return StackFrame(TRY(MkGcRoot(pod)));
return StackFrame(std::move(pod));
}
Result<Value> StackFrame::parent() const {
@ -582,7 +582,7 @@ Result<StackFrame> StackFrame::setpc(uint64_t pc) {
pod->data[i] = _value->data[i];
}
return StackFrame(TRY(MkGcRoot(pod)));
return StackFrame(std::move(pod));
}
Result<StackFrame> StackFrame::incpc() { return setpc(pc() + 1); }
@ -656,7 +656,7 @@ Result<StackFrame> StackFrame::detach(uint64_t depth) const {
pod->data[i] = _value->data[i];
}
return StackFrame(TRY(MkGcRoot(pod)));
return StackFrame(std::move(pod));
}
auto par = TRY(parent());
@ -679,7 +679,7 @@ Result<StackFrame> StackFrame::detach(uint64_t depth) const {
pod->data[i] = _value->data[i];
}
return StackFrame(TRY(MkGcRoot(pod)));
return StackFrame(std::move(pod));
}
Result<StackFrame> StackFrame::attach(const StackFrame& frame) const {
@ -699,7 +699,7 @@ Result<StackFrame> StackFrame::attach(const StackFrame& frame) const {
pod->data[i] = _value->data[i];
}
return StackFrame(TRY(MkGcRoot(pod)));
return StackFrame(std::move(pod));
}
StackFrame& par_frame = *par.to<StackFrame>();
@ -719,7 +719,7 @@ Result<StackFrame> StackFrame::attach(const StackFrame& frame) const {
pod->data[i] = _value->data[i];
}
return StackFrame(TRY(MkGcRoot(pod)));
return StackFrame(std::move(pod));
}
Result<Value> StackFrame::unwind(const Value& val, uint64_t start_from) const {
@ -780,7 +780,7 @@ Result<StackFrame> StackFrame::set_guard(bool guard) const {
pod->data[i] = _value->data[i];
}
return StackFrame(TRY(MkGcRoot(pod)));
return StackFrame(std::move(pod));
}
Result<String> StackFrame::backtrace(uint64_t indent) const {
@ -854,7 +854,7 @@ Result<Continuation> Continuation::create(const Value& value,
pod->value = value.pod();
pod->frame = frame.pod();
return Continuation(TRY(MkGcRoot(pod)));
return Continuation(std::move(pod));
}
Result<Value> Continuation::copy_value() const {
@ -889,7 +889,7 @@ Result<Task> Task::create(uint64_t task_id, const Array& params) {
pod->task_id = task_id;
pod->params = params.pod();
return Task(TRY(MkGcRoot(pod)));
return Task(std::move(pod));
}
Result<Value> Task::copy_value() const {
@ -925,7 +925,7 @@ Result<TaskResult> TaskResult::create(const Value& result, const Value& error) {
pod->result = result.pod();
pod->error = error.pod();
return TaskResult(TRY(MkGcRoot(pod)));
return TaskResult(std::move(pod));
}
Result<Value> TaskResult::copy_value() const {
@ -959,7 +959,7 @@ Result<Pair> Pair::create(const Value& first, const Value& rest) {
pod->first = first.pod();
pod->rest = rest.pod();
return Pair(TRY(MkGcRoot(pod)));
return Pair(std::move(pod));
}
Result<Pair> Pair::create(const Array& arr) {
@ -1030,7 +1030,7 @@ Result<Function> Function::create(const Value& name, uint64_t arity,
pod->code = code.pod();
pod->closure = closure.pod();
return Function(TRY(MkGcRoot(pod)));
return Function(std::move(pod));
}
Result<Function> Function::create(const Function& prototype,
@ -1042,7 +1042,7 @@ Result<Function> Function::create(const Function& prototype,
pod->code = prototype._value->code;
pod->closure = closure.pod();
return Function(TRY(MkGcRoot(pod)));
return Function(std::move(pod));
}
Result<Value> Function::name() const {
@ -1117,7 +1117,7 @@ Result<StdlibFunction> StdlibFunction::create(StdlibFunctionId fun_id) {
auto pod = TRY(arena_alloc<PodStdlibFunction>());
pod->fun_id = (uint64_t)fun_id;
return StdlibFunction(TRY(MkGcRoot(pod)));
return StdlibFunction(std::move(pod));
}
Result<Symbol> StdlibFunction::name() const {
@ -1135,7 +1135,7 @@ Result<Module> Module::create(const Value& name, const Function& fun) {
pod->name = name.pod();
pod->fun = fun.pod();
return Module(TRY(MkGcRoot(pod)));
return Module(std::move(pod));
}
Result<Value> Module::name() const { return Value::create(_value->name.get()); }
@ -1206,7 +1206,7 @@ Result<Array> Array::set(uint64_t idx, const Value& value) const {
}
pod->data[idx] = value.pod();
return Array(TRY(MkGcRoot(pod)));
return Array(std::move(pod));
}
Result<Array> Array::set(const Value& key, const Value& value) const {
@ -1230,7 +1230,7 @@ Result<Array> Array::append(const Value& rhs) const {
}
pod->data[TRY(this->size())] = rhs.pod();
return Array(TRY(MkGcRoot(pod)));
return Array(std::move(pod));
}
short cmp_tag(Tag lhs, Tag rhs) {
@ -1299,7 +1299,7 @@ Result<Dict> Dict::set(const Value& key, const Value& value) const {
}
}
return Dict(TRY(MkGcRoot(pod)));
return Dict(std::move(pod));
}
Result<Value> Dict::set_value(const Value& key, const Value& value) const {

View file

@ -167,7 +167,7 @@ class Nil : public Object {
static Result<Nil> create() {
auto pod = TRY(arena_alloc<PodNil>());
return Nil(TRY(MkGcRoot(pod)));
return Nil(std::move(pod));
}
virtual Result<Value> copy_value() const final;
@ -211,7 +211,7 @@ class Array : public Object {
pod->size = 0;
return Array(TRY(MkGcRoot(pod)));
return Array(std::move(pod));
}
virtual Result<uint64_t> size() const { return _value->size; }
@ -243,7 +243,7 @@ class Array : public Object {
pod->data[lhs_size + i] = rhs._value->data[i].get();
}
return Array(TRY(MkGcRoot(pod)));
return Array(std::move(pod));
}
Result<Array> slice(uint64_t start, uint64_t end) const {
@ -255,7 +255,7 @@ class Array : public Object {
pod->data[i] = _value->data[start + i];
}
return Array(TRY(MkGcRoot(pod)));
return Array(std::move(pod));
}
private:
@ -293,7 +293,7 @@ class ByteArray : public Object {
memcpy(pod->data, chars, size * sizeof(char32_t));
pod->size = size;
return ByteArray(TRY(MkGcRoot(pod)));
return ByteArray(std::move(pod));
}
static Result<ByteArray> create(const char* str) {
@ -303,7 +303,7 @@ class ByteArray : public Object {
memcpy(pod->data, str, size);
pod->size = size;
return ByteArray(TRY(MkGcRoot(pod)));
return ByteArray(std::move(pod));
}
static Result<ByteArray> create(const String& str);
@ -329,7 +329,7 @@ class ByteArray : public Object {
memcpy(pod->data, _value->data, sizeof(char) * lhs_size);
memcpy(pod->data, rhs + lhs_size, sizeof(char) * rhs_size);
return ByteArray(TRY(MkGcRoot(pod)));
return ByteArray(std::move(pod));
}
Result<ByteArray> concat(const char* rhs, uint64_t rhs_size) const {
@ -341,7 +341,7 @@ class ByteArray : public Object {
memcpy(pod->data, _value->data, sizeof(char) * lhs_size);
memcpy(pod->data + lhs_size, rhs, sizeof(char) * rhs_size);
return ByteArray(TRY(MkGcRoot(pod)));
return ByteArray(std::move(pod));
}
Result<ByteArray> concat(ByteArray& rhs) const {
@ -354,7 +354,7 @@ class ByteArray : public Object {
memcpy(pod->data, _value->data, sizeof(char) * lhs_size);
memcpy(pod->data + lhs_size, rhs._value->data, sizeof(char) * rhs_size);
return ByteArray(TRY(MkGcRoot(pod)));
return ByteArray(std::move(pod));
}
Result<ByteArray> slice(uint64_t start, uint64_t end) const {
@ -364,7 +364,7 @@ class ByteArray : public Object {
pod->size = res_size;
memcpy(pod->data, _value->data + start, sizeof(char) * res_size);
return ByteArray(TRY(MkGcRoot(pod)));
return ByteArray(std::move(pod));
}
Result<String> hex() const;
@ -407,7 +407,7 @@ class Dict : public Object {
pod->size = 0;
return Dict(TRY(MkGcRoot(pod)));
return Dict(std::move(pod));
}
virtual Result<Value> copy_value() const final;
@ -436,7 +436,7 @@ class Dict : public Object {
pod->data[i] = rhs._value->data[i];
}
return Dict(TRY(MkGcRoot(pod)));
return Dict(std::move(pod));
}
virtual Result<uint64_t> size() const final { return _value->size; }
@ -495,7 +495,7 @@ class String : public Object {
memcpy(pod->data, chars, size * sizeof(char32_t));
pod->size = size;
return String(TRY(MkGcRoot(pod)));
return String(std::move(pod));
}
static Result<String> create(const ByteArray& bytes) {
@ -506,7 +506,7 @@ class String : public Object {
for (uint64_t i = 0; i < size; i++) pod->data[i] = TRY(bytes[i]);
pod->size = size;
return String(TRY(MkGcRoot(pod)));
return String(std::move(pod));
}
static Result<String> create(const Symbol& sym);
@ -518,7 +518,7 @@ class String : public Object {
for (uint64_t i = 0; i < size; i++) pod->data[i] = str[i];
pod->size = size;
return String(TRY(MkGcRoot(pod)));
return String(std::move(pod));
}
virtual Result<uint64_t> size() const final { return _value->size; }
@ -542,7 +542,7 @@ class String : public Object {
memcpy(pod->data, _value->data, sizeof(char32_t) * lhs_size);
for (uint64_t i = 0; i < rhs_size; i++) pod->data[lhs_size + i] = rhs[i];
return String(TRY(MkGcRoot(pod)));
return String(std::move(pod));
}
Result<String> concat(const char* rhs) { return concat(rhs, strlen(rhs)); }
@ -556,7 +556,7 @@ class String : public Object {
memcpy(pod->data, _value->data, sizeof(char32_t) * lhs_size);
for (uint64_t i = 0; i < rhs_size; i++) pod->data[lhs_size + i] = rhs[i];
return String(TRY(MkGcRoot(pod)));
return String(std::move(pod));
}
Result<String> concat(const String& rhs) const {
@ -569,7 +569,7 @@ class String : public Object {
memcpy(pod->data, _value->data, sizeof(char32_t) * lhs_size);
memcpy(pod->data + lhs_size, rhs._value->data, sizeof(char32_t) * rhs_size);
return String(TRY(MkGcRoot(pod)));
return String(std::move(pod));
}
Result<String> slice(uint64_t start, uint64_t end) const {
@ -579,7 +579,7 @@ class String : public Object {
pod->size = res_size;
memcpy(pod->data, _value->data + start, sizeof(char32_t) * res_size);
return String(TRY(MkGcRoot(pod)));
return String(std::move(pod));
}
friend class Symbol;
@ -651,7 +651,7 @@ class Symbol : public Object {
memcpy(pod->data, chars, size * sizeof(char32_t));
return Symbol(TRY(MkGcRoot(pod)));
return Symbol(std::move(pod));
}
static Result<Symbol> create(const char* str) {
@ -661,7 +661,7 @@ class Symbol : public Object {
for (uint64_t i = 0; i < size; i++) pod->data[i] = str[i];
pod->size = size;
return Symbol(TRY(MkGcRoot(pod)));
return Symbol(std::move(pod));
}
static Result<Symbol> create(String& rhs);
@ -705,7 +705,7 @@ class SrcLoc : public Object {
pod->sourcerange.start = start;
pod->sourcerange.end = end;
return SrcLoc(TRY(MkGcRoot(pod)));
return SrcLoc(std::move(pod));
}
SourceRange sourcerange() const;
@ -836,7 +836,7 @@ class Int64 : public Object {
auto pod = TRY(arena_alloc<PodInt64>());
pod->value = val;
return Int64(TRY(MkGcRoot(pod)));
return Int64(std::move(pod));
}
int64_t value() const { return _value->value; }
@ -892,7 +892,7 @@ class Float : public Object {
auto pod = TRY(arena_alloc<PodFloat>());
pod->value = val;
return Float(TRY(MkGcRoot(pod)));
return Float(std::move(pod));
}
double value() const { return _value->value; }
@ -928,7 +928,7 @@ class Bool : public Object {
auto pod = TRY(arena_alloc<PodBool>());
pod->value = val;
return Bool(TRY(MkGcRoot(pod)));
return Bool(std::move(pod));
}
bool value() const { return _value->value; }
@ -969,7 +969,7 @@ class Opcode : public Object {
pod->arg3 = arg3;
pod->arg4 = arg4;
return Opcode(TRY(MkGcRoot(pod)));
return Opcode(std::move(pod));
}
Oc opcode() const { return _value->opcode; }
@ -1159,7 +1159,7 @@ class StackFrame : public Object {
pod->data[i] = _value->data[start + i];
}
return Array(TRY(MkGcRoot(pod)));
return Array(std::move(pod));
}
Result<StackFrame> call(const Value& fun, uint64_t start, uint64_t end,
@ -1214,7 +1214,7 @@ class Error : public Object {
pod->name = name_str.pod();
pod->message = message_str.pod();
return Error(TRY(MkGcRoot(pod)));
return Error(std::move(pod));
}
static Result<Error> create(const Symbol& name, const String& message) {
@ -1223,7 +1223,7 @@ class Error : public Object {
pod->name = name.pod();
pod->message = message.pod();
return Error(TRY(MkGcRoot(pod)));
return Error(std::move(pod));
}
Result<Value> name() const;

View file

@ -378,7 +378,7 @@ Result<ByteArray> serialize(const Value& val) {
Serializer ss((uint8_t*)pod->data, false);
ss.serialize_object(val.pod());
auto ba = ByteArray(TRY(MkGcRoot(pod)));
auto ba = ByteArray(std::move(pod));
ba = TRY(ba.to_rle());
return ba;
@ -388,8 +388,8 @@ Result<Value> deserialize(const ByteArray& val) {
auto decompressed = TRY(val.from_rle());
auto size = TRY(decompressed.size());
PodObject* obj = TRY(arena_alloc_bytes<PodObject>(size));
memcpy(obj, decompressed.raw_bytes(), size);
auto obj = TRY(arena_alloc_bytes<PodObject>(size));
memcpy(obj.get(), decompressed.raw_bytes(), size);
return TRY(Value::create(obj));
return TRY(Value::create(obj.get()));
}