Skip to content

Commit

Permalink
Add GC-Safety annotations
Browse files Browse the repository at this point in the history
See #6
  • Loading branch information
devin122 committed Dec 29, 2020
1 parent b088718 commit 56aead3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
16 changes: 16 additions & 0 deletions include/hustle/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,20 @@ static_assert(CELL_INT_MIN < 0);
static_assert(CELL_INT_MAX > 0);
} // namespace hustle

// GC safety annotations.
#if defined(HUSTLE_GC_SAFETY)
#define GC_SAFETY(x) __attribute((annotate("gc::" #x)))
#else
#define GC_SAFETY(x)
#endif

/// Mark a function as pontentially triggering a GC
#define HUSTLE_MAY_ALLOCATE GC_SAFETY(may_allocate)

/// Mark a class as being allocated on the GC'd heap
#define HUSTLE_HEAP_ALLOCATED GC_SAFETY(heap_allocated)

/// Marks a class as aliasing a heap pointer
#define HUSTLE_HEAP_POINTER GC_SAFETY(heap_pointer)

#endif
2 changes: 1 addition & 1 deletion include/hustle/GC.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Heap {
~Heap() = default;
// TODO: this should be inlined for perf, but for the moment we leave it this
// way for flexibility
Object* allocate(size_t size);
Object* allocate(size_t size) HUSTLE_MAY_ALLOCATE;
void gc();

private:
Expand Down
2 changes: 1 addition & 1 deletion include/hustle/Object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ struct Object {

// constexpr bool is_marked() const noexcept { return header.marked; }
// constexpr void mark(bool m = true) noexcept { header.marked = m ? 1: 0;}
};
} HUSTLE_HEAP_ALLOCATED;

#include "classes.def"

Expand Down
12 changes: 7 additions & 5 deletions include/hustle/VM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct VM {
VM(const VM&) = delete;
VM& operator=(const VM&) = delete;

void evaluate(Cell c);
void evaluate(Cell c) HUSTLE_MAY_ALLOCATE;

void interpreter_loop();
void step_hook();
Expand All @@ -75,16 +75,18 @@ struct VM {

// CallStackEntry enter(Word)

void call(Cell c);
void call(Cell c) HUSTLE_MAY_ALLOCATE;

// hack because im lazy
template <typename T>
void push_obj(T* o) {
push(Cell(o));
}

Word* register_primitive(const char* name, CallType handler);
void register_symbol(String* str, Quotation* quote, bool parseword = false);
Word* register_primitive(const char* name,
CallType handler) HUSTLE_MAY_ALLOCATE;
void register_symbol(String* str, Quotation* quote,
bool parseword = false) HUSTLE_MAY_ALLOCATE;
void register_symbol(String* string, Word* word);

cell_t lookup_symbol(const std::string& name);
Expand All @@ -103,7 +105,7 @@ struct VM {

template <typename T, typename... Args>
// std::enable_if_t<std::is_base_of_v<Object, T>, T*>
T* allocate(Args... args) {
T* allocate(Args... args) HUSTLE_MAY_ALLOCATE {
size_t allocation_size =
hustle::object_allocation_size<T>(std::forward<Args>(args)...);
void* memory = heap_.allocate(allocation_size);
Expand Down
8 changes: 4 additions & 4 deletions include/hustle/cell.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@

#ifndef HUSTLE_CELL_HPP
#define HUSTLE_CELL_HPP
//#include "Object.hpp"
#include "Core.hpp"

#include <hustle/Core.hpp>
#include <hustle/Support/Assert.hpp>
#include <type_traits>

Expand Down Expand Up @@ -169,7 +169,7 @@ class Cell {
protected:
explicit constexpr Cell(cell_t cell) noexcept : raw_(cell) {}
cell_t raw_ = 0;
};
} HUSTLE_HEAP_POINTER;

// constexpr TypedCell<intptr_t> operator"" _c(intptr_t x) { return
// Cell::from_int(x); }
Expand Down Expand Up @@ -211,7 +211,7 @@ class TypedCell : public Cell {
}
// operator Cell() { return Cell(raw_); }
// constexpr operator cell_t() noexcept { return raw_; }
};
} HUSTLE_HEAP_POINTER;

} // namespace hustle

Expand Down
2 changes: 1 addition & 1 deletion utils/hustlegen/classes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static void print_class_defs(IndentingStream& out, const ClassList& classes) {
}

out.outdent();
out.writeln("}};\n");
out.writeln("}} HUSTLE_HEAP_ALLOCATED;\n");
}
}

Expand Down

0 comments on commit 56aead3

Please sign in to comment.