diff --git a/include/avian/codegen/promise.h b/include/avian/codegen/promise.h index 6d683533dc..dce86bb9dc 100644 --- a/include/avian/codegen/promise.h +++ b/include/avian/codegen/promise.h @@ -118,7 +118,7 @@ class OffsetPromise : public Promise { class ListenPromise : public Promise { public: - ListenPromise(vm::System* s, util::Allocator* allocator) + ListenPromise(vm::System* s, util::AllocOnly* allocator) : s(s), allocator(allocator), listener(0) { } @@ -142,7 +142,7 @@ class ListenPromise : public Promise { } vm::System* s; - util::Allocator* allocator; + util::AllocOnly* allocator; Listener* listener; Promise* promise; }; @@ -150,7 +150,7 @@ class ListenPromise : public Promise { class DelayedPromise : public ListenPromise { public: DelayedPromise(vm::System* s, - util::Allocator* allocator, + util::AllocOnly* allocator, Promise* basis, DelayedPromise* next) : ListenPromise(s, allocator), basis(basis), next(next) diff --git a/include/avian/util/allocator.h b/include/avian/util/allocator.h index fc4163c775..8b5b1b9232 100644 --- a/include/avian/util/allocator.h +++ b/include/avian/util/allocator.h @@ -16,24 +16,21 @@ namespace avian { namespace util { -class Allocator { +class AllocOnly { public: - - // Returns null on failure - virtual void* tryAllocate(size_t size) = 0; - - // Aborts on failure virtual void* allocate(size_t size) = 0; +}; - // By contract, size MUST be the original size of the allocated data, and p - // MUST point to the original base of the allocated data. No partial frees. +class Allocator : public AllocOnly { + public: + virtual void* tryAllocate(size_t size) = 0; virtual void free(const void* p, size_t size) = 0; }; } // namespace util } // namespace avian -inline void* operator new(size_t size, avian::util::Allocator* allocator) +inline void* operator new(size_t size, avian::util::AllocOnly* allocator) { return allocator->allocate(size); } diff --git a/include/avian/util/slice.h b/include/avian/util/slice.h index 69667b63c4..13930bc8a6 100644 --- a/include/avian/util/slice.h +++ b/include/avian/util/slice.h @@ -69,12 +69,12 @@ class Slice { return Slice(this->begin() + begin, count); } - static Slice alloc(Allocator* a, size_t count) + static Slice alloc(AllocOnly* a, size_t count) { return Slice((T*)a->allocate(sizeof(T) * count), count); } - static Slice allocAndSet(Allocator* a, size_t count, const T& item) + static Slice allocAndSet(AllocOnly* a, size_t count, const T& item) { Slice slice(alloc(a, count)); for (size_t i = 0; i < count; i++) { @@ -83,14 +83,14 @@ class Slice { return slice; } - Slice clone(Allocator* a, size_t newCount) + Slice clone(AllocOnly* a, size_t newCount) { T* newItems = (T*)a->allocate(newCount * sizeof(T)); memcpy(newItems, items, min(count, newCount) * sizeof(T)); return Slice(newItems, newCount); } - Slice cloneAndSet(Allocator* a, size_t newCount, const T& item) + Slice cloneAndSet(AllocOnly* a, size_t newCount, const T& item) { Slice slice(clone(a, newCount)); for (size_t i = count; i < newCount; i++) { diff --git a/src/avian/zone.h b/src/avian/zone.h index fa1de3ddeb..e3b0662bd6 100644 --- a/src/avian/zone.h +++ b/src/avian/zone.h @@ -17,7 +17,7 @@ namespace vm { -class Zone : public avian::util::Allocator { +class Zone : public avian::util::AllocOnly { public: class Segment { public: @@ -31,9 +31,8 @@ class Zone : public avian::util::Allocator { uint8_t data[0]; }; - Zone(System* s, Allocator* allocator, unsigned minimumFootprint) - : s(s), - allocator(allocator), + Zone(avian::util::Allocator* allocator, size_t minimumFootprint) + : allocator(allocator), segment(0), minimumFootprint(minimumFootprint < sizeof(Segment) ? 0 @@ -56,6 +55,46 @@ class Zone : public avian::util::Allocator { segment = 0; } + virtual void* allocate(size_t size) + { + size = pad(size); + void* p = tryAllocate(size); + if (p) { + return p; + } else { + ensure(size); + void* r = segment->data + segment->position; + segment->position += size; + return r; + } + } + + void* peek(size_t size) + { + size = pad(size); + Segment* s = segment; + while (s->position < size) { + size -= s->position; + s = s->next; + } + return s->data + (s->position - size); + } + + void pop(size_t size) + { + size = pad(size); + Segment* s = segment; + while (s->position < size) { + size -= s->position; + Segment* next = s->next; + allocator->free(s, sizeof(Segment) + s->size); + s = next; + } + s->position -= size; + segment = s; + } + + private: static unsigned padToPage(unsigned size) { return (size + (LikelyPageSizeInBytes - 1)) & ~(LikelyPageSizeInBytes - 1); @@ -95,7 +134,7 @@ class Zone : public avian::util::Allocator { } } - virtual void* tryAllocate(size_t size) + void* tryAllocate(size_t size) { size = pad(size); if (tryEnsure(size)) { @@ -107,54 +146,7 @@ class Zone : public avian::util::Allocator { } } - virtual void* allocate(size_t size) - { - size = pad(size); - void* p = tryAllocate(size); - if (p) { - return p; - } else { - ensure(size); - void* r = segment->data + segment->position; - segment->position += size; - return r; - } - } - - void* peek(unsigned size) - { - size = pad(size); - Segment* s = segment; - while (s->position < size) { - size -= s->position; - s = s->next; - } - return s->data + (s->position - size); - } - - void pop(unsigned size) - { - size = pad(size); - Segment* s = segment; - while (s->position < size) { - size -= s->position; - Segment* next = s->next; - allocator->free(s, sizeof(Segment) + s->size); - s = next; - } - s->position -= size; - segment = s; - } - - virtual void free(const void*, size_t) - { - // not supported - abort(s); - } - - System* s; - Allocator* allocator; - void* context; + avian::util::Allocator* allocator; Segment* segment; unsigned minimumFootprint; }; diff --git a/src/compile.cpp b/src/compile.cpp index 0a0a3775ba..ead6942a1e 100644 --- a/src/compile.cpp +++ b/src/compile.cpp @@ -1158,7 +1158,7 @@ class Context { Context(MyThread* t, BootContext* bootContext, GcMethod* method) : thread(t), - zone(t->m->system, t->m->heap, InitialZoneCapacityInBytes), + zone(t->m->heap, InitialZoneCapacityInBytes), assembler(t->arch->makeAssembler(t->m->heap, &zone)), client(t), compiler(makeCompiler(t->m->system, assembler, &zone, &client)), @@ -1191,7 +1191,7 @@ class Context { Context(MyThread* t) : thread(t), - zone(t->m->system, t->m->heap, InitialZoneCapacityInBytes), + zone(t->m->heap, InitialZoneCapacityInBytes), assembler(t->arch->makeAssembler(t->m->heap, &zone)), client(t), compiler(0), @@ -3743,8 +3743,7 @@ class Stack { Stack* s; }; - Stack(MyThread* t) - : thread(t), zone(t->m->system, t->m->heap, 0), resource(this) + Stack(MyThread* t) : thread(t), zone(t->m->heap, 0), resource(this) { } diff --git a/src/tools/bootimage-generator/main.cpp b/src/tools/bootimage-generator/main.cpp index b85cc1317c..8860496be3 100644 --- a/src/tools/bootimage-generator/main.cpp +++ b/src/tools/bootimage-generator/main.cpp @@ -1431,7 +1431,7 @@ void writeBootImage2(Thread* t, // sequence point, for gc (don't recombine statements) roots(t)->setOutOfMemoryError(t, throwable); - Zone zone(t->m->system, t->m->heap, 64 * 1024); + Zone zone(t->m->heap, 64 * 1024); class MyCompilationHandler : public Processor::CompilationHandler { public: diff --git a/unittest/codegen/assembler-test.cpp b/unittest/codegen/assembler-test.cpp index fa20b9a26c..f463b0c5b4 100644 --- a/unittest/codegen/assembler-test.cpp +++ b/unittest/codegen/assembler-test.cpp @@ -52,7 +52,7 @@ class Asm { Assembler* a; Asm(BasicEnv& env) - : zone(env.s, env.heap, 8192), a(env.arch->makeAssembler(env.heap, &zone)) + : zone(env.heap, 8192), a(env.arch->makeAssembler(env.heap, &zone)) { }